Docs 菜单
Docs 主页
/
MongoDB Manual
/ /

查询返回的项目字段

在此页面上

  • 返回匹配文档中的所有字段
  • 仅返回指定字段和 _id字段
  • 抑制 _id 字段
  • 返回除已排除字段之外的所有字段
  • 返回嵌入式文档中的特定字段
  • 抑制嵌入式文档中的特定字段
  • 对数组中嵌入式文档的投影
  • 已返回数组中特定于项目的数组元素
  • 使用聚合表达式投影字段
  • 使用 MongoDB Atlas 从查询返回的项目字段
  • 其他注意事项

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

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

  • MongoDB Atlas用户界面。 要学习;了解更多信息,请参阅使用MongoDB Atlas从查询中返回的项目字段

  • MongoDB Compass。


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


默认情况下,MongoDB 中的查询会返回匹配文档中的所有字段。要限制 MongoDB 发送到应用程序的数据量,可以添加 投影文档来指定或限制要返回的字段。

本页提供的示例展示了如何使用 MongoDB Compass 执行带投影的查询操作。

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

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

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

本页面提供的示例展示使用 MongoCollection.Find() 方法在 MongoDB C# 驱动程序中执行查询操作。

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

本页面提供的示例展示使用 MongoDB Go 驱动程序 中的 Collection.Find 函数执行投影查询操作。

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

本页提供了使用 com.mongodb.reactivestreams 进行带有投影的查询操作的示例。 客户端.MongoCollection.find MongoDB Java Reactive Streams 驱动程序 中的方法 。

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

本页面将借助 MongoDB Java 同步驱动程序 中的 com.mongodb.client.MongoCollection.find 方法展示带有投影的查询操作示例。

提示

此驱动程序提供了 com.mongodb.client.model.Filters 辅助方法,以便于创建过滤器文档。此页面中的示例使用这些方法创建过滤器文档。

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

本页提供的示例投影了使用查询()MongoDB Kotlin协程驱动程序中的方法。

提示

此驱动程序提供了 com.mongodb.client.model.Filters 辅助方法,以便于创建过滤器文档。此页面中的示例使用这些方法创建过滤器文档。

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

本页提供了使用motor.motor_asyncio.AsyncIOMotorCollection.find 电机 驱动程序中的 方法进行投影查询操作的示例。

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

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

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

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

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

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

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

本页提供了使用 PyMongo Python 驱动程序中的 pymongo.collection.Collection.find方法进行投影查询操作的示例。

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

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

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

本页面提供了使用 MongoDB Scala 驱动程序中的 collection.find () 方法进行投影查询操作的示例。

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

db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
[
{ "item": "journal", "status": "A", "size": { "h": 14, "w": 21, "uom": "cm" }, "instock": [ { "warehouse": "A", "qty": 5 } ] },
{ "item": "notebook", "status": "A", "size": { "h": 8.5, "w": 11, "uom": "in" }, "instock": [ { "warehouse": "C", "qty": 5 } ] },
{ "item": "paper", "status": "D", "size": { "h": 8.5, "w": 11, "uom": "in" }, "instock": [ { "warehouse": "A", "qty": 60 } ] },
{ "item": "planner", "status": "D", "size": { "h": 22.85, "w": 30, "uom": "cm" }, "instock": [ { "warehouse": "A", "qty": 40 } ] },
{ "item": "postcard", "status": "A", "size": { "h": 10, "w": 15.25, "uom": "cm" }, "instock": [ { "warehouse": "B", "qty": 15 }, { "warehouse": "C", "qty": 35 } ] }
]

