查询数组

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala

This page provides examples of query operations on array fields using the db.collection.find() method in the mongo shell. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);

You can run the operation in the web shell below:

MongoDB Web Shell

This page provides examples of query operations on array fields using MongoDB Compass. The examples on this page use the inventory collection. Populate the inventory collection with the following documents:

[
  { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
  { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
  { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
  { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
  { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]

For instructions on inserting documents in MongoDB Compass, see Insert Documents.

This page provides examples of query operations on array fields using the pymongo.collection.Collection.find() method in the PyMongo Python driver. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

db.inventory.insert_many([
    {"item": "journal",
     "qty": 25,
     "tags": ["blank", "red"],
     "dim_cm": [14, 21]},
    {"item": "notebook",
     "qty": 50,
     "tags": ["red", "blank"],
     "dim_cm": [14, 21]},
    {"item": "paper",
     "qty": 100,
     "tags": ["red", "blank", "plain"],
     "dim_cm": [14, 21]},
    {"item": "planner",
     "qty": 75,
     "tags": ["blank", "red"],
     "dim_cm": [22.85, 30]},
    {"item": "postcard",
     "qty": 45,
     "tags": ["blue"],
     "dim_cm": [10, 15.25]}])

This page provides examples of query operations on array fields using the com.mongodb.client.MongoCollection.find method in the MongoDB Java Synchronous Driver.

Tip

The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.

The examples on this page use the inventory collection. To populate the inventory collection, run the following:

collection.insertMany(asList(
        Document.parse("{ item: 'journal', qty: 25, tags: ['blank', 'red'], dim_cm: [ 14, 21 ] }"),
        Document.parse("{ item: 'notebook', qty: 50, tags: ['red', 'blank'], dim_cm: [ 14, 21 ] }"),
        Document.parse("{ item: 'paper', qty: 100, tags: ['red', 'blank', 'plain'], dim_cm: [ 14, 21 ] }"),
        Document.parse("{ item: 'planner', qty: 75, tags: ['blank', 'red'], dim_cm: [ 22.85, 30 ] }"),
        Document.parse("{ item: 'postcard', qty: 45, tags: ['blue'], dim_cm: [ 10, 15.25 ] }")
));

This page provides examples of query operations on array fields using the Collection.find() method in the MongoDB Node.js Driver. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

await db.collection('inventory').insertMany([
  {
    item: 'journal',
    qty: 25,
    tags: ['blank', 'red'],
    dim_cm: [14, 21]
  },
  {
    item: 'notebook',
    qty: 50,
    tags: ['red', 'blank'],
    dim_cm: [14, 21]
  },
  {
    item: 'paper',
    qty: 100,
    tags: ['red', 'blank', 'plain'],
    dim_cm: [14, 21]
  },
  {
    item: 'planner',
    qty: 75,
    tags: ['blank', 'red'],
    dim_cm: [22.85, 30]
  },
  {
    item: 'postcard',
    qty: 45,
    tags: ['blue'],
    dim_cm: [10, 15.25]
  }
]);

This page provides examples of query operations on array fields using the MongoDB\Collection::find() method in the MongoDB PHP Library. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

$insertManyResult = $db->inventory->insertMany([
    [
        'item' => 'journal', 
        'qty' => 25,
        'tags' => ['blank', 'red'],
        'dim_cm' => [14, 21],
    ],
    [
        'item' => 'notebook', 
        'qty' => 50,
        'tags' => ['red', 'blank'],
        'dim_cm' => [14, 21],
    ],
    [
        'item' => 'paper', 
        'qty' => 100,
        'tags' => ['red', 'blank', 'plain'],
        'dim_cm' => [14, 21],
    ],
    [
        'item' => 'planner', 
        'qty' => 75,
        'tags' => ['blank', 'red'],
        'dim_cm' => [22.85, 30],
    ],
    [
        'item' => 'postcard', 
        'qty' => 45,
        'tags' => ['blue'],
        'dim_cm' => [10, 15.25],
    ],
]);

This page provides examples of query operations on array fields using the motor.motor_asyncio.AsyncIOMotorCollection.find() method in the Motor driver. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

await db.inventory.insert_many([
    {"item": "journal",
     "qty": 25,
     "tags": ["blank", "red"],
     "dim_cm": [14, 21]},
    {"item": "notebook",
     "qty": 50,
     "tags": ["red", "blank"],
     "dim_cm": [14, 21]},
    {"item": "paper",
     "qty": 100,
     "tags": ["red", "blank", "plain"],
     "dim_cm": [14, 21]},
    {"item": "planner",
     "qty": 75,
     "tags": ["blank", "red"],
     "dim_cm": [22.85, 30]},
    {"item": "postcard",
     "qty": 45,
     "tags": ["blue"],
     "dim_cm": [10, 15.25]}])

This page provides examples of query operations on array fields using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.

The examples on this page use the inventory collection. To populate the inventory collection, run the following:

Publisher<Success> insertManyPublisher = collection.insertMany(asList(
        Document.parse("{ item: 'journal', qty: 25, tags: ['blank', 'red'], dim_cm: [ 14, 21 ] }"),
        Document.parse("{ item: 'notebook', qty: 50, tags: ['red', 'blank'], dim_cm: [ 14, 21 ] }"),
        Document.parse("{ item: 'paper', qty: 100, tags: ['red', 'blank', 'plain'], dim_cm: [ 14, 21 ] }"),
        Document.parse("{ item: 'planner', qty: 75, tags: ['blank', 'red'], dim_cm: [ 22.85, 30 ] }"),
        Document.parse("{ item: 'postcard', qty: 45, tags: ['blue'], dim_cm: [ 10, 15.25 ] }")
));

This page provides examples of query operations on array fields using the MongoCollection.Find() method in the MongoDB C# Driver. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

var documents = new[]
{
    new BsonDocument
    {
        { "item", "journal" },
        { "qty", 25 },
        { "tags", new BsonArray { "blank", "red" } },
        { "dim_cm", new BsonArray { 14, 21 } }
    },
    new BsonDocument
    {
        { "item", "notebook" },
        { "qty", 50 },
        { "tags", new BsonArray { "red", "blank" } },
        { "dim_cm", new BsonArray { 14, 21 } }
    },
    new BsonDocument
    {
        { "item", "paper" },
        { "qty", 100 },
        { "tags", new BsonArray { "red", "blank", "plain" } },
        { "dim_cm", new BsonArray { 14, 21 } }
    },
    new BsonDocument
    {
        { "item", "planner" },
        { "qty", 75 },
        { "tags", new BsonArray { "blank", "red" } },
        { "dim_cm", new BsonArray { 22.85, 30 } }
    },
    new BsonDocument
    {
        { "item", "postcard" },
        { "qty", 45 },
        { "tags", new BsonArray { "blue" } },
        { "dim_cm", new BsonArray { 10, 15.25 } }
    }
};
collection.InsertMany(documents);

This page provides examples of query operations on array fields using the MongoDB::Collection::find() method in the MongoDB Perl Driver. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

$db->coll("inventory")->insert_many(
    [
        {
            item   => "journal",
            qty    => 25,
            tags   => [ "blank", "red" ],
            dim_cm => [ 14, 21 ]
        },
        {
            item   => "notebook",
            qty    => 50,
            tags   => [ "red", "blank" ],
            dim_cm => [ 14, 21 ]
        },
        {
            item   => "paper",
            qty    => 100,
            tags   => [ "red", "blank", "plain" ],
            dim_cm => [ 14, 21 ]
        },
        {
            item   => "planner",
            qty    => 75,
            tags   => [ "blank", "red" ],
            dim_cm => [ 22.85, 30 ]
        },
        {
            item   => "postcard",
            qty    => 45,
            tags   => ["blue"],
            dim_cm => [ 10, 15.25 ]
        }
    ]
);

This page provides examples of query operations on array fields using the Mongo::Collection#find() method in the MongoDB Ruby Driver. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

client[:inventory].insert_many([{ item: 'journal',
                                  qty: 25,
                                  tags: ['blank', 'red'],
                                  dim_cm: [ 14, 21 ] },
                                { item: 'notebook',
                                  qty: 50,
                                  tags: ['red', 'blank'],
                                  dim_cm: [ 14, 21 ] },
                                { item: 'paper',
                                  qty: 100,
                                  tags: ['red', 'blank', 'plain'],
                                  dim_cm: [ 14, 21 ] },
                                { item: 'planner',
                                  qty: 75,
                                  tags: ['blank', 'red'],
                                  dim_cm: [ 22.85, 30 ] },
                                { item: 'postcard',
                                  qty: 45,
                                  tags: ['blue'],
                                  dim_cm: [ 10, 15.25 ] }
                               ])

This page provides examples of query operations on array fields using the collection.find() method in the MongoDB Scala Driver. The examples on this page use the inventory collection. To populate the inventory collection, run the following:

collection.insertMany(Seq(
  Document("""{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] }"""),
  Document("""{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] }"""),
  Document("""{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] }"""),
  Document("""{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] }"""),
  Document("""{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }""")
)).execute()

匹配数组

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala

To specify equality condition on an array, use the query document { <field>: <value> } where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document { <field>: <value> } where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document { <field>: <value> } where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document eq( <field>, <value>) where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document { <field>: <value> } where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document [ <field> => <value> ] where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document { <field>: <value> } where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document eq( <field>, <value>) where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, construct a filter using the Eq method:

Builders<BsonDocument>.Filter.Eq(<field>, <value>)

<value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document { <field> => <value> } where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document { <field> => <value> } where <value> is the exact array to match, including the order of the elements.

To specify equality condition on an array, use the query document equal( <field>, <value> ) where <value> is the exact array to match, including the order of the elements.

下面的示例以指定 Sequences 查询字段tags的值是正好具有两个元素"red""blank"的数组的所有文档:

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala
db.inventory.find( { tags: ["red", "blank"] } )

Copy the following filter into the Compass query bar and click Find:

{ tags: ["red", "blank"] }

cursor = db.inventory.find({"tags": ["red", "blank"]})
FindIterable<Document> findIterable = collection.find(eq("tags", asList("red", "blank")));
const cursor = db.collection('inventory').find({
  tags: ['red', 'blank']
});
$cursor = $db->inventory->find(['tags' => ['red', 'blank']]);
cursor = db.inventory.find({"tags": ["red", "blank"]})
FindPublisher<Document> findPublisher = collection.find(eq("tags", asList("red", "blank")));
var filter = Builders<BsonDocument>.Filter.Eq("tags", new[] { "red", "blank" });
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { tags => [ "red", "blank" ] } );
client[:inventory].find(tags: ['red', 'blank'])
var findObservable = collection.find(equal("tags", Seq("red", "blank")))

相反,如果您希望找到一个同时包含元素"red""blank"的数组,而不考虑该数组中的 Sequences 或其他元素,请使用$all运算符:

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala
db.inventory.find( { tags: { $all: ["red", "blank"] } } )

Copy the following filter into the Compass query bar and click Find:

{ tags: { $all: ["red", "blank"] } }

cursor = db.inventory.find({"tags": {"$all": ["red", "blank"]}})
findIterable = collection.find(all("tags", asList("red", "blank")));
const cursor = db.collection('inventory').find({
  tags: { $all: ['red', 'blank'] }
});
$cursor = $db->inventory->find(['tags' => ['$all' => ['red', 'blank']]]);
cursor = db.inventory.find({"tags": {"$all": ["red", "blank"]}})
findPublisher = collection.find(all("tags", asList("red", "blank")));
var filter = Builders<BsonDocument>.Filter.All("tags", new[] { "red", "blank" });
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { tags => { '$all' => [ "red", "blank" ] } } );
client[:inventory].find(tags: { '$all' => ['red', 'blank'] })
findObservable = collection.find(all("tags", "red", "blank"))

查询数组中的元素

Mongo Shell
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value.

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value.

To query if the array field contains at least one element with the specified value, use the filter eq( <field>, <value>) where <value> is the element value.

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value.

To query if the array field contains at least one element with the specified value, use the filter [ <field> => <value> ] where <value> is the element value.

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value.

To query if the array field contains at least one element with the specified value, use the filter eq( <field>, <value>) where value is the element value.

To query if the array field contains at least one element with the specified value, construct a filter using the Eq method:

Builders<BsonDocument>.Filter.Eq(<field>, <value>)

<value> is the element value to match.

To query if the array field contains at least one element with the specified value, use the filter

{ <field> => <value> } where value is the element value.

To query if the array field contains at least one element with the specified value, use the filter { <field> => <value> } where <value> is the element value.

To query if the array field contains at least one element with the specified value, use the filter equal( <field>, <value> ) where <value> is the element value.

下面的示例查询所有文档,其中tags是包含字符串"red"作为其元素之一的数组:

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala
db.inventory.find( { tags: "red" } )

To specify conditions on the elements in the array field, use query operators in the query filter document:

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

Copy the following filter into the Compass query bar and click Find:

{ tags: "red" }


To specify conditions on the elements in the array field, use query operators in the query filter document:

{ <array field>: { <operator1>: <value1>, ... } }
cursor = db.inventory.find({"tags": "red"})

To specify conditions on the elements in the array field, use query operators in the query filter document:

{ <array field>: { <operator1>: <value1>, ... } }
findIterable = collection.find(eq("tags", "red"));

To specify conditions on the elements in the array field, use query operators in the query filter document. For example:

and(gte(<array field>, <value1>), lt(<array field>, <value2>) ...)
const cursor = db.collection('inventory').find({
  tags: 'red'
});

To specify conditions on the elements in the array field, use query operators in the query filter document:

{ <array field>: { <operator1>: <value1>, ... } }
$cursor = $db->inventory->find(['tags' => 'red']);

To specify conditions on the elements in the array field, use query operators in the query filter document:

[ <array field> => [ <operator1> => <value1>, ... ] ]
cursor = db.inventory.find({"tags": "red"})

To specify conditions on the elements in the array field, use query operators in the query filter document:

{ <array field>: { <operator1>: <value1>, ... } }
findPublisher = collection.find(eq("tags", "red"));

To specify conditions on the elements in the array field, use query operators in the query filter document. For example:

and(gte(<array field>, <value1>), lt(<array field>, <value2>) ...)
var filter = Builders<BsonDocument>.Filter.Eq("tags", "red");
var result = collection.Find(filter).ToList();

To specify conditions on the elements in the array field, use query operators in the query filter document. For example:

var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<array field>, <value1>), builder.Lt(<array field>, <value2>));
$cursor = $db->coll("inventory")->find( { tags => "red" } );

To specify conditions on the elements in the array field, use query operators in the query filter document:

{ <array field> => { <operator1> => <value1>, ... } }
client[:inventory].find(tags: 'red')

To specify conditions on the elements in the array field, use query operators in the query filter document:

{ <array field> => { <operator1> => <value1>, ... } }
findObservable = collection.find(equal("tags", "red"))

To specify conditions on the elements in the array field, use query operators in the query filter document:

and(gte(<array field>, <value1>), lt(<array field>, <value2>) ...)

例如,以下操作查询数组dim_cm包含至少一个值大于25的元素的所有文档。

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala
db.inventory.find( { dim_cm: { $gt: 25 } } )

Copy the following filter into the Compass query bar and click Find:

{ dim_cm: { $gt: 25 } }

cursor = db.inventory.find({"dim_cm": {"$gt": 25}})
findIterable = collection.find(gt("dim_cm", 25));
const cursor = db.collection('inventory').find({
  dim_cm: { $gt: 25 }
});
$cursor = $db->inventory->find(['dim_cm' => ['$gt' => 25]]);
cursor = db.inventory.find({"dim_cm": {"$gt": 25}})
findPublisher = collection.find(gt("dim_cm", 25));
var filter = Builders<BsonDocument>.Filter.Gt("dim_cm", 25);
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { "dim_cm" => { '$gt' => 25 } } );
client[:inventory].find(dim_cm: { '$gt' => 25 })
findObservable = collection.find(gt("dim_cm", 25))

