Docs 菜单
Docs 主页
/
MongoDB Manual
/

查询文档

在此页面上

  • 选择集合中的所有文档
  • 指定相等条件
  • 使用查询操作符指定条件
  • 指定 AND条件
  • 指定 OR 条件
  • 指定 ANDOR 条件
  • 使用 MongoDB Atlas 查询文档
  • 其他查询教程
  • 行为
  • 其他方法和选项

要查询文档,请指定查询谓词以指示要返回的文档。如果指定空查询谓词 ( { } ),则查询将返回集合中的所有文档。

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

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

  • MongoDB Atlas 用户界面。要了解更多信息,请参阅使用 MongoDB Atlas 查询文档

  • MongoDB Compass。


➤ 使用右上角的 Select your language(选择语言)下拉菜单,设置以下示例的语言或选择 MongoDB Compass。


本页面提供的示例展示在 mongosh 中使用 db.collection.find() 方法执行查询操作。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

此页面中的示例展示了使用 MongoDB Compass 执行的查询操作。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用 mongoc_collection_find_with_opts 进行查询操作的示例。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供的示例展示了使用 MongoCollection.Find() MongoDB C# 驱动程序 中的方法 。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用 Collection.Find 查询操作的示例 MongoDB Go 驱动程序 中的函数 。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供的示例展示了使用 com.mongodb.reactivestreams.client.MongoCollection.find MongoDB Java Reactive Streams Driver 中的方法 。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

此页面中的示例展示了使用 MongoDB Java 同步驱动程序中的 com.mongodb.client.MongoCollection.find 方法执行的查询操作。

提示

驱动程序提供了 com.mongodb.client.model.Filters 帮助器方法,以方便创建筛选器文档。本页上的示例使用这些方法创建筛选器文档。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用 MongoCollection.find() 进行查询操作的示例。 MongoDB Kotlin 协程驱动程序中的方法。

提示

驱动程序提供了 com.mongodb.client.model.Filters 帮助器方法,以方便创建筛选器文档。本页上的示例使用这些方法创建筛选器文档。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供的示例展示了使用motor.motor_asyncio.AsyncIOMotorCollection.find Motor 中的方法 驱动程序。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页面提供的示例展示使用 Collection.find()方法在 MongoDB Node.js 驱动程序中执行查询操作。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供了使用 MongoDB::Collection::find() 进行查询操作的示例 MongoDB Perl 驱动程序 中的方法 。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页面提供的示例展示在 MongoDB PHP Library 中使用 MongoDB\\Collection::find() 方法执行查询操作。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供的示例展示了使用pymongo.collection.Collection.find PyMongo Python 驱动程序中的方法。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页面提供的示例展示在 MongoDB Ruby 驱动程序中使用 Mongo::Collection#find() 方法执行查询操作。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

本页提供的示例展示了使用 collection.find() MongoDB Scala 驱动程序 中的方法 。

此页面上的示例使用的是 inventory 集合。连接到 MongoDB 实例中的测试数据库,然后创建 inventory 集合:

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 选择集合中的所有文档
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 查询栏中,然后单击 Find

{ 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 查询栏中,然后单击 Find

{ 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 查询栏中,然后单击 Find

{ 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 查询栏中,然后单击 Find

{ $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

注意

使用比较运算符的查询受类型范围限制。

在以下示例中,复合查询文档选择集合中 status 等于 "A"qty 小于 ($lt) 30item 以字符 p 开头的文档:

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

将以下过滤器复制到 Compass 查询栏中,然后单击 Find

{ 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. 如果尚未显示,请从导航栏的 Organizations 菜单中选择包含所需项目的组织。

  2. 如果尚未显示,请从导航栏的 Projects(项目)菜单中选择所需的项目。

  3. 如果 Clusters(数据库部署)页面尚未出现,请单击侧边栏中的 Database(数据库)。

    此时会显示“集群”页面。

2
  1. 对于包含样本数据的集群,单击Browse Collections

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

  3. 选择 movies 集合。

3

Filter 字段中指定查询筛选器文档。查询筛选器文档使用查询运算符来指定搜索条件。

将以下查询筛选器文档复制到 Filter 搜索栏:

{ year: 1924 }
4

此查询筛选器返回 sample_mflix.movies 集合中 year 字段与 1924 匹配的所有文档。

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

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

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

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

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

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

Collection.Find 函数返回一个 游标 匹配文档。查看 游标 文档以获取更多信息。

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

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

MongoDB::Collection::find() 方法返回指向匹配文档的 游标 。有关 遍历游标的信息,请参阅 MongoDB Perl 驱动程序文档。

Mongo::Collection#find() 方法会返回 CollectionView,它是 Enumerable游标是在枚举 View 时创建的;例如,通过调用 #to_a()#each()。您还可以通过在 Enumerator 上调用 #to_enum() 来获取 View。有关迭代游标的信息,请参阅 Ruby 驱动程序 API 文档。

collection.find() 方法返回 find Observable

关于对副本集和副本集分片的读取,读关注 (read concern) 允许客户端为其读取操作选择隔离级别。有关更多信息,请参阅读关注 (read concern) 。

使用 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 的方法。

以下还可以从集合中读取文档:

以下还可以从集合中读取文档:

后退

方法