有关在 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"),
"status", BCON_UTF8 ("A"),
"size", "{",
"h", BCON_DOUBLE (14),
"w", BCON_DOUBLE (21),
"uom", BCON_UTF8 ("cm"),
"}",
"instock", "[",
"{",
"warehouse", BCON_UTF8 ("A"),
"qty", BCON_INT64 (5),
"}",
"]");
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"),
"status", BCON_UTF8 ("A"),
"size", "{",
"h", BCON_DOUBLE (8.5),
"w", BCON_DOUBLE (11),
"uom", BCON_UTF8 ("in"),
"}",
"instock", "[",
"{",
"warehouse", BCON_UTF8 ("C"),
"qty", BCON_INT64 (5),
"}",
"]");
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"),
"status", BCON_UTF8 ("D"),
"size", "{",
"h", BCON_DOUBLE (8.5),
"w", BCON_DOUBLE (11),
"uom", BCON_UTF8 ("in"),
"}",
"instock", "[",
"{",
"warehouse", BCON_UTF8 ("A"),
"qty", BCON_INT64 (60),
"}",
"]");
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"),
"status", BCON_UTF8 ("D"),
"size", "{",
"h", BCON_DOUBLE (22.85),
"w", BCON_DOUBLE (30),
"uom", BCON_UTF8 ("cm"),
"}",
"instock", "[",
"{",
"warehouse", BCON_UTF8 ("A"),
"qty", BCON_INT64 (40),
"}",
"]");
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"),
"status", BCON_UTF8 ("A"),
"size", "{",
"h", BCON_DOUBLE (10),
"w", BCON_DOUBLE (15.25),
"uom", BCON_UTF8 ("cm"),
"}",
"instock", "[",
"{",
"warehouse", BCON_UTF8 ("B"),
"qty", BCON_INT64 (15),
"}","{",
"warehouse", BCON_UTF8 ("C"),
"qty", BCON_INT64 (35),
"}",
"]");
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[]
{
new BsonDocument
{
{ "item", "journal" },
{ "status", "A" },
{ "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } } },
{ "instock", new BsonArray
{
new BsonDocument { { "warehouse", "A" }, { "qty", 5 } } }
}
},
new BsonDocument
{
{ "item", "notebook" },
{ "status", "A" },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
{ "instock", new BsonArray
{
new BsonDocument { { "warehouse", "C" }, { "qty", 5 } } }
}
},
new BsonDocument
{
{ "item", "paper" },
{ "status", "D" },
{ "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } },
{ "instock", new BsonArray
{
new BsonDocument { { "warehouse", "A" }, { "qty", 60 } } }
}
},
new BsonDocument
{
{ "item", "planner" },
{ "status", "D" },
{ "size", new BsonDocument { { "h", 22.85 }, { "w", 30 }, { "uom", "cm" } } },
{ "instock", new BsonArray
{
new BsonDocument { { "warehouse", "A" }, { "qty", 40 } } }
}
},
new BsonDocument
{
{ "item", "postcard" },
{ "status", "A" },
{ "size", new BsonDocument { { "h", 10 }, { "w", 15.25 }, { "uom", "cm" } } },
{ "instock", new BsonArray
{
new BsonDocument { { "warehouse", "B" }, { "qty", 15 } },
new BsonDocument { { "warehouse", "C" }, { "qty", 35 } } }
}
}
};
collection.InsertMany(documents);
docs := []interface{}{
bson.D{
{"item", "journal"},
{"status", "A"},
{"size", bson.D{
{"h", 14},
{"w", 21},
{"uom", "cm"},
}},
{"instock", bson.A{
bson.D{
{"warehouse", "A"},
{"qty", 5},
},
}},
},
bson.D{
{"item", "notebook"},
{"status", "A"},
{"size", bson.D{
{"h", 8.5},
{"w", 11},
{"uom", "in"},
}},
{"instock", bson.A{
bson.D{
{"warehouse", "EC"},
{"qty", 5},
},
}},
},
bson.D{
{"item", "paper"},
{"status", "D"},
{"size", bson.D{
{"h", 8.5},
{"w", 11},
{"uom", "in"},
}},
{"instock", bson.A{
bson.D{
{"warehouse", "A"},
{"qty", 60},
},
}},
},
bson.D{
{"item", "planner"},
{"status", "D"},
{"size", bson.D{
{"h", 22.85},
{"w", 30},
{"uom", "cm"},
}},
{"instock", bson.A{
bson.D{
{"warehouse", "A"},
{"qty", 40},
},
}},
},
bson.D{
{"item", "postcard"},
{"status", "A"},
{"size", bson.D{
{"h", 10},
{"w", 15.25},
{"uom", "cm"},
}},
{"instock", bson.A{
bson.D{
{"warehouse", "B"},
{"qty", 15},
},
bson.D{
{"warehouse", "EC"},
{"qty", 35},
},
}},
},
}
result, err := coll.InsertMany(context.TODO(), docs)
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{ item: 'journal', status: 'A', size: { h: 14, w: 21, uom: 'cm' }, instock: [ { warehouse: 'A', qty: 5 }]}"),
Document.parse("{ item: 'notebook', status: 'A', size: { h: 8.5, w: 11, uom: 'in' }, instock: [ { warehouse: 'C', qty: 5}]}"),
Document.parse("{ item: 'paper', status: 'D', size: { h: 8.5, w: 11, uom: 'in' }, instock: [ { warehouse: 'A', qty: 60 }]}"),
Document.parse("{ item: 'planner', status: 'D', size: { h: 22.85, w: 30, uom: 'cm' }, instock: [ { warehouse: 'A', qty: 40}]}"),
Document.parse("{ item: 'postcard', status: 'A', size: { h: 10, w: 15.25, uom: 'cm' }, "
+ "instock: [ { warehouse: 'B', qty: 15 }, { warehouse: 'C', qty: 35 } ] }")
));
collection.insertMany(asList(
Document.parse("{ item: 'journal', status: 'A', size: { h: 14, w: 21, uom: 'cm' }, instock: [ { warehouse: 'A', qty: 5 }]}"),
Document.parse("{ item: 'notebook', status: 'A', size: { h: 8.5, w: 11, uom: 'in' }, instock: [ { warehouse: 'C', qty: 5}]}"),
Document.parse("{ item: 'paper', status: 'D', size: { h: 8.5, w: 11, uom: 'in' }, instock: [ { warehouse: 'A', qty: 60 }]}"),
Document.parse("{ item: 'planner', status: 'D', size: { h: 22.85, w: 30, uom: 'cm' }, instock: [ { warehouse: 'A', qty: 40}]}"),
Document.parse("{ item: 'postcard', status: 'A', size: { h: 10, w: 15.25, uom: 'cm' }, "
+ "instock: [ { warehouse: 'B', qty: 15 }, { warehouse: 'C', qty: 35 } ] }")
));
collection.insertMany(
listOf(
Document("item", "journal")
.append("status", "A")
.append("size", Document("h", 14).append("w", 21).append("uom", "cm"))
.append("instock", listOf(
Document("warehouse", "A").append("qty", 5),
)),
Document("item", "notebook")
.append("status", "A")
.append("size", Document("h", 8.5).append("w", 11).append("uom", "in"))
.append("instock", listOf(
Document("warehouse", "C").append("qty", 5),
)),
Document("item", "paper")
.append("status", "D")
.append("size", Document("h", 8.5).append("w", 11).append("uom", "in"))
.append("instock", listOf(
Document("warehouse", "A").append("qty", 60),
)),
Document("item", "planner")
.append("status", "D")
.append("size", Document("h", 22.85).append("w", 30).append("uom", "cm"))
.append("instock", listOf(
Document("warehouse", "A").append("qty", 40),
)),
Document("item", "postcard")
.append("status", "A")
.append("size", Document("h", 10).append("w", 15.25).append("uom", "cm"))
.append("instock", listOf(
Document("warehouse", "B").append("qty", 15),
Document("warehouse", "C").append("qty", 35)
)),
)
)
await db.inventory.insert_many(
[
{
"item": "journal",
"status": "A",
"size": {"h": 14, "w": 21, "uom": "cm"},
"instock": [{"warehouse": "A", "qty": 5}],
},
{
"item": "notebook",
"status": "A",
"size": {"h": 8.5, "w": 11, "uom": "in"},
"instock": [{"warehouse": "C", "qty": 5}],
},
{
"item": "paper",
"status": "D",
"size": {"h": 8.5, "w": 11, "uom": "in"},
"instock": [{"warehouse": "A", "qty": 60}],
},
{
"item": "planner",
"status": "D",
"size": {"h": 22.85, "w": 30, "uom": "cm"},
"instock": [{"warehouse": "A", "qty": 40}],
},
{
"item": "postcard",
"status": "A",
"size": {"h": 10, "w": 15.25, "uom": "cm"},
"instock": [{"warehouse": "B", "qty": 15}, {"warehouse": "C", "qty": 35}],
},
]
)
await db.collection('inventory').insertMany([
{
item: 'journal',
status: 'A',
size: { h: 14, w: 21, uom: 'cm' },
instock: [{ warehouse: 'A', qty: 5 }]
},
{
item: 'notebook',
status: 'A',
size: { h: 8.5, w: 11, uom: 'in' },
instock: [{ warehouse: 'C', qty: 5 }]
},
{
item: 'paper',
status: 'D',
size: { h: 8.5, w: 11, uom: 'in' },
instock: [{ warehouse: 'A', qty: 60 }]
},
{
item: 'planner',
status: 'D',
size: { h: 22.85, w: 30, uom: 'cm' },
instock: [{ warehouse: 'A', qty: 40 }]
},
{
item: 'postcard',
status: 'A',
size: { h: 10, w: 15.25, uom: 'cm' },
instock: [
{ warehouse: 'B', qty: 15 },
{ warehouse: 'C', qty: 35 }
]
}
]);
$db->coll("inventory")->insert_many(
[
{
item => "journal",
status => "A",
size => { h => 14, w => 21, uom => "cm" },
instock => [ { warehouse => "A", qty => 5 } ]
},
{
item => "notebook",
status => "A",
size => { h => 8.5, w => 11, uom => "in" },
instock => [ { warehouse => "C", qty => 5 } ]
},
{
item => "paper",
status => "D",
size => { h => 8.5, w => 11, uom => "in" },
instock => [ { warehouse => "A", qty => 60 } ]
},
{
item => "planner",
status => "D",
size => { h => 22.85, w => 30, uom => "cm" },
instock => [ { warehouse => "A", qty => 40 } ]
},
{
item => "postcard",
status => "A",
size => { h => 10, w => 15.25, uom => "cm" },
instock => [
{ warehouse => "B", qty => 15 },
{ warehouse => "C", qty => 35 }
]
}
]
);
$insertManyResult = $db->inventory->insertMany([
[
'item' => 'journal',
'status' => 'A',
'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'],
'instock' => [
['warehouse' => 'A', 'qty' => 5],
],
],
[
'item' => 'notebook',
'status' => 'A',
'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
'instock' => [
['warehouse' => 'C', 'qty' => 5],
],
],
[
'item' => 'paper',
'status' => 'D',
'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'],
'instock' => [
['warehouse' => 'A', 'qty' => 60],
],
],
[
'item' => 'planner',
'status' => 'D',
'size' => ['h' => 22.85, 'w' => 30, 'uom' => 'cm'],
'instock' => [
['warehouse' => 'A', 'qty' => 40],
],
],
[
'item' => 'postcard',
'status' => 'A',
'size' => ['h' => 10, 'w' => 15.25, 'uom' => 'cm'],
'instock' => [
['warehouse' => 'B', 'qty' => 15],
['warehouse' => 'C', 'qty' => 35],
],
],
]);
db.inventory.insert_many(
[
{
"item": "journal",
"status": "A",
"size": {"h": 14, "w": 21, "uom": "cm"},
"instock": [{"warehouse": "A", "qty": 5}],
},
{
"item": "notebook",
"status": "A",
"size": {"h": 8.5, "w": 11, "uom": "in"},
"instock": [{"warehouse": "C", "qty": 5}],
},
{
"item": "paper",
"status": "D",
"size": {"h": 8.5, "w": 11, "uom": "in"},
"instock": [{"warehouse": "A", "qty": 60}],
},
{
"item": "planner",
"status": "D",
"size": {"h": 22.85, "w": 30, "uom": "cm"},
"instock": [{"warehouse": "A", "qty": 40}],
},
{
"item": "postcard",
"status": "A",
"size": {"h": 10, "w": 15.25, "uom": "cm"},
"instock": [{"warehouse": "B", "qty": 15}, {"warehouse": "C", "qty": 35}],
},
]
)
client[:inventory].insert_many([{ item: 'journal',
status: 'A',
size: { h: 14, w: 21, uom: 'cm' },
instock: [ { warehouse: 'A', qty: 5 }] },
{ item: 'notebook',
status: 'A',
size: { h: 8.5, w: 11, uom: 'in' },
instock: [ { warehouse: 'C', qty: 5 }] },
{ item: 'paper',
status: 'D',
size: { h: 8.5, w: 11, uom: 'in' },
instock: [ { warehouse: 'A', qty: 60 }] },
{ item: 'planner',
status: 'D',
size: { h: 22.85, w: 30, uom: 'cm' },
instock: [ { warehouse: 'A', qty: 40 }] },
{ item: 'postcard',
status: 'A',
size: { h: 10, w: 15.25, uom: 'cm' },
instock: [ { warehouse: 'B', qty: 15 },
{ warehouse: 'C', qty: 35 }] }])
collection.insertMany(Seq(
Document("""{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] }"""),
Document("""{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] }"""),
Document("""{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] }"""),
Document("""{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] }"""),
Document("""{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" },
instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }""")
)).execute()

