In the previous mongodb remove operation, the commonly used field update operators are introduced in detail. The most commonly used in the work is all kinds of complex queries in mongodb. In the relational database, you can achieve a variety of queries based on tables and return specified columns by projection. The corresponding query function can also be implemented in MongoDB. At the same time, because MongoDB supports nested documents and arrays, MongoDB can also implement queries based on nested documents and arrays. Next, we introduce the rich queries in MongoDB in detail.

1. find

Description:

Select documents in the collection and return to the selected documents according to the query criteria.

Grammar:

db.collection.find(

.

)

Remark:

{field1:, field2:… }

Parameter explanation;

Query: Optional. Use the query operator to specify a selection filter, which is equivalent to reading the document to filter the document. To return all documents in the collection, omit this parameter or pass an empty document ({}).

Projection: Optional. Specifying the fields to be returned in the document that match the query filter is equivalent to specifying which fields to return. To return all fields in a matching document, omit this parameter. For more information, see Projection.

1.1. Common operators

1.1.1. Comparison query operators

$eq: matches a value equal to the specified value.

$ne: Matches all values that are not equal to the specified value.

$IN: Matches any value specified in the array.

$nin: does not match the value specified in the array.

$gt: Matches a value greater than the specified value.

$gte: Matches values greater than or equal to the specified value.

$lt: Matches values less than the specified value.

$LTE: Matches values less than or equal to the specified value.

1.1.2. Logical query operators

$and: concatenates the query clause with the logic and returns all documents that match the two clause conditions, equivalent to matching all documents with multiple filter conditions.

$NOR: Query clause with logical NOR join, return all documents that do not match the two clauses, equivalent to return multiple filter criteria are not true documents.

$OR: Query a clause with a logical join, or return all documents that match any clause condition, equivalent to returning documents with at least one filter condition

$NOT: reverses the effect of the query expression and returns documents that do not match the query expression.

1.1.3. Element query operators

$EXISTS: Matches a document with a specified field.

$type: Selects the document if the field is of the specified type.

1.1.4. Evaluate the query operator

$expr: Allows the use of aggregate expressions in the query language.

$regex: Selects documents whose values match the specified regular expression.

1.1.5. Array query operators

$all: Matches an array containing all the elements specified in the query, equivalent to returning the document containing all the query values in the array fields.

Field match: if the elements in the array field match all of the specified elemMatch: If the elements in the array field match all specified elemMatch criteria, the document is selected, which is equivalent to matching at least one document in the array field whose value meets the filtering criteria

$SIZE: Select the document if the array field is the specified size.

1.2. The sample

Example:

Initialize data:

db.findExample.insertMany([

{ item: “journal”, qty: 25, size: { h: 14, w: 21, uom: “cm” }, status: “A”,tags: [“blank”, “red”], dim_cm: [ 14, 21 ],instock: [ { warehouse: “A”, qty: 5 }, { warehouse: “C”, qty: 15 } ] },

{item: “the notebook”, qty: 50, size: {h: 8.5, w: 11, uom: “in”}, status: “A”, tags: [” red “, “blank”], dim_cm: [ 14, 21 ] , instock: [ { warehouse: “C”, qty: 5 } ] },

{item: “paper”, qty: 100, size: {h: 8.5, w: 11, uom: “in”}, status: “D”, tags: [” red “, “blank”, “plain”], dim_cm: [ 14, 21 ], instock: [ { warehouse: “A”, qty: 60 }, { warehouse: “B”, qty: 15 } ]},

{item: “planner”, qty: 75, size: {h: 22.85, w: 30, uom: “cm”}, status: “D”, tags: [” blank “, “red”], dim_cm: [22.85, 30], instock: [{warehouse: “A”, qty: 40}, {warehouse: “B”, qty: 5}]}

{item: “Postcard “, QTY: 45, size: {H: 10, W: 15.25, UOM:” CM “}, status: “A”, tags: [“blue”], DIM_CM: [10, 15.25], instock: [{warehouse: “B”, qty: 15}, {warehouse: “C”, qty: 35}]}

]);

1.2.1. Querying a single object

1.2.1.1. Query all

db.findExample.find({})

1.2.1.2. Query by field equality

db.findExample.find({status:”D”})

1.2.1.3. Query according to a field in

db.findExample.find({ status: { $in: [ “A”, “D” ] } } )

1.2.1.4. Query based on multiple fields

Note that in MongoDB queries, two adjacent conditions are automatically translated into AND without other qualifiers such as $or, equivalent to the implicit AND operation.

db.findExample.find( { status: “A”, qty: { $lt: 30 } } )