为数组元素指定多个条件

在数组元素上指定复合条件时,可以指定查询,以使单个数组元素满足这些条件,或者数组元素的任何组合均满足条件。

使用数组元素上的复合过滤条件查询数组

以下示例查询其中dim_cm数组包含满足某种查询条件的元素的文档;例如,一个元素可以满足大于15的条件,而另一个元素可以满足小于20的条件,或者单个元素可以满足两个条件:

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

Copy the following filter into the Compass query bar and click Find:

{ dim_cm: { $gt: 15, $lt: 20 } }

cursor = db.inventory.find({"dim_cm": {"$gt": 15, "$lt": 20}})
findIterable = collection.find(and(gt("dim_cm", 15), lt("dim_cm", 20)));
const cursor = db.collection('inventory').find({
  dim_cm: { $gt: 15, $lt: 20 }
});
$cursor = $db->inventory->find([
    'dim_cm' => [
        '$gt' => 15,
        '$lt' => 20,
    ],
]);
cursor = db.inventory.find({"dim_cm": {"$gt": 15, "$lt": 20}})
findPublisher = collection.find(and(gt("dim_cm", 15), lt("dim_cm", 20)));
var builder = Builders<BsonDocument>.Filter;
var filter = builder.And(builder.Gt("dim_cm", 15), builder.Lt("dim_cm", 20));
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find(
    { "dim_cm" => { '$gt' => 15, '$lt' => 20 } }
);
client[:inventory].find(dim_cm: { '$gt' => 15,
                                  '$lt' => 20 })