如果未指定投影文档,db.collection.find() 方法将返回匹配文档中的所有字段。

如果未指定投影文档,Compass 将返回匹配文档中的所有字段。

如果您未指定投影过滤器,则 MongoCollection.Find() 方法将返回匹配文档中的所有字段。

如果未指定 投影 ,则 com.mongodb.reactivestreams. 客户端.MongoCollection.find 方法返回匹配文档中的所有字段。

如果未指定投影,则 com.mongodb.client.MongoCollection.find 方法将返回匹配文档中的所有字段。

如果未指定 投影 文档,则 MongoCollection.find()方法返回匹配文档中的所有字段。

如果未指定投影文档,find() 方法将在匹配的文档中生成所有字段。

如果未指定投影文档,则 find() 方法将在匹配的文档中生成所有字段。

如果未指定投影文档,则 find() 方法将在匹配的文档中生成所有字段。

如果您未指定投影文档,则 find 方法将返回匹配文档中的所有字段。

如果未指定投影文档,则 find() 方法将返回匹配文档中的所有字段。

如果未指定投影,则 collection.find() 方法返回匹配文档中的所有字段。

如下示例将返回 inventory 集合中的所有文档的所有字段,其中的 status 等于 "A"

db.inventory.find( { status: "A" } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Find(连接)。

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"));
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{{"status", "A"}},
)
FindPublisher<Document> findPublisher = collection.find(eq("status", "A"));
FindIterable<Document> findIterable = collection.find(eq("status", "A"));
val findFlow = collection
.find(eq("status", "A"))
cursor = db.inventory.find({"status": "A"})
const cursor = db.collection('inventory').find({
status: 'A'
});
$cursor = $db->coll("inventory")->find( { status => "A" } );
$cursor = $db->inventory->find(['status' => 'A']);
cursor = db.inventory.find({"status": "A"})
client[:inventory].find(status: 'A')
var findObservable = collection.find(equal("status", "A"))

