文档菜单
文档首页
/
MongoDB 手册
/

查询文档

在本页

  • 选择集合中的所有文档
  • 指定等式条件
  • 使用查询运算符指定条件
  • 指定AND 条件
  • 指定 OR 条件
  • 指定 AND 以及 OR 条件
  • 使用 MongoDB Atlas 查询文档
  • 更多查询教程
  • 行为
  • 其他方法和选项

要查询文档,指定一个查询谓词,指明您想要返回的文档。如果您指定一个空的查询谓词({ }),则查询将返回集合中的所有文档。

您可以使用以下方法在 MongoDB 中查询文档

  • 您编程语言的驱动程序。

  • MongoDB Atlas UI。想了解更多,请参阅使用 MongoDB Atlas 查询文档

  • MongoDB Compass.


使用右上角的选择您的语言下拉菜单来设置以下示例的语言或选择 MongoDB Compass。


本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用 db.collection.find() 方法。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作创建 inventory 集合。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用 mongoc_collection_find_with_opts.

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用 MongoCollection.Find() 方法,在 MongoDB C# 驱动程序 中。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用Collection.Find函数在MongoDB Go Driver中。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用MongoDB的com.mongodb.reactivestreams.client.MongoCollection.find方法。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用MongoDB的com.mongodb.client.MongoCollection.find方法。

提示:

驱动程序提供了com.mongodb.client.model.Filters辅助方法,以简化创建过滤器文档的过程。本页上的示例使用这些方法创建过滤器文档。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作通过使用MongoDB Kotlin Coroutine Driver.中的MongoCollection.find()方法。

提示:

驱动程序提供了com.mongodb.client.model.Filters辅助方法,以简化创建过滤器文档的过程。本页上的示例使用这些方法创建过滤器文档。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作motor.motor_asyncio.AsyncIOMotorCollection.find 方法中使用 Motor 驱动程序。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用Collection.find()方法在MongoDB Node.js 驱动程序。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用 MongoDB::Collection::find() 方法,该方法在 MongoDB Perl 驱动 中。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用 MongoDB\\Collection::find() 方法,该方法在 MongoDB PHP 库 中。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用 pymongo.collection.Collection.find 方法,该方法在 PyMongo Python 驱动中。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用 Mongo::Collection#find() 方法,该方法在 MongoDB Ruby 驱动 中。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

本页提供了使用 db.collection.find() 方法在 mongosh. 进行查询操作示例。查询操作使用 collection.find() 方法在 MongoDB Scala Driver 中。

此页面的示例使用 inventory 集合。连接到您的 MongoDB 实例中的测试数据库,然后使用 MongoDB Compass.

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" }
]);
[
{ "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" }
]

有关在 MongoDB Compass 中插入文档的说明,请参阅 插入文档。