findObservable = collection.find(and(gt("dim_cm", 15), lt("dim_cm", 20)))

查询满足多个条件的数组元素

使用$elemMatch运算符可以在数组的元素上指定多个条件,以使至少一个数组元素满足所有指定的条件。

以下示例查询dim_cm数组包含至少一个既大于($gt)22又小于($lt)30的元素的文档:

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )

Copy the following filter into the Compass query bar and click Find:

{ dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } }

cursor = db.inventory.find(
    {"dim_cm": {"$elemMatch": {"$gt": 22, "$lt": 30}}})
findIterable = collection.find(elemMatch("dim_cm", Document.parse("{ $gt: 22, $lt: 30 }")));
const cursor = db.collection('inventory').find({
  dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } }
});
$cursor = $db->inventory->find([
    'dim_cm' => [
        '$elemMatch' => [
            '$gt' => 22,
            '$lt' => 30,
        ],
    ],
]);
cursor = db.inventory.find(
    {"dim_cm": {"$elemMatch": {"$gt": 22, "$lt": 30}}})
findPublisher = collection.find(elemMatch("dim_cm", Document.parse("{ $gt: 22, $lt: 30 }")));
var filter = Builders<BsonDocument>.Filter.ElemMatch<BsonValue>("dim_cm", new BsonDocument { { "$gt", 22 }, { "$lt", 30 } });
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find(
    { dim_cm => { '$elemMatch' => { '$gt' => 22, '$lt' => 30 } } }
);
client[:inventory].find(dim_cm: { '$elemMatch' => { '$gt' => 22,
                                                    '$lt' => 30 } })