此操作对应于以下 SQL 语句:

SELECT * from inventory WHERE status = "A"

通过在投影文档中将<field>设置为1,投影可以显式包含多个字段。以下操作会返回与查询匹配的所有文档。 在结果集中,匹配文档中仅返回itemstatus和默认情况下的_id字段。

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1 }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1), "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);

此外,请务必根据需要调用以下方法来清理所有打开的资源:

var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status");
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A")).projection(include("item", "status"));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A")).projection(include("item", "status"));
val findFlow = collection
.find(eq("status", "A")).projection(include("item", "status"))
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1})
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1 });
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { item => 1, status => 1 } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1]],
);
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1})
client[:inventory].find({ status: 'A' },
projection: { item: 1, status: 1 })
findObservable = collection.find(equal("status", "A")).projection(include("item", "status"))

此操作对应于以下 SQL 语句:

SELECT _id, item, status from inventory WHERE status = "A"

您可通过在投影中将其设为 0 以便从结果中删除 _id 字段,如以下示例所示:

db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, _id: 0 }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"_id", BCON_INT64 (0), "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Exclude("_id");
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"_id", 0},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), excludeId()));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), excludeId()));
val findFlow = collection
.find(eq("status", "A")).projection(fields(include("item", "status"), excludeId()))
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "_id": 0})
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, _id: 0 });
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { item => 1, status => 1, "_id" => 0 } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1, '_id' => 0]],
);
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "_id": 0})
client[:inventory].find({ status: 'A' },
projection: { item: 1, status: 1, _id: 0 })
findObservable = collection.find(equal("status", "A"))
.projection(fields(include("item", "status"), excludeId()))