1.2.1.5. Query explicit AND operations based on multiple fields

db.findExample.find( { and: \[ { status: “A” }, { qty: { lt: 30 } } ] } )

1.2.1.6. Query according to multiple fields or

db.findExample.find( { or: \[ { status: “A” }, { qty: { lt: 30 } } ] } )

1.2.1.7. Implicit AND + OR query

db.findExample.find( {

status: “A”,

or: \[ { qty: { lt: 30 } }, { item: /^p/ } ]

})

1.2.2. Query (single) embedded documents

1.2.2.1. Complete matching of embedded documents

[Note: The order of field names matching embedded documents cannot be reversed]

db.findExample.find( { size: { h: 14, w: 21, uom: “cm” } })

1.2.2.2. Using equality queries on embedded document fields

db.findExample.find( { “size.uom”: “in” } )

1.2.3. Query (multiple) embedded documents

1.2.3.1. Embedded documents match completely

【 Note: As long as one of the multiple embedded documents matches the above query conditions, the document will be returned. The order of field names should not be out of order, and the elements should not be too few. 】

db.findExample.find( { “instock”: { warehouse: “A”, qty: 5 } } )

1.2.3.2. Locate elements according to object coordinates for query

db.findExample.find( { ‘instock.0.qty’: { $lte: 10 } } )

1.2.3.3. Query in OR mode according to multiple filtering criteria

db.findExample.find( { “instock.qty”: 5, “instock.warehouse”: “A” } )

1.2.3.4. Query in AND mode according to multiple filtering criteria

db.findExample.find( { “instock”: { $elemMatch: { qty: 5, warehouse: “A” } } } )

1.2.4. Query arrays

1.2.4.1. Array exact match query

db.findExample.find( { tags: [“red”, “blank”] } )

1.2.4.2. Partial matching (including relation, equivalent to in, independent of order)

db.findExample.find( { tags: { $all: [“red”, “blank”] } } )

1.2.4.3. The query array contains an element

db.findExample.find( { tags: “plain” } )

1.2.4.4. Use of single operands in arrays

Gt greater than gt greater than GTE greater than or equal to lt less than lt less than lt less than lt less than lt less than lte less than or equal to

db.findExample.find( { dim_cm: { $gt: 25 } } )

1.2.4.5. Multiple operands in arrays

Compound filter conditions actually perform a relational query of OR, as long as one of the elements in the queue meets one of the conditions, the document is considered to match.

db.findExample.find( { dim_cm: {
g t : 15 , gt: 15,
lt: 20 } }

1.2.4.6. Locate elements according to array coordinates for query

db.findExample.find( { “dim_cm.1”: { $gt: 25 } } )

1.2.4.7. Matching query by queue length

db.findExample.find( { “tags”: { $size: 3 } } )

1.2.5. Qualify return fields

[Note: default output field _id]

db.findExample.find( { status: “A” }, { item: 1, status: 1 } )

[Note: field _id is not output]

db.findExample.find( { status: “A” }, { item: 1, status: 1, _id: 0 } )

Specify a set of fields not to be returned [This query will return all result fields except status and Instock;

db.findExample.find( { status: “A” }, { status: 0, instock: 0 } )

Qualify the set of return fields for the embedded document

Returns the specified set of fields in the embedded document

db.findExample.find( { status: “A” }, { item: 1, status: 1, “instock.qty”: 1 } )

The set of returned fields is qualified by the qualified operator

In the case of $slice, the following example returns only the last element in the instock embedded document collection;

db.findExample.find( { status: “A” }, { name: 1, status: 1, instock: { $slice: -1} } )

1.2.6. Query for null or missing fields

Initialize test data

db.findNullExample.insertMany([

{ _id: 1, item: null },

{ _id: 2 }

])

If you use {item: null} to match documents, you will match either of the following conditions: 1) Contains the item field and the item value is null; 2) contains no item field.

db.findNullExample.find( { item: null } )

{item: {Type: 10}}, where {Type: 10} represents the BSON Type Null. A query using BSON Type Null will only match a document containing the item field and its value is Null.

db.findNullExample.find( { item : { $type: 10 } } )

Whether the matching field exists

db.findNullExample.find( { item : { $exists: false } } )

2. findOne

Description:

Returns documents that meet the specified query criteria on a collection or view. If multiple documents satisfy the query, the method returns the first document according to the natural order, which reflects the order of documents on disk. In capped collections, the natural order is the same as the insert order. If no document satisfies the query, the method returns NULL.

The syntax and usage of find are the same

Next, compare SQL and MongoDB