findObservable = collection.find(elemMatch("dim_cm", Document("$gt" -> 22, "$lt" -> 30)))

通过数组索引位置查询元素

使用dot notation,可以为数组的特定索引或位置处的元素指定查询条件。该数组使用基于零的索引。

Note

使用点符号查询时,该字段和嵌套字段必须在引号内。

以下示例查询数组dim_cm中第二个元素大于25的所有文档:

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )

Copy the following filter into the Compass query bar and click Find:

{ "dim_cm.1": { $gt: 25 } }

cursor = db.inventory.find({"dim_cm.1": {"$gt": 25}})
findIterable = collection.find(gt("dim_cm.1", 25));
const cursor = db.collection('inventory').find({
  'dim_cm.1': { $gt: 25 }
});
$cursor = $db->inventory->find(['dim_cm.1' => ['$gt' => 25]]);
cursor = db.inventory.find({"dim_cm.1": {"$gt": 25}})
findPublisher = collection.find(elemMatch("dim_cm", Document.parse("{ $gt: 22, $lt: 30 }")));
var filter = Builders<BsonDocument>.Filter.Gt("dim_cm.1", 25);
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { "dim_cm.1" => { '$gt' => 25 } } );
client[:inventory].find('dim_cm.1' => { '$gt' => 25 })
findObservable = collection.find(gt("dim_cm.1", 25))

按数组长度查询数组

使用$size运算符可按元素数查询数组。例如,以下选择数组tags具有 3 个元素的文档。

Mongo Shell
Compass
Python
Java (Sync)
Node.js
PHP
Motor
Java (Async)
C#
Perl
Ruby
Scala
db.inventory.find( { "tags": { $size: 3 } } )

Copy the following filter into the Compass query bar and click Find:

{ "tags": { $size: 3 } }

cursor = db.inventory.find({"tags": {"$size": 3}})
findIterable = collection.find(size("tags", 3));
const cursor = db.collection('inventory').find({
  tags: { $size: 3 }
});
$cursor = $db->inventory->find(['tags' => ['$size' => 3]]);
cursor = db.inventory.find({"tags": {"$size": 3}})
findPublisher = collection.find(size("tags", 3));
var filter = Builders<BsonDocument>.Filter.Size("tags", 3);
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { tags => { '$size' => 3 } } );
client[:inventory].find(tags: { '$size' => 3 })
findObservable = collection.find(size("tags", 3))

其他查询教程

有关其他查询示例,请参见:

首页