此操作对应于以下 SQL 语句:

SELECT item, status from inventory WHERE status = "A"

注意

_id 字段之外,您无法在投影文档中合并包含与排除声明。

您可以使用投影来排除特定字段,而不必在匹配文档中列出要返回的字段。以下示例将在匹配文档中返回除 statusinstock 字段之外的所有字段:

db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { status: 0, instock: 0 }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "status", BCON_INT64 (0),
"instock", BCON_INT64 (0), "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Exclude("status").Exclude("instock");
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"status", 0},
{"instock", 0},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A")).projection(exclude("item", "status"));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A")).projection(exclude("item", "status"));
val findFlow = collection
.find(eq("status", "A")).projection(exclude("item", "status"))
cursor = db.inventory.find({"status": "A"}, {"status": 0, "instock": 0})
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ status: 0, instock: 0 });
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { status => 0, instock => 0 } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['status' => 0, 'instock' => 0]],
);
cursor = db.inventory.find({"status": "A"}, {"status": 0, "instock": 0})
client[:inventory].find({ status: 'A' },
projection: { status: 0, instock: 0 })
findObservable = collection.find(equal("status", "A")).projection(exclude("item", "status"))

注意

_id 字段之外,您无法在投影文档中合并包含与排除声明。

您可以返回嵌入式文档中的特定字段。使用点符号引用嵌入式字段,并在投影文档中设为 1

以下示例将返回:

  • _id 字段(默认返回)、

  • item 字段。

  • status 字段。

  • size 文档中的 uom 字段。

uom 字段仍会嵌入在 size 文档中。

db.inventory.find(
{ status: "A" },
{ item: 1, status: 1, "size.uom": 1 }
)
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, "size.uom": 1 }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"size.uom", BCON_INT64 (1), "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Include("size.uom");
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"size.uom", 1},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A")).projection(include("item", "status", "size.uom"));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A")).projection(include("item", "status", "size.uom"));
val findFlow = collection
.find(eq("status", "A")).projection(include("item", "status", "size.uom"))
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "size.uom": 1})
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, 'size.uom': 1 });
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { item => 1, status => 1, "size.uom" => 1 } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1, 'size.uom' => 1]],
);
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "size.uom": 1})
client[:inventory].find({ status: 'A' },
projection: { 'item' => 1, 'status' => 1, 'size.uom' => 1 })
findObservable = collection.find(equal("status", "A")).projection(include("item", "status", "size.uom"))

您还可以使用嵌套表单指定嵌入式字段。例如:{ item: 1, status: 1, size: { uom: 1 } }

您可以隐藏嵌入式文档中的特定字段。使用点符号引用投影文档中的嵌入式字段,并设为 0

以下示例将指定一个投影来排除 size 文档内的 uom 字段。所有其他字段均会在匹配的文档中返回:

db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { "size.uom": 0 }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "size.uom", BCON_INT64 (0), "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Exclude("size.uom");
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"size.uom", 0},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A")).projection(exclude("size.uom"));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A")).projection(exclude("size.uom"));
val findFlow = collection
.find(eq("status", "A")).projection(exclude("size.uom"))
cursor = db.inventory.find({"status": "A"}, {"size.uom": 0})
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 'size.uom': 0 });
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { "size.uom" => 0 } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['size.uom' => 0]],
);
cursor = db.inventory.find({"status": "A"}, {"size.uom": 0})
client[:inventory].find({ status: 'A' },
projection: { 'size.uom' => 0 })
findObservable = collection.find(equal("status", "A")).projection(exclude("size.uom"))

