Query Documents
This page provides examples of query operations 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, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
You can run the operation in the web shell below:
This page provides examples of query operations using MongoDB Compass. The examples on this page use the inventory
collection. Populate the inventory
collection with the following documents:
[
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
This page provides examples of query operations 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,
"size": {"h": 14, "w": 21, "uom": "cm"},
"status": "A"},
{"item": "notebook",
"qty": 50,
"size": {"h": 8.5, "w": 11, "uom": "in"},
"status": "A"},
{"item": "paper",
"qty": 100,
"size": {"h": 8.5, "w": 11, "uom": "in"},
"status": "D"},
{"item": "planner",
"qty": 75, "size": {"h": 22.85, "w": 30, "uom": "cm"},
"status": "D"},
{"item": "postcard",
"qty": 45,
"size": {"h": 10, "w": 15.25, "uom": "cm"},
"status": "A"}])
This page provides examples of query operations 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, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"),
Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }")
));
This page provides examples of query operations 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,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
},
{
item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'A'
},
{
item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D'
},
{
item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D'
},
{
item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A'
}
]);
This page provides examples of query operations 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,
'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
'status' => 'A',
],
[
'item' => 'notebook',
'qty' => 50,
'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
'status' => 'A',
],
[
'item' => 'paper',
'qty' => 100,
'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
'status' => 'D',
],
[
'item' => 'planner',
'qty' => 75,
'size' => ['h' => 22.85, 'w' => 30, 'uom' => 'cm'],
'status' => 'D',
],
[
'item' => 'postcard',
'qty' => 45,
'size' => ['h' => 10, 'w' => 15.25, 'uom' => 'cm'],
'status' => 'A',
],
]);
This page provides examples of query operations 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,
"size": {"h": 14, "w": 21, "uom": "cm"},
"status": "A"},
{"item": "notebook",
"qty": 50,
"size": {"h": 8.5, "w": 11, "uom": "in"},
"status": "A"},
{"item": "paper",
"qty": 100,
"size": {"h": 8.5, "w": 11, "uom": "in"},
"status": "D"},
{"item": "planner",
"qty": 75, "size": {"h": 22.85, "w": 30, "uom": "cm"},
"status": "D"},
{"item": "postcard",
"qty": 45,
"size": {"h": 10, "w": 15.25, "uom": "cm"},
"status": "A"}])
This page provides examples of query operations 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, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"),
Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"),
Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"),
Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"),
Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }")
));
This page provides examples of query operations 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 BsonDocument[]
{
new BsonDocument
{
{ "item", "journal" },
{ "qty", 25 },
{ "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm"} } },
{ "status", "A" }
},
new BsonDocument
{
{ "item", "notebook" },
{ "qty", 50 },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in"} } },
{ "status", "A" }
},
new BsonDocument
{
{ "item", "paper" },
{ "qty", 100 },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in"} } },
{ "status", "D" }
},
new BsonDocument
{
{ "item", "planner" },
{ "qty", 75 },
{ "size", new BsonDocument { { "h", 22.85 }, { "w", 30 }, { "uom", "cm"} } },
{ "status", "D" }
},
new BsonDocument
{
{ "item", "postcard" },
{ "qty", 45 },
{ "size", new BsonDocument { { "h", 10 }, { "w", 15.25 }, { "uom", "cm"} } },
{ "status", "A" }
},
};
collection.InsertMany(documents);
This page provides examples of query operations 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,
size => { h => 14, w => 21, uom => "cm" },
status => "A"
},
{
item => "notebook",
qty => 50,
size => { h => 8.5, w => 11, uom => "in" },
status => "A"
},
{
item => "paper",
qty => 100,
size => { h => 8.5, w => 11, uom => "in" },
status => "D"
},
{
item => "planner",
qty => 75,
size => { h => 22.85, w => 30, uom => "cm" },
status => "D"
},
{
item => "postcard",
qty => 45,
size => { h => 10, w => 15.25, uom => "cm" },
status => "A"
}
]
);
This page provides examples of query operations 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,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A' },
{ item: 'notebook',
qty: 50,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'A' },
{ item: 'paper',
qty: 100,
size: { h: 8.5, w: 11, uom: 'in' },
status: 'D' },
{ item: 'planner',
qty: 75,
size: { h: 22.85, w: 30, uom: 'cm' },
status: 'D' },
{ item: 'postcard',
qty: 45,
size: { h: 10, w: 15.25, uom: 'cm' },
status: 'A' }
])
This page provides examples of query operations 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, size: { h: 14, w: 21, uom: "cm" }, status: "A" }"""),
Document("""{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" }"""),
Document("""{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }"""),
Document("""{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }"""),
Document("""{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }""")
)).execute()
选择集合中的所有文档
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
db.inventory.find( {} )
To select all documents in the collection, pass an empty document as the query filter parameter to the query bar. The query filter parameter determines the select criteria:
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
cursor = db.inventory.find({})
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
FindIterable<Document> findIterable = collection.find(new Document());
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
const cursor = db.collection('inventory').find({});
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
$cursor = $db->inventory->find([]);
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
cursor = db.inventory.find({})
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
FindPublisher<Document> findPublisher = collection.find(new Document());
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
var filter = Builders<BsonDocument>.Filter.Empty;
var result = collection.Find(filter).ToList();
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
$cursor = $db->coll("inventory")->find( {} );
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
client[:inventory].find({})
To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:
var findObservable = collection.find(Document())
此操作对应于以下 SQL 语句:
SELECT * FROM inventory
For more information on the syntax of the method, see find().
For more information on the MongoDB Compass query bar, see Query Bar.
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see com.mongodb.client.MongoCollection.find.
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see com.mongodb.reactivestreams.client.MongoCollection.find.
For more information on the syntax of the method, see Find().
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see find().
For more information on the syntax of the method, see collection.find().
指定平等条件
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
[ <field1> => <value1>, ... ]
To specify equality conditions, use <field>:<value>
expressions in the query filter document:
{ <field1>: <value1>, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq method to create the query filter document:
and(eq( <field1>, <value1>), eq( <field2>, <value2>) ...)
To specify equality conditions, construct a filter using the Eq method:
Builders<BsonDocument>.Filter.Eq(<field>, <value>);
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
To specify equality conditions, use <field> => <value>
expressions in the query filter document:
{ <field1> => <value1>, ... }
To specify equality conditions, use the com.mongodb.client.model.Filters.eq_
method to create the query filter document:
and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)
以下示例从inventory
集合中选择status
等于"D"
的所有文档:
db.inventory.find( { status: "D" } )
Copy the following filter into the Compass query bar and click Find:
{ status: "D" }
cursor = db.inventory.find({"status": "D"})
findIterable = collection.find(eq("status", "D"));
const cursor = db.collection('inventory').find({ status: 'D' });
$cursor = $db->inventory->find(['status' => 'D']);
cursor = db.inventory.find({"status": "D"})
findPublisher = collection.find(eq("status", "D"));
var filter = Builders<BsonDocument>.Filter.Eq("status", "D");
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { status => "D" } );
client[:inventory].find(status: 'D')
findObservable = collection.find(equal("status", "D"))
此操作对应于以下 SQL 语句:
SELECT * FROM inventory WHERE status = "D"
Note
The MongoDB Compass query bar autocompletes the current query based on the keys in your collection's documents, including keys in embedded sub-documents.
使用查询运算符指定条件
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
A query filter document can use the query operators to specify conditions in the following form:
[ <field1> => [ <operator1> => <value1> ], ... ]
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, ... }
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))
In addition to the equality filter, MongoDB provides various query operators to specify filter conditions. Use the FilterDefinitionBuilder methods to create a filter document. For example:
var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
A query filter document can use the query operators to specify conditions in the following form:
{ <field1> => { <operator1> => <value1> }, ... }
In addition to the equality condition, MongoDB provides various query operators to specify filter conditions. Use the com.mongodb.client.model.Filters_
helper methods to facilitate the creation of filter documents. For example:
and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))
以下示例从inventory
集合中检索所有文档,其中status
等于"A"
或"D"
:
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
Copy the following filter into the Compass query bar and click Find:
{ status: { $in: [ "A", "D" ] } }
cursor = db.inventory.find({"status": {"$in": ["A", "D"]}})
findIterable = collection.find(in("status", "A", "D"));
const cursor = db.collection('inventory').find({
status: { $in: ['A', 'D'] }
});
$cursor = $db->inventory->find(['status' => ['$in' => ['A', 'D']]]);
cursor = db.inventory.find({"status": {"$in": ["A", "D"]}})
findPublisher = collection.find(in("status", "A", "D"));
var filter = Builders<BsonDocument>.Filter.In("status", new[] { "A", "D" });
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { status => { '$in' => [ "A", "D" ] } } );
client[:inventory].find(status: { '$in' => [ 'A', 'D' ]})
findObservable = collection.find(in("status", "A", "D"))
该操作对应于以下 SQL 语句:
SELECT * FROM inventory WHERE status in ("A", "D")
请参阅查询和投影运算符文档以获取 MongoDB 查询运算符的完整列表。
指定 AND 条件
复合查询可以为集合文档中的多个字段指定条件。隐式地,逻辑AND
连接连接复合查询的子句,以便该查询选择集合中符合所有条件的文档。
以下示例检索inventory
集合中status
等于"A"
和 qty
小于($lt)30
的所有文档:
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
Copy the following filter into the Compass query bar and click Find:
{ status: "A", qty: { $lt: 30 } }
cursor = db.inventory.find({"status": "A", "qty": {"$lt": 30}})
findIterable = collection.find(and(eq("status", "A"), lt("qty", 30)));
const cursor = db.collection('inventory').find({
status: 'A',
qty: { $lt: 30 }
});
$cursor = $db->inventory->find([
'status' => 'A',
'qty' => ['$lt' => 30],
]);
cursor = db.inventory.find({"status": "A", "qty": {"$lt": 30}})
findPublisher = collection.find(and(eq("status", "A"), lt("qty", 30)));
var builder = Builders<BsonDocument>.Filter;
var filter = builder.And(builder.Eq("status", "A"), builder.Lt("qty", 30));
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { status => "A", qty => { '$lt' => 30 } } );
client[:inventory].find(status: 'A', qty: { '$lt' => 30 })
findObservable = collection.find(and(equal("status", "A"), lt("qty", 30)))
该操作对应于以下 SQL 语句:
SELECT * FROM inventory WHERE status = "A" AND qty < 30
有关其他 MongoDB 比较运算符,请参见comparison operators。
指定 OR 条件
使用$or运算符,可以指定一个复合查询,该查询将每个子句与逻辑OR
连词连接起来,以便该查询选择集合中至少匹配一个条件的文档。
以下示例检索集合中status
等于"A"
或 qty
小于($lt)30
的所有文档:
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
Copy the following filter into the Compass query bar and click Find:
{ $or: [ { status: "A" }, { qty: { $lt: 30 } } ] }
cursor = db.inventory.find(
{"$or": [{"status": "A"}, {"qty": {"$lt": 30}}]})
findIterable = collection.find(or(eq("status", "A"), lt("qty", 30)));
const cursor = db.collection('inventory').find({
$or: [{ status: 'A' }, { qty: { $lt: 30 } }]
});
$cursor = $db->inventory->find([
'$or' => [
['status' => 'A'],
['qty' => ['$lt' => 30]],
],
]);
cursor = db.inventory.find(
{"$or": [{"status": "A"}, {"qty": {"$lt": 30}}]})
findPublisher = collection.find(or(eq("status", "A"), lt("qty", 30)));
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Or(builder.Eq("status", "A"), builder.Lt("qty", 30));
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find(
{ '$or' => [ { status => "A" }, { qty => { '$lt' => 30 } } ] }
);
client[:inventory].find('$or' => [{ status: 'A' },
{ qty: { '$lt' => 30 } }
])
findObservable = collection.find(or(equal("status", "A"), lt("qty", 30)))
该操作对应于以下 SQL 语句:
SELECT * FROM inventory WHERE status = "A" OR qty < 30
Note
使用comparison operators的查询受Type Bracketing约束。
指定 AND 以及 OR 条件
在以下示例中,复合查询文档选择集合中所有的文档,其中status
等于"A"
并且 *要么qty
小于($lt)30
或 item
以字符p
开头:
db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
Copy the following filter into the Compass query bar and click Find:
{ status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] }
cursor = db.inventory.find({
"status": "A",
"$or": [{"qty": {"$lt": 30}}, {"item": {"$regex": "^p"}}]})
findIterable = collection.find(
and(eq("status", "A"),
or(lt("qty", 30), regex("item", "^p")))
);
const cursor = db.collection('inventory').find({
status: 'A',
$or: [{ qty: { $lt: 30 } }, { item: { $regex: '^p' } }]
});
$cursor = $db->inventory->find([
'status' => 'A',
'$or' => [
['qty' => ['$lt' => 30]],
// Alternatively: ['item' => new \MongoDB\BSON\Regex('^p')]
['item' => ['$regex' => '^p']],
],
]);
cursor = db.inventory.find({
"status": "A",
"$or": [{"qty": {"$lt": 30}}, {"item": {"$regex": "^p"}}]})
findPublisher = collection.find(
and(eq("status", "A"),
or(lt("qty", 30), regex("item", "^p")))
);
var builder = Builders<BsonDocument>.Filter;
var filter = builder.And(
builder.Eq("status", "A"),
builder.Or(builder.Lt("qty", 30), builder.Regex("item", new BsonRegularExpression("^p"))));
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find(
{
status => "A",
'$or' => [ { qty => { '$lt' => 30 } }, { item => { '$regex' => "^p" } } ]
}
);
client[:inventory].find(status: 'A',
'$or' => [{ qty: { '$lt' => 30 } },
{ item: { '$regex' => BSON::Regexp::Raw.new('^p') } }
])
findObservable = collection.find(and(
equal("status", "A"),
or(lt("qty", 30), regex("item", "^p")))
)
该操作对应于以下 SQL 语句:
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
Note
MongoDB 支持正则表达式$regex查询以执行字符串模式匹配。
其他查询教程
有关其他查询示例,请参见:
Behavior
Cursor
The db.collection.find() method returns a cursor to the matching documents.
The MongoDB Compass Find operation opens a cursor to the matching documents of the collection based on the find query.
For more information on sampling in MongoDB Compass, see the Compass FAQ.
The pymongo.collection.Collection.find() method returns a cursor to the matching documents. See the PyMongo documentation for iterating over a cursor.
The com.mongodb.client.MongoCollection.find method returns an instance of the com.mongodb.client.FindIterable interface.
The Collection.find() method returns a cursor.
The MongoDB\Collection::find() method returns a cursor to the matching documents. See the MongoDB PHP Library documentation for iterating over a cursor.
com.mongodb.reactivestreams.client.MongoCollection.find returns an instance of the com.mongodb.reactivestreams.client.FindPublisher interface.
The MongoCollection.Find() method returns a cursor to the matching documents. See the MongoDB C# driver documentation for iterating over a cursor.
The MongoDB::Collection::find() method returns a cursor to the matching documents. See the MongoDB Perl driver documentation for iterating over a cursor.
The Mongo::Collection#find() method returns a CollectionView, which is an Enumerable
. A Cursor is created when the View
is enumerated; for example, by calling #to_a()
or #each()
. You can also get an Enumerator
by calling #to_enum()
on the View
. See the Ruby driver API documentation for iterating over a cursor.
The collection.find() method returns the find Observable.
Read Isolation
3.2 版中的新功能。
对于对replica sets的读取和副本集shards的读取,读取关注使 Client 端可以为其读取选择隔离级别。有关更多信息,请参见Read Concern。
Additional Methods
The following methods can also read documents from a collection:
-
In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries.
Note
The db.collection.findOne() method also performs a read operation to return a single document. Internally, the db.collection.findOne() method is the db.collection.find() method with a limit of 1.
Additional Options
In addition to filter
, MongoDB Compass also allows the following options to be passed to the query bar:
Project | Specify which fields to return in the resulting data. |
Sort | Specify the sort order of the returned documents. |
Skip | Specify the first n-number of document to skip before returning the result set. |
Limit | Specify the maximum number of documents to return. |
Additional Methods
The following methods can also read documents from a collection:
-
In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See the PyMongo Aggregation Examples.
Note
The pymongo.collection.Collection.find_one() method also performs a read operation to return a single document. Internally, the pymongo.collection.Collection.find_one() method is the pymongo.collection.Collection.find() method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
- In the aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See the Java Synchronous Driver Aggregation Examples.
Additional Methods
The following methods can also read documents from a collection:
-
In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See the MongoDB Node.js Driver's aggregation tutorial.
Note
The Collection.findOne() method also performs a read operation to return a single document. Internally, the Collection.findOne() method is the Collection.find() method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
-
In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See the MongoDB PHP Library's aggregation example.
Note
The MongoDB\Collection::findOne() method also performs a read operation to return a single document. Internally, the MongoDB\Collection::findOne() method is the MongoDB\Collection::find() method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
- In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See com.mongodb.reactivestreams.client.MongoCollection.aggregate for more information.
Additional Methods
The following methods can also read documents from a collection:
-
In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See the MongoDB C# driver's LINQ documentation.
Note
The MongoCollection.FindOne() method also performs a read operation to return a single document. Internally, the MongoCollection.FindOne() method is the MongoCollection.Find() method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
-
In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See the MongoDB Perl driver's aggregation examples.
Note
The MongoDB::Collection::find_one() method also performs a read operation to return a single document. Internally, the MongoDB::Collection::find_one() method is the MongoDB::Collection::find() method with a limit of 1.
Additional Methods
The following methods can also read documents from a collection:
- In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See the MongoDB Ruby driver's aggregation examples.
Additional Methods
The following methods can also read documents from a collection:
- In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries. See the MongoDB Scala driver's aggregate method.