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 类来创建投影文档。

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 类来创建投影文档。

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)))

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

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

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

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

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

您可以在查询投影中指定聚合表达式。通过使用聚合表达式,您可以投影新字段并修改现有字段的值。

例如,以下操作使用聚合表达式覆盖 status 字段的值,并投影新字段 areareportNumber

注意

以下示例使用 MongoDB Shell 语法。有关使用聚合的投影的驱动程序示例,请参阅您的驱动程序文档

db.inventory.find(
{ },
{
_id: 0,
item: 1,
status: {
$switch: {
branches: [
{
case: { $eq: [ "$status", "A" ] },
then: "Available"
},
{
case: { $eq: [ "$status", "D" ] },
then: "Discontinued"
},
],
default: "No status found"
}
},
area: {
$concat: [
{ $toString: { $multiply: [ "$size.h", "$size.w" ] } },
" ",
"$size.uom"
]
},
reportNumber: { $literal: 1 }
}
)
[
{
item: 'journal',
status: 'Available',
area: '294 cm',
reportNumber: 1
},
{
item: 'planner',
status: 'Discontinued',
area: '685.5 cm',
reportNumber: 1
},
{
item: 'notebook',
status: 'Available',
area: '93.5 in',
reportNumber: 1
},
{
item: 'paper',
status: 'Discontinued',
area: '93.5 in',
reportNumber: 1
},
{
item: 'postcard',
status: 'Available',
area: '152.5 cm',
reportNumber: 1
}
]

本部分的示例使用的是示例电影数据集。要了解如何将示例数据集加载到您的 MongoDB Atlas 部署中,请参阅加载示例数据

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

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

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

  3. 如果尚未显示,请单击侧边栏中的Clusters

    会显示集群页面。

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

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

  3. 选择 movies 集合。

3
  1. 单击 More Options字段右侧的 。Filter

  2. 指定查询筛选器。

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

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

    { year: 1924 }
4

指定要在查询结果中返回的字段。

将以下项目文档复制到Project栏中:

{ title: 1, plot: 1 }
5

此查询筛选器返回collection中字段与sample_mflix.movies year1924匹配的所有文档的以下字段:

  • _id

  • title

  • plot

MongoDB Atlas 默认返回_id字段。要省略_id字段,请将以下项目文档复制到Project栏中,然后单击Apply

{ title: 1, plot: 1, _id: 0 }

MongoDB 对投影执行额外的限制。有关详细信息,请参阅投影限制

提示

另请参阅:

后退

嵌入式文档数组