Hướng dẫn query like in mongodb

Docs HomeMongoDB Manual

$in

The $in operator selects the documents where the value of a field equals any value in the specified array. To specify an $in expression, use the following prototype:

{ field: { $in: [, , ... ] } }

For comparison of different BSON type values, see the specified BSON comparison order.

If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array [for example, , , and so on].

The $in operator compares each parameter to each document in the collection, which can lead to performance issues. To improve performance:

  • It is recommended that you limit the number of parameters passed to the $in operator to tens of values. Using hundreds of parameters or more can negatively impact query performance.

  • Create an index on the field you want to query.

Note

This document describes the $in query operator. For the $in aggregation operator, see $in [aggregation].

Create the inventory collection:

db.inventory.insertMany[ [
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
{ "item": "Maps", "tags": [ "office", "storage" ] },
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
] ]

Consider the following example:

db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]

This query selects all documents in the inventory collection where the value of the quantity field is either 5 or 15.

{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }

Although you can write this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.

The following updateMany[] operation sets the exclude field to false when the tags array has at least one element that matches either "home" or "school".

db.inventory.updateMany[
{ tags: { $in: [ "home", "school" ] } },
{ $set: { exclude: false } }
]

Example output:

{
item: 'Pens',
quantity: 350,
tags: [ 'school', 'office' ],
exclude: false
},
{
item: 'Erasers',
quantity: 15,
tags: [ 'school', 'home' ],
exclude: false
},
{
item: 'Maps',
tags: [ 'office', 'storage' ]
},
{
item: 'Books',
quantity: 5,
tags: [ 'school', 'storage', 'home' ],
exclude: false
}

For additional examples in querying arrays, see:

  • Query an Array

  • Query an Array of Embedded Documents

For additional examples in querying, see:

  • Query Documents

The $in operator can specify matching values using regular expressions of the form /pattern/. You cannot use $regex operator expressions inside an $in.

Consider the following example:

db.inventory.find[ { tags: { $in: [ /^be/, /^st/ ] } } ]

This query selects all documents in the inventory collection where the tags field holds either a string that starts with be or st or an array with at least one element that starts with be or st.

Tip

Chủ Đề