mongoc_collection_t *collection;
mongoc_bulk_operation_t *bulk;
bson_t *doc;
bool r;
bson_error_t error;
bson_t reply;
collection = mongoc_database_get_collection (db, "inventory");
bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
doc = BCON_NEW (
"item", BCON_UTF8 ("journal"),
"qty", BCON_INT64 (25),
"size", "{",
"h", BCON_DOUBLE (14),
"w", BCON_DOUBLE (21),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("A"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("notebook"),
"qty", BCON_INT64 (50),
"size", "{",
"h", BCON_DOUBLE (8.5),
"w", BCON_DOUBLE (11),
"uom", BCON_UTF8 ("in"),
"}",
"status", BCON_UTF8 ("A"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("paper"),
"qty", BCON_INT64 (100),
"size", "{",
"h", BCON_DOUBLE (8.5),
"w", BCON_DOUBLE (11),
"uom", BCON_UTF8 ("in"),
"}",
"status", BCON_UTF8 ("D"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("planner"),
"qty", BCON_INT64 (75),
"size", "{",
"h", BCON_DOUBLE (22.85),
"w", BCON_DOUBLE (30),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("D"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
doc = BCON_NEW (
"item", BCON_UTF8 ("postcard"),
"qty", BCON_INT64 (45),
"size", "{",
"h", BCON_DOUBLE (10),
"w", BCON_DOUBLE (15.25),
"uom", BCON_UTF8 ("cm"),
"}",
"status", BCON_UTF8 ("A"));
r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
goto done;
}
/* "reply" is initialized on success or error */
r = (bool) mongoc_bulk_operation_execute (bulk, &reply, &error);
if (!r) {
MONGOC_ERROR ("%s\n", error.message);
}
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);
docs := []interface{}{
bson.D{
{"item", "journal"},
{"qty", 25},
{"size", bson.D{
{"h", 14},
{"w", 21},
{"uom", "cm"},
}},
{"status", "A"},
},
bson.D{
{"item", "notebook"},
{"qty", 50},
{"size", bson.D{
{"h", 8.5},
{"w", 11},
{"uom", "in"},
}},
{"status", "A"},
},
bson.D{
{"item", "paper"},
{"qty", 100},
{"size", bson.D{
{"h", 8.5},
{"w", 11},
{"uom", "in"},
}},
{"status", "D"},
},
bson.D{
{"item", "planner"},
{"qty", 75},
{"size", bson.D{
{"h", 22.85},
{"w", 30},
{"uom", "cm"},
}},
{"status", "D"},
},
bson.D{
{"item", "postcard"},
{"qty", 45},
{"size", bson.D{
{"h", 10},
{"w", 15.25},
{"uom", "cm"},
}},
{"status", "A"},
},
}
result, err := coll.InsertMany(context.TODO(), docs)
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' }")
));
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' }")
));
collection.insertMany(
listOf(
Document("item", "journal")
.append("qty", 25)
.append("size", Document("h", 14)
.append("w", 21)
.append("uom", "cm")
)
.append("status", "A"),
Document("item", "notebook")
.append("qty", 50)
.append("size", Document("h", 8.5)
.append("w", 11)
.append("uom", "in")
)
.append("status", "A"),
Document("item", "paper")
.append("qty", 100)
.append("size", Document("h", 8.5)
.append("w", 11)
.append("uom", "in")
)
.append("status", "D"),
Document("item", "planner")
.append("qty", 75)
.append("size", Document("h", 22.85)
.append("w", 30)
.append("uom", "cm")
)
.append("status", "D"),
Document("item", "postcard")
.append("qty", 45)
.append("size", Document("h", 10)
.append("w", 15.25)
.append("uom", "cm")
)
.append("status", "A"),
)
)
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",
},
]
)
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'
}
]);
$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"
}
]
);
$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',
],
]);
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",
},
]
)
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' }
])
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()

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 查询栏。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

要选择集合中的所有文档,请将空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择标准

db.inventory.find( {} )
Compass select all documents in collection
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (NULL);
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);

请确保通过调用以下方法适当清理任何打开的资源

var filter = Builders<BsonDocument>.Filter.Empty;
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{},
)
FindPublisher<Document> findPublisher = collection.find(new Document());
FindIterable<Document> findIterable = collection.find(new Document());
val flowInsertMany = collection
.find(empty())
cursor = db.inventory.find({})
const cursor = db.collection('inventory').find({});
$cursor = $db->coll("inventory")->find( {} );
$cursor = $db->inventory->find([]);
cursor = db.inventory.find({})
client[:inventory].find({})
var findObservable = collection.find(Document())

此操作使用查询谓词{},对应以下SQL语句

SELECT * FROM inventory

有关该方法语法的更多信息,请参阅find()

有关MongoDB Compass查询栏的更多信息,请参阅查询栏。

有关该方法语法的更多信息,请参阅mongoc_collection_find_with_opts。

有关该方法语法的更多信息,请参阅Find().

有关该方法语法的更多信息,请参阅com.mongodb.reactivestreams.client.MongoCollection.find

有关方法语法的更多信息,请参阅com.mongodb.client.MongoCollection.find.

