Summary of MongoDB query tips

  • 2020-05-10 23:06:23
  • OfStack

In MongoDB, the db.collection.find () method is used to retrieve documents from the collection. The db.collection.find () method returns a cursor that retrieves the document. The db.collection.findOne () method also reads, returning a single document. On the internal implementation, the db.collection.findOne () method is db.collection.find () using limit 1.

All documents in the query collection:

1.1 empty query documents ({}) can retrieve all documents in a collection:

db.inventory.find( {} )

2. find() without specifying an query document is equivalent to specifying a query for an empty query document. Therefore, the following query is equivalent to the above query:

db.inventory.find()

Specify equality conditions:
Use { < field > : < value > The} document specifies the equality condition and queries for all includes < field > Field with a value of < value > The document. The following example retrieves all documents with an type field value of snacks from the inventory collection:

db.inventory.find( { type: "snacks" } )

Specify conditions using the query operator:
You can specify conditions in MongoDB using the query operator. The following example queries the type field for a value of 'food' or 'snacks' from the inventory collection:

db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } )

Although the $or operator can be used for this query, $in is used instead of $or for an equality check for the same 1 field.

Specify AND conditions:
The conformance query can specify multiple document fields in the criteria. Logic AND connects the composite query conditions of 1, and queries the documents that meet all the conditions. In the following example, the query document specifies a query condition equal to the food and price fields that are less than the specified value ($lt) :

db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )

This query selects documents where all type field values are equal to food and price field values are less than 9.95.

Specify OR conditions:
Using the $or operator, you can specify a composite query that USES a logical OR join to select documents that match at least one of the criteria in the collection. In the following example, all documents in the query collection whose qty field value is greater than ($gt)100 or price field value is less than ($lt)9.95:

db.inventory.find(
    { $or: [
        { qty: { $gt: 100 } },
              { price: { $lt: 9.95 } }
      ]
    }
)

Specify both the AND and OR conditions:
With more criteria, you can specify precise query criteria. In the following example, documents that conform to query documents select all type fields in the query document collection with a value of 'food' and an qty value greater than ($gt)100 or an price value less than ($lt)9.95:

db.inventory.find( { type: 'food', $or: [ { qty: { $gt: 100 } },
                                                  { price:{ $lt:9.95 } }
                                                ]
                          } ) 


A document:
When a field contains an embedded document (that is, a subdocument), you can specify the entire subdocument as the value of one field, or you can use the dot symbol to "enter" the subfile and specify the value of each field of the subdocument:
1. Exact matching of sub-documents:
Specify 1 equality condition to match the entire subdocument, using query document { < field > : < value > }, < value > Used to match subdocuments. The "equal" match subdocument requires that the subdocument's fields match exactly < value > Conditions, including the order of the fields. The following example queries the value of the producer field to match a subdocument containing only the company field with the value "ABC123" and the subdocument with the value "123 Street", in order:
             
db.inventory.find(
                             {
                                 producer: {
                                       company: 'ABC123',
                                       address: '123 Street' }
                             }
                          )


2. Subdocument field "equal" match:
The field of the subdocument of the specified field in the query collection contains the document of the specified condition. The following example USES the dot notation to query all documents of producer whose conmpany field value is "ABC123" :
             
db.inventory.find( { 'producer.company': 'ABC123' } )

Array:
              when the field value is an array, you can use the array to match exactly or specify the value in the array. If the array element is a subdocument, you can use the dot notation to specify the fields.
Exact matching array:
              specifies an equality condition in the array, using query document { < field > : < value > }, < value > Is the array used for matching. The exact matching of an array requires that the array's fields match exactly as specified < value > , including the order of elements:
             

db.inventory.find( { tags: [ 'fruit', 'food', 'citrus' ] } )

Match 1 array element:
              can specify a single element in an array for an "equal" match. This specification matches an array that must contain at least one specified element. The following example will slice all tags documents that are 1 array and contain "fruit" elements:
               
db.inventory.find( { tags: 'fruit' } )

Matches the specified elements of the array:
              matches documents in an array with the specified index or position equal to the condition. In the following example, a dot-notation is used to match documents where all tags is an array and the first element is "fruit" :
             
db.inventory.find( { 'tags.0' : 'fruit' } )

Subdocument array:

Match subdocument fields using array index:
              if you know the index of the subdocument array, you can specify the location of the subdocument. The following example queries all documents whose memo contains an array and whose first element is a subdocument whose by field value is "shipping" :
             

db.inventory.find( { 'memos.0.by': 'shipping' } )

Matching 1 field without using array index:
If you do not know where the subdocuments are indexed, use the field name of the point-concatenated array and the subdocument field name. The following example queries that all memos fields are an array containing at least one subdocument whose by field value is "shipping" :
             
db.inventory.find( { 'memos.by': 'shipping' } )

Match multiple fields:
              to match multiple fields in a subdocument, you can use the dot symbol or the $elemMatch operator. The following example USES the dot notation to query the memos field value is an array, and the subdocument memo field is equal to "on tiem", and by field is equal to "shipping" :
     
db.inventory.find(
                       { 'memos.memo': 'on time', 'memos.by': 'shipping' }
                 )

            the following example queries the memos field value is an array using the $elemMatch operator, and the subdocument memo field is equal to "on tiem", by field is equal to "shipping" document:
             
db.inventory.find( {
                     memos: {
                                $elemMatch: {
                                              memo : 'on time',
                                              by: 'shipping' }
                            }
                   }
                 )


Related articles: