MongoDB Document Translation-CRUD Operations-Query Documents

Keywords: less MongoDB network

English original address: https://docs.mongodb.com/v3.2/tutorial/query-documents/.
It is a coincidence that this article is a personal translation for personal learning. If there are any mistakes, please correct them.

consult your documentation

Query method

MongoDB provides the db.collection.find() method to read documents from collections. The db.collection.find() method returns the cursor of the matched document.

db.collection.find(<query filter>, <projection>)

You can specify the following optional fields:

  • Query filter to specify the returned document
  • Projection, specifying which fields are returned from the matched document. Projection limits the amount of data MongoDB returns to the client over the network.

You can optionally add a cursor modifier to enforce limits on returns, skips, and sort. Unless you specify sort(), the order of returning documents is uncertain.

Example set

This example uses the db.collection.find() method in the mongoshell. In mongoshell, if the returned cursor is not assigned to the variable with the var keyword, the cursor automatically iterates to 20 times, printing the first 20 documents of the result.

To populate the users collection mentioned in this example, run the code in the mongoshell:

Be careful

IfusersThe collection already contains the same_id A collection that needs to be deleted before inserting a document(db.users.drop())

db.users.insertMany(
  [
     {
       _id: 1,
       name: "sue",
       age: 19,
       type: 1,
       status: "P",
       favorites: { artist: "Picasso", food: "pizza" },
       finished: [ 17, 3 ],
       badges: [ "blue", "black" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 85, bonus: 10 }
       ]
     },
     {
       _id: 2,
       name: "bob",
       age: 42,
       type: 1,
       status: "A",
       favorites: { artist: "Miro", food: "meringue" },
       finished: [ 11, 25 ],
       badges: [ "green" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 64, bonus: 12 }
       ]
     },
     {
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites: { artist: "Cassatt", food: "cake" },
       finished: [ 6 ],
       badges: [ "blue", "red" ],
       points: [
          { points: 81, bonus: 8 },
          { points: 55, bonus: 20 }
       ]
     },
     {
       _id: 4,
       name: "xi",
       age: 34,
       type: 2,
       status: "D",
       favorites: { artist: "Chagall", food: "chocolate" },
       finished: [ 5, 11 ],
       badges: [ "red", "black" ],
       points: [
          { points: 53, bonus: 15 },
          { points: 51, bonus: 15 }
       ]
     },
     {
       _id: 5,
       name: "xyz",
       age: 23,
       type: 2,
       status: "D",
       favorites: { artist: "Noguchi", food: "nougat" },
       finished: [ 14, 6 ],
       badges: [ "orange" ],
       points: [
          { points: 71, bonus: 20 }
       ]
     },
     {
       _id: 6,
       name: "abc",
       age: 43,
       type: 1,
       status: "A",
       favorites: { food: "pizza", artist: "Picasso" },
       finished: [ 18, 12 ],
       badges: [ "black", "blue" ],
       points: [
          { points: 78, bonus: 8 },
          { points: 57, bonus: 7 }
       ]
     }
  ]
)

Query all documents in a collection

An empty query filter document ({}) selects all documents in the collection:

db.users.find( {} )

Omitting query filtering documents in db.collection.find() is equivalent to specifying an empty query document. In this way, the following operations are equivalent to the previous ones:

db.users.find()

Specify query filtering conditions

Assignment equals condition

Query filtered documents can be specified with < filed >: < value > expressions as conditions, and all documents containing fields can be selected with values equal to < value >.

{ <field1>: <value1>, ...}

The following example returns all documents with a status value of "A" from the users collection:

db.users.find( {status: "A"} )

Specify conditions with query operators

Query filtering documents can be specified with query operators in the following format:

{ <field>: { <operator1>: <value1>}, ...}

The following example returns a document whose status field is equal to "P" or "D" from the users collection.

db.users.find( {status: {$in: ["P", "D"]}} )

Although you can use the $or operator to express this query, when performing equality checks on the same field, use $in instead of $or.

For a complete list of query operators, refer to the query and projection operators.

Specify AND conditions

A composite query can specify query conditions in multiple fields of a document. The logical operator AND connects all parts of a composite query so that the query can select documents that satisfy all conditions in the collection.

The following example returns all documents with status equal to "A"** and ** age less than 30 from the users collection:

db.users.find( {status: "A", age: {$lt: 30}} )

Specify OR conditions

With the $or operator, you can use logical OR to connect the various parts of a composite query so that the query can select documents in the collection that satisfy at least one of the criteria.

The following example returns all documents with status equal to "A"** or ** age less than 30 from the users collection:

db.users.find(
  [
    $or: [{status: "A"}, {age: {$lt: 30}}]
  ]
)

Specify both AND and OR conditions

With additional clauses, you can specify exact conditions for matching documents.

In the following example, a composite query document selects a record whose status is equal to "A" from the collection and whose age is less than 30 or whose type is equal to 1.

db.users.find(
  {
    status: "A",
    $or: [{age: {$lt: 30}}, {type: 1}]
  }
)

Query in Embedded Documents

When a field contains an embedded document, the query can specify an exact match on the embedded document, or it can specify a match on the embedded document through a separate field using point marking.

Accurate Matching Embedded Documents

To specify an exact match in the entire embedded document, use {field >: < value >}, where < value > is the matching document. To match equally in embedded documents, the specified < value > must match documents exactly, including field order.

In the following example, the query matches all documents: the favorites field is an embedded document containing only artist fields, whose values are equal to "Picasso" and "food fields, whose values are equal to"pizza", in the order of artist and food:

db.users.find( {favorites: {artists: "Picasso", food: "pizza"}} )

Equal Matching Embedded Document Fields

Point marking can be used to match the specified fields embedded in the document. Equivalence matching of specified fields in embedded documents selects embedded documents that contain specified fields with specified values, and embedded documents can contain other fields.

In the following example, the query uses point marking to match the favorites field as an artist field with a value equal to "Picasso" and possibly all documents embedded in the document with other fields.

db.users.find( {"favorites.artist": "Picasso"} )

Queries on arrays

When the value of a field is an array, you can specify a match for the entire array, or the specified value in the array. If the array contains embedded documents, you can use dot markup to query the specified fields in the embedded documents.

If you specify multiple conditions using the $elemMatch operator, the array must contain at least one element that meets all conditions. See that a single element satisfies the condition.

If you do not specify multiple conditions with $elemMatch, a combination of array elements must satisfy all conditions, and a single element does not necessarily need to satisfy, that is, different array elements can satisfy different parts of the conditions. See that the combination of elements satisfies the conditions.

Accurately matching the entire array

To specify an equal match for an array, use the query document {field >: < value >}, where < value > is the array to match. Accurate matching of an array needs to be satisfied: the array must match the specified <value> exactly, including the element order.

The following example queries the badges field as a document containing an array of "blue" and "black" elements in sequence:

db.users.find( {badges: ["blue", "black"]} )

The query returns the following documents:

{
   "_id" : 1,
   "name" : "sue",
   "age" : 19,
   "type" : 1,
   "status" : "P",
   "favorites" : { "artist" : "Picasso", "food" : "pizza" },
   "finished" : [ 17, 3 ]
   "badges" : [ "blue", "black" ],
   "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ]
}

An element of a matching array

Equality matching can also specify a single element in the matching array. As long as the value of at least one element in the array equals the specified value.

The following example queries the badges field as a document containing an array of "black" elements.

db.users.find( { badges: "black" } )

The query returns the following documents:

{
   "_id" : 1,
   "name" : "sue",
   "age" : 19,
   "type" : 1,
   "status" : "P",
   "favorites" : { "artist" : "Picasso", "food" : "pizza" },
   "finished" : [ 17, 3 ]
   "badges" : [ "blue", "black" ],
   "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ]
}
{
   "_id" : 4,
   "name" : "xi",
   "age" : 34,
   "type" : 2,
   "status" : "D",
   "favorites" : { "artist" : "Chagall", "food" : "chocolate" },
   "finished" : [ 5, 11 ],
   "badges" : [ "red", "black" ],
   "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ]
}
{
   "_id" : 6,
   "name" : "abc",
   "age" : 43,
   "type" : 1,
   "status" : "A",
   "favorites" : { "food" : "pizza", "artist" : "Picasso" },
   "finished" : [ 18, 12 ],
   "badges" : [ "black", "blue" ],
   "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ]
}

Matches the specified elements in the array

Equality matching can also specify elements with specific subscripts or locations in matching arrays by point marking.

In the following example, the query matches a document whose badges field is an array of "black" as the first element using point marking.

db.users.find( { "badges.0": "black" } )

This operation returns the following results:

{
   "_id" : 6,
   "name" : "abc",
   "age" : 43,
   "type" : 1,
   "status" : "A",
   "favorites" : { "food" : "pizza", "artist" : "Picasso" },
   "finished" : [ 18, 12 ],
   "badges" : [ "black", "blue" ],
   "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ]
}

Specify multiple conditions for array elements

A single element satisfies a condition

Use the $elemMatch operator to specify multiple conditions on an array element, and at least one array element satisfies all conditions.

The following example query field finished is a document with at least one element greater than 15 and less than 20.

db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )

This operation returns the following document, whose finished field is an array, and at least one element satisfies the condition.

{
   "_id" : 1,
   "name" : "sue",
   "age" : 19,
   "type" : 1,
   "status" : "P",
   "favorites" : { "artist" : "Picasso", "food" : "pizza" },
   "finished" : [ 17, 3 ]
   "badges" : [ "blue", "black" ],
   "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ]
}
{
   "_id" : 6,
   "name" : "abc",
   "age" : 43,
   "type" : 1,
   "status" : "A",
   "favorites" : { "food" : "pizza", "artist" : "Picasso" },
   "finished" : [ 18, 12 ],
   "badges" : [ "black", "blue" ],
   "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ]
}

The combination of array elements satisfies the condition

The following example queries such a document: its finished is an array, and the combination of elements contained in the array satisfies the query criteria. That is, one element satisfies the condition of greater than 15, while another element satisfies the condition of less than 20, or a single element satisfies both conditions.

db.users.find( { finished: { $gt: 15, $lt: 20 } } )

This operation returns the following results:

{
   "_id" : 1,
   "name" : "sue",
   "age" : 19,
   "type" : 1,
   "status" : "P",
   "favorites" : { "artist" : "Picasso", "food" : "pizza" },
   "finished" : [ 17, 3 ]
   "badges" : [ "blue", "black" ],
   "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ]
}
{
   "_id" : 2,
   "name" : "bob",
   "age" : 42,
   "type" : 1,
   "status" : "A",
   "favorites" : { "artist" : "Miro", "food" : "meringue" },
   "finished" : [ 11, 20 ],
   "badges" : [ "green" ],
   "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ]
}
{
   "_id" : 6,
   "name" : "abc",
   "age" : 43,
   "type" : 1,
   "status" : "A",
   "favorites" : { "food" : "pizza", "artist" : "Picasso" },
   "finished" : [ 18, 12 ],
   "badges" : [ "black", "blue" ],
   "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ]
}

An array of embedded documents

Matching Fields Embedded in Documents with Array Subscripts

If you know the subscript of the embedded document in the array, you can use dot marking to indicate the location of the embedded document.

The following example chooses a document whose points field is an array, and the first element of the array is a document, which contains points field whose value is less than or equal to 55.

db.users.find( { 'points.0.points': { $lte: 55 } } )

This operation returns the following documents:

{
   "_id" : 4,
   "name" : "xi",
   "age" : 34,
   "type" : 2,
   "status" : "D",
   "favorites" : { "artist" : "Chagall", "food" : "chocolate" },
   "finished" : [ 5, 11 ],
   "badges" : [ "red", "black" ],
   "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ]
}

Does not specify array subscripts to match fields

If you don't know the subscript of the document in the array, you can use "." to link the field name of the array with the field name embedded in the document.

The following example queries a document whose points are an array in which at least one embedded document contains a points field whose value is less than or equal to 55:

db.users.find( { 'points.points': { $lte: 55 } } )

This operation returns the following results:

{
   "_id" : 3,
   "name" : "ahn",
   "age" : 22,
   "type" : 2,
   "status" : "A",
   "favorites" : { "artist" : "Cassatt", "food" : "cake" },
   "finished" : [ 6 ],
   "badges" : [ "blue", "red" ],
   "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ]
}
{
   "_id" : 4,
   "name" : "xi",
   "age" : 34,
   "type" : 2,
   "status" : "D",
   "favorites" : { "artist" : "Chagall", "food" : "chocolate" },
   "finished" : [ 5, 11 ],
   "badges" : [ "red", "black" ],
   "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ]
}

Specify multiple conditions for the document array

A single element satisfies a condition

Using the $elemMatch operator, multiple conditions are specified on an array of embedded documents, and at least one document in the array is filtered to satisfy all specified conditions.

The following example queries a document whose points field is an array in which at least one embedded document contains points fields with values less than or equal to 70 and bonus fields with values equal to 20.

db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } )

This operation returns the following documents:

{
   "_id" : 3,
   "name" : "ahn",
   "age" : 22,
   "type" : 2,
   "status" : "A",
   "favorites" : { "artist" : "Cassatt", "food" : "cake" },
   "finished" : [ 6 ],
   "badges" : [ "blue", "red" ],
   "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ]
}

The combination of embedded document elements satisfies the conditions

The following example queries such a document: its points field is an array, and a combination of array elements satisfies the query conditions, that is, the points field of one element is less than or equal to 70, the bonus field of another element is equal to 20, or a single element satisfies both conditions.

db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } )

The query returns the following documents:

{
   "_id" : 2,
   "name" : "bob",
   "age" : 42,
   "type" : 1,
   "status" : "A",
   "favorites" : { "artist" : "Miro", "food" : "meringue" },
   "finished" : [ 11, 20 ],
   "badges" : [ "green" ],
   "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ]
}
{
   "_id" : 3,
   "name" : "ahn",
   "age" : 22,
   "type" : 2,
   "status" : "A",
   "favorites" : { "artist" : "Cassatt", "food" : "cake" },
   "finished" : [ 6 ],
   "badges" : [ "blue", "red" ],
   "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ]
}

Posted by kpasiva on Fri, 05 Apr 2019 22:36:30 -0700