您还可以使用嵌套表单指定嵌入式字段。例如:{ size: { uom: 0 } }

使用点符号来投影嵌入数组中的文档内的特定字段。

以下示例指定要返回的投影:

  • _id 字段(默认返回)、

  • item 字段。

  • status 字段。

  • 嵌入到 instock 数组中的文档内的 qty 字段。

db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, "instock.qty": 1 }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"instock.qty", BCON_INT64 (1), "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Include("instock.qty");
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"instock.qty", 1},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A")).projection(include("item", "status", "instock.qty"));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A")).projection(include("item", "status", "instock.qty"));
val findFlow = collection
.find(eq("status", "A")).projection(include("item", "status", "instock.qty"))
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "instock.qty": 1})
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, 'instock.qty': 1 });
$cursor = $db->coll("inventory")->find( { status => "A" },
{ projection => { item => 1, status => 1, "instock.qty" => 1 } } );
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1, 'instock.qty' => 1]],
);
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "instock.qty": 1})
client[:inventory].find({ status: 'A' },
projection: {'item' => 1, 'status' => 1, 'instock.qty' => 1 })
findObservable = collection.find(equal("status", "A")).projection(include("item", "status", "instock.qty"))

对于包含数组的字段,MongoDB 提供了用来操作数组的以下投影操作符:$elemMatch$slice$

如下示例使用 $slice 投影操作符返回 instock 数组中的最后一个元素:

db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, instock: { $slice: -1 } }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"instock", "{",
"$slice", BCON_INT64 (-1),
"}", "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Slice("instock", -1);
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"instock", bson.D{
{"$slice", -1},
}},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 FindFlow。投影()方法更改为find()方法。 该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

val findFlow = collection
.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, instock: { $slice: -1 } });
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { item => 1, status => 1, instock => { '$slice' => -1 } } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1, 'instock' => ['$slice' => -1]]],
);
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
client[:inventory].find({ status: 'A' },
projection: {'item' => 1,
'status' => 1,
'instock' => { '$slice' => -1 } })
findObservable = collection.find(equal("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))

$elemMatch$slice$唯一可用于投影要包含在返回数组中的特定元素的操作符。 例如,您无法使用数组索引来投影特定的数组元素;例如 { "instock.0": 1 }投影不对包含第一个元素的数组进行投影。

对于包含数组的字段,MongoDB 提供了用来操作数组的以下投影操作符:$elemMatch$slice$

如下示例使用 $slice 投影操作符返回 instock 数组中的最后一个元素:

db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, instock: { $slice: -1 } }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"instock", "{",
"$slice", BCON_INT64 (-1),
"}", "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Slice("instock", -1);
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"instock", bson.D{
{"$slice", -1},
}},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 FindFlow。投影()方法更改为find()方法。 该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

val findFlow = collection
.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, instock: { $slice: -1 } });
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { item => 1, status => 1, instock => { '$slice' => -1 } } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1, 'instock' => ['$slice' => -1]]],
);
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
client[:inventory].find({ status: 'A' },
projection: {'item' => 1,
'status' => 1,
'instock' => { '$slice' => -1 } })
findObservable = collection.find(equal("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))

对于包含数组的字段,MongoDB 提供了用来操作数组的以下投影操作符:$elemMatch$slice$

如下示例使用 $slice 投影操作符返回 instock 数组中的最后一个元素:

db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, instock: { $slice: -1 } }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"instock", "{",
"$slice", BCON_INT64 (-1),
"}", "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Slice("instock", -1);
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"instock", bson.D{
{"$slice", -1},
}},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 FindFlow。投影()方法更改为find()方法。 该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