有关方法语法的更多信息,请参阅MongoCollection.find().

要查看find()方法支持的选项,请参阅find().

有关该方法语法的更多信息,请参阅 find().

有关该方法语法的更多信息,请参阅 find().

有关该方法语法的更多信息,请参阅 find.

有关该方法语法的更多信息,请参阅 find().

关于方法语法的更多信息,请参阅collection.find().

要指定等价条件,请在查询过滤器文档中使用<field>:<value>表达式:

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

要指定等价条件,请在查询过滤器文档中使用<field>:<value>表达式:

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

要指定等价条件,请在查询过滤器文档中使用<field>:<value>表达式:

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

要指定等价条件,请使用Eq方法构造过滤器

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

要指定相等条件,请使用 com.mongodb.client.model.Filters.eq 方法创建 查询过滤器文档:

and(eq(<field1>, <value1>), eq(<field2>, <value2>) ...)

要指定相等条件,请使用 com.mongodb.client.model.Filters.eq_ 方法创建 查询过滤器文档:

and(eq(<field1>, <value1>), eq(<field2>, <value2>) ...)

要指定相等条件,请使用 Filters.eq() 方法创建 查询过滤器文档:

and(eq(<field1>, <value1>), eq(<field2>, <value2>) ...)

要指定等价条件,请在查询过滤器文档中使用<field>:<value>表达式:

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

要指定等价条件,请在查询过滤器文档中使用<field>:<value>表达式:

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

查询过滤器文档 中使用 <field> => <value> 表达式来指定条件:

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

查询过滤器文档 中使用 <field> => <value> 表达式来指定条件:

[ <field1> => <value1>, ... ]

要指定等价条件,请在查询过滤器文档中使用<field>:<value>表达式:

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

查询过滤器文档 中使用 <field> => <value> 表达式来指定条件:

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

要指定相等条件,请使用 com.mongodb.client.model.Filters.eq_ 方法创建 查询过滤器文档:

and(equal(<field1>, <value1>), equal(<field2>, <value2>) ...)

以下示例从 inventory 集合中选取所有 status 等于 "D" 的文档

db.inventory.find( { status: "D" } )

将以下过滤器复制到 Compass 查询栏并点击查找:

{ status: "D" }
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("D"));
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "D");
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{{"status", "D"}},
)
findPublisher = collection.find(eq("status", "D"));
findIterable = collection.find(eq("status", "D"));
val findFlow = collection
.find(eq("status", "D"))
cursor = db.inventory.find({"status": "D"})
const cursor = db.collection('inventory').find({ status: 'D' });
$cursor = $db->coll("inventory")->find( { status => "D" } );
$cursor = $db->inventory->find(['status' => 'D']);
cursor = db.inventory.find({"status": "D"})
client[:inventory].find(status: 'D')
findObservable = collection.find(equal("status", "D"))

此操作使用查询谓词 { status: "D" },对应以下 SQL 语句

SELECT * FROM inventory WHERE status = "D"

注意

MongoDB Compass查询栏会根据您集合文档中的键自动完成当前查询,包括嵌套子文档中的键。

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

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

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

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

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

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

除了等值过滤器外,MongoDB还提供了各种 查询运算符 来指定过滤条件。使用 FilterDefinitionBuilder 方法来创建过滤器文档。例如

var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));

除了等值条件外,MongoDB还提供了各种 查询运算符 来指定过滤条件。使用 com.mongodb.client.model.Filters 辅助方法来简化过滤器文档的创建。例如

and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))

除了等值条件外,MongoDB还提供了各种 查询运算符 来指定过滤条件。使用 com.mongodb.client.model.Filters 辅助方法来简化过滤器文档的创建。例如

and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))

除了等式条件外,MongoDB还提供了各种查询运算符来指定过滤条件。使用com.mongodb.client.model.Filters辅助方法来简化过滤文档的创建。例如

and(gte(<field1>, <value1>), lt(<field2>, <value2>), eq(<field3>, <value3>))

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

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

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

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

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

{ <field1> => { <operator1> => <value1> }, ... }

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

[ <field1> => [ <operator1> => <value1> ], ... ]

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

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

查询过滤器文档可以使用 查询运算符 来指定以下形式的条件

{ <field1> => { <operator1> => <value1> }, ... }

除了等式条件外,MongoDB还提供了各种查询运算符来指定过滤条件。使用com.mongodb.client.model.Filters_辅助方法来简化过滤文档的创建。例如

and(gte(<field1>, <value1>), lt(<field2>, <value2>), equal(<field3>, <value3>))

以下示例从inventory集合中检索所有status等于“A”或“D”的文档

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

将以下过滤条件复制到Compass查询栏,然后单击查找

{ status: { $in: [ "A", "D" ] } }
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
"status", "{",
"$in", "[",
BCON_UTF8 ("A"), BCON_UTF8 ("D"),
"]",
"}");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.In("status", new[] { "A", "D" });
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{{"status", bson.D{{"$in", bson.A{"A", "D"}}}}})
findPublisher = collection.find(in("status", "A", "D"));
findIterable = collection.find(in("status", "A", "D"));
val findFlow = collection
.find(`in`("status", "A", "D"))
cursor = db.inventory.find({"status": {"$in": ["A", "D"]}})
const cursor = db.collection('inventory').find({
status: { $in: ['A', 'D'] }
});
$cursor = $db->coll("inventory")->find( { status => { '$in' => [ "A", "D" ] } } );
$cursor = $db->inventory->find(['status' => ['$in' => ['A', 'D']]]);
cursor = db.inventory.find({"status": {"$in": ["A", "D"]}})
client[:inventory].find(status: { '$in' => [ 'A', 'D' ]})
findObservable = collection.find(in("status", "A", "D"))

注意

尽管您可以使用$or运算符来表示此查询,但在对同一字段进行等式检查时,最好使用$in运算符而不是$or运算符。

该操作使用查询谓词{ status: { $in: [ "A", "D" ] } },对应以下SQL语句

SELECT * FROM inventory WHERE status in ("A", "D")

有关MongoDB查询运算符的完整列表,请参阅查询和投影运算符文档。

复合查询可以指定集合文档中多个字段的条件。隐式地,逻辑AND运算符连接复合查询的各个子句,以便查询选择满足所有条件的集合文档。

以下示例检索inventory集合中所有status等于"A"qty小于 ($lt) 30的文档。

db.inventory.find( { status: "A", qty: { $lt: 30 } } )

将以下过滤条件复制到Compass查询栏,然后单击查找