val findFlow = collection
.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, instock: { $slice: -1 } });
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { item => 1, status => 1, instock => { '$slice' => -1 } } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1, 'instock' => ['$slice' => -1]]],
);
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
client[:inventory].find({ status: 'A' },
projection: {'item' => 1,
'status' => 1,
'instock' => { '$slice' => -1 } })
findObservable = collection.find(equal("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))

$elemMatch$slice$唯一可用于项目要包含在返回大量中的特定元素的操作符。

例如,以下操作不会使用第一个元素项目数组:

Builders<BsonDocument>.Projection.Include("instock.0")

对于包含数组的字段,MongoDB 提供了用来操作数组的以下投影操作符:$elemMatch$slice$

如下示例使用 $slice 投影操作符返回 instock 数组中的最后一个元素:

db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, instock: { $slice: -1 } }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"instock", "{",
"$slice", BCON_INT64 (-1),
"}", "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Slice("instock", -1);
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"instock", bson.D{
{"$slice", -1},
}},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 FindFlow。投影()方法更改为find()方法。 该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

val findFlow = collection
.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, instock: { $slice: -1 } });
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { item => 1, status => 1, instock => { '$slice' => -1 } } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1, 'instock' => ['$slice' => -1]]],
);
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
client[:inventory].find({ status: 'A' },
projection: {'item' => 1,
'status' => 1,
'instock' => { '$slice' => -1 } })
findObservable = collection.find(equal("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))

$elemMatch$slice$唯一可用于投影要包含在返回数组中的特定元素的操作符。 例如,您无法使用数组索引来投影特定的数组元素;例如 include("instock.0")投影不对包含第一个元素的数组进行投影。

对于包含数组的字段,MongoDB 提供了用来操作数组的以下投影操作符:$elemMatch$slice$

如下示例使用 $slice 投影操作符返回 instock 数组中的最后一个元素:

db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, instock: { $slice: -1 } }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"instock", "{",
"$slice", BCON_INT64 (-1),
"}", "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Slice("instock", -1);
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"instock", bson.D{
{"$slice", -1},
}},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 FindFlow。投影()方法更改为find()方法。 该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。

val findFlow = collection
.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ item: 1, status: 1, instock: { $slice: -1 } });
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { item => 1, status => 1, instock => { '$slice' => -1 } } }
);
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['item' => 1, 'status' => 1, 'instock' => ['$slice' => -1]]],
);
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock": {"$slice": -1}}
)
client[:inventory].find({ status: 'A' },
projection: {'item' => 1,
'status' => 1,
'instock' => { '$slice' => -1 } })
findObservable = collection.find(equal("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)))

$elemMatch$slice$唯一可用于投影要包含在返回数组中的特定元素的操作符。 例如,您无法使用数组索引来投影特定的数组元素;例如 include("instock.0")投影不对包含第一个元素的数组进行投影。

对于包含数组的字段,MongoDB 提供了用来操作数组的以下投影操作符:$elemMatch$slice$

如下示例使用 $slice 投影操作符返回 instock 数组中的最后一个元素:

db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
  1. 将以下表达式复制到 Filter 字段中:

    { status: "A" }
  2. 单击 Options 打开其他查询选项。

  3. 将以下表达式复制到 Project 字段中:

    { item: 1, status: 1, instock: { $slice: -1 } }
  4. 单击 Find(连接)。

mongoc_collection_t *collection;
bson_t *filter;
bson_t *opts;
mongoc_cursor_t *cursor;
collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("status", BCON_UTF8 ("A"));
opts = BCON_NEW ("projection", "{", "item", BCON_INT64 (1),
"status", BCON_INT64 (1),
"instock", "{",
"$slice", BCON_INT64 (-1),
"}", "}");
cursor = mongoc_collection_find_with_opts (collection, filter, opts, NULL);
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var projection = Builders<BsonDocument>.Projection.Include("item").Include("status").Slice("instock", -1);
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
projection := bson.D{
{"item", 1},
{"status", 1},
{"instock", bson.D{
{"$slice", -1},
}},
}
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"status", "A"},
},
options.Find().SetProjection(projection),
)
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));

要指定投影文档,请链接 com.mongodb。客户端.FindIterable。投影 方法更改为find 方法。该示例使用 com.mongodb。 客户端.model.Projections 类来创建投影文档。