{ status: "A", qty: { $lt: 30 } }
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
"status", BCON_UTF8 ("A"),
"qty", "{",
"$lt", BCON_INT64 (30),
"}");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var builder = Builders<BsonDocument>.Filter;
var filter = builder.And(builder.Eq("status", "A"), builder.Lt("qty", 30));
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
{"qty", bson.D{{"$lt", 30}}},
})
findPublisher = collection.find(and(eq("status", "A"), lt("qty", 30)));
findIterable = collection.find(and(eq("status", "A"), lt("qty", 30)));
val findFlow = collection
.find(and(eq("status", "A"), lt("qty", 30)))
cursor = db.inventory.find({"status": "A", "qty": {"$lt": 30}})
const cursor = db.collection('inventory').find({
status: 'A',
qty: { $lt: 30 }
});
$cursor = $db->coll("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}})
client[:inventory].find(status: 'A', qty: { '$lt' => 30 })
findObservable = collection.find(and(equal("status", "A"), lt("qty", 30)))

该操作使用查询谓词{ status: "A", qty: { $lt: 30 } },它对应于以下SQL语句

SELECT * FROM inventory WHERE status = "A" AND qty < 30

有关其他MongoDB比较运算符,请参阅比较运算符

使用$or运算符,您可以指定一个复合查询,该查询使用逻辑OR运算符连接每个子句,以便查询选择至少满足一个条件的集合文档。

以下示例检索集合中所有status等于"A"qty小于 ($lt) 30的文档。

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

将以下过滤条件复制到Compass查询栏,然后单击查找

{ $or: [ { status: "A" }, { qty: { $lt: 30 } } ] }
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
"$or", "[",
"{",
"status", BCON_UTF8 ("A"),
"}","{",
"qty", "{",
"$lt", BCON_INT64 (30),
"}",
"}",
"]");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Or(builder.Eq("status", "A"), builder.Lt("qty", 30));
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"$or",
bson.A{
bson.D{{"status", "A"}},
bson.D{{"qty", bson.D{{"$lt", 30}}}},
}},
})
findPublisher = collection.find(or(eq("status", "A"), lt("qty", 30)));
findIterable = collection.find(or(eq("status", "A"), lt("qty", 30)));
val findFlow = collection
.find(or(eq("status", "A"), lt("qty", 30)))
cursor = db.inventory.find({"$or": [{"status": "A"}, {"qty": {"$lt": 30}}]})
const cursor = db.collection('inventory').find({
$or: [{ status: 'A' }, { qty: { $lt: 30 } }]
});
$cursor = $db->coll("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}}]})
client[:inventory].find('$or' => [{ status: 'A' },
{ qty: { '$lt' => 30 } }
])
findObservable = collection.find(or(equal("status", "A"), lt("qty", 30)))

该操作使用查询谓词{ $or: [ { status: 'A' }, { qty: { $lt: 30 } } ] },它对应于以下SQL语句

SELECT * FROM inventory WHERE status = "A" OR qty < 30

注意

使用比较运算符的查询受类型括号的影响。

以下示例中,复合查询文档选择所有状态等于"A"的集合中的文档,并且要么数量小于($lt)30要么项目以字符p开头。

db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )

将以下过滤条件复制到Compass查询栏,然后单击查找

{ status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] }
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
"status", BCON_UTF8 ("A"),
"$or", "[",
"{",
"qty", "{",
"$lt", BCON_INT64 (30),
"}",
"}","{",
"item", BCON_REGEX ("^p", ""),
"}",
"]");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
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, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
{"$or", bson.A{
bson.D{{"qty", bson.D{{"$lt", 30}}}},
bson.D{{"item", bson.Regex{Pattern: "^p", Options: ""}}},
}},
})
findPublisher = collection.find(
and(eq("status", "A"),
or(lt("qty", 30), regex("item", "^p")))
);
findIterable = collection.find(
and(eq("status", "A"),
or(lt("qty", 30), regex("item", "^p")))
);
val findFlow = collection
.find(
and(eq("status", "A"),
or(lt("qty", 30), regex("item", "^p")))
)
cursor = db.inventory.find(
{"status": "A", "$or": [{"qty": {"$lt": 30}}, {"item": {"$regex": "^p"}}]}
)
const cursor = db.collection('inventory').find({
status: 'A',
$or: [{ qty: { $lt: 30 } }, { item: { $regex: '^p' } }]
});
$cursor = $db->coll("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"}}]}
)
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")))
)

此操作使用查询谓词为

{
status: 'A',
$or: [
{ qty: { $lt: 30 } }, { item: { $regex: '^p' } }
]
}

对应以下SQL语句

SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")

注意

MongoDB支持正则表达式$regex查询以执行字符串模式匹配。

本节中的示例使用电影样本数据集。有关如何将样本数据集加载到您的MongoDB Atlas部署中的信息,请参阅加载样本数据。

要在MongoDB Atlas中查询返回的字段,请按照以下步骤操作

1
  1. 如果尚未显示,请从导航栏中的 组织菜单选择包含您所需项目的组织。

  2. 如果尚未显示,请从导航栏中的项目菜单选择您的项目。

  3. 点击侧边栏中的集群

2
  1. 导航到

  2. 对于包含示例数据的集群,点击浏览集合

  3. 在左侧导航窗格中,选择sample_mflix数据库。

3

过滤器字段中指定过滤器字段

将以下查询过滤器文档复制到过滤器搜索栏中。

{ year: 1924 }
4

此查询过滤器返回所有在 sample_mflix.movies 集合中,year 字段匹配 1924 的文档。

更多查询示例,请参阅

方法 db.collection.find() 返回与匹配文档的 游标

MongoDB Compass 的 查找 操作根据查找查询打开集合匹配文档的 游标

有关 MongoDB Compass 中采样信息的更多信息,请参阅 Compass 常见问题解答

mongoc_collection_find 方法返回与匹配文档的 游标

MongoCollection.Find() 方法返回与匹配文档的 游标。有关在 MongoDB C# 驱动程序中遍历游标的信息,请参阅 遍历游标

函数 Collection.Find 返回一个指向匹配文档的 Cursor。有关更多信息,请参阅 Cursor 文档。

该方法返回一个 com.mongodb.client.MongoCollection.find 方法的实例。

方法 MongoCollection.find() 返回一个 FindFlow 类的实例。

方法 Collection.find() 返回一个 游标。

方法 MongoDB::Collection::find() 返回一个指向匹配文档的 游标。有关迭代游标的信息,请参阅 MongoDB Perl 驱动程序的文档中的 迭代游标 部分。

方法 MongoDB\\Collection::find() 返回一个指向匹配文档的 游标。有关迭代游标的信息,请参阅 MongoDB PHP 库文档中的 迭代游标 部分。

方法pymongo.collection.Collection.find返回一个指向匹配文档的游标。有关遍历游标的信息,请参阅PyMongo文档。

方法Mongo::Collection#find()返回一个CollectionView,这是一个Enumerable。当View被枚举时,例如通过调用#to_a()#each(),将创建一个Cursor。您还可以通过在View上调用#to_enum()来获取一个Enumerator。有关遍历游标的更多信息,请参阅Ruby驱动API文档。

方法collection.find()返回find Observable

对于读取到副本集和副本集分片,读取关注点允许客户端选择其读取的隔离级别。更多信息,请参阅读取关注点

当使用 MongoDB 驱动程序或 mongosh 运行查找操作时,命令返回一个管理查询结果的 游标。查询结果不会以文档数组的形式返回。

有关如何遍历游标中的文档的说明,请参阅您的 驱动程序文档。如果您使用的是 mongosh,请参阅 mongosh 中遍历游标

以下也可以从集合中读取文档

注意

db.collection.findOne() 方法执行与 db.collection.find() 方法相同的操作,但限制为 1。

除了 filter 外,MongoDB Compass 还允许您将以下选项传递到查询栏

指定要返回的结果数据中的字段。
指定返回文档的排序顺序。
指定在返回结果集之前要跳过的第一个 n 个文档。
指定要返回的最大文档数。

以下方法也可以从集合中读取文档

以下也可以从集合中读取文档

注意

MongoCollection.FindOne() 方法与MongoCollection.Find() 方法执行相同的操作,但限制为1。

以下也可以从集合中读取文档

以下也可以从集合中读取文档

以下方法也可以从集合中读取文档

以下也可以从集合中读取文档

注意

Collection.findOne() 方法与Collection.find() 方法具有相同的操作,但限制为1。

以下也可以从集合中读取文档

注意

MongoDB::Collection::find_one()方法与MongoDB::Collection::find()方法执行相同的操作,但限制为返回1个结果。

以下也可以从集合中读取文档

注意

MongoDB\\Collection::findOne()方法与MongoDB\\Collection::find()方法执行相同的操作,但限制为返回1个结果。

以下也可以从集合中读取文档

注意

pymongo.collection.Collection.find_one 方法与pymongo.collection.Collection.find 方法类似,但限制返回结果数量为 1。

以下也可以从集合中读取文档

以下也可以从集合中读取文档

返回

方法