On this page
从查询返回的项目字段
默认情况下,MongoDB 中的查询返回匹配文档中的所有字段。要限制 MongoDB 发送给应用程序的数据量,可以包含projection文档以指定或限制要返回的字段。
This page provides examples of query operations with projection using the db.collection.find() method in the mongo shell. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
db.inventory.insertMany( [
{ item: "journal", 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 } ] }
]);
You can run the operation in the web shell below:
This page provides examples of query operations with projection using MongoDB Compass. The examples on this page use the inventory
collection. Populate the inventory
collection with the following documents:
[
{ 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 } ] }
]
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
This page provides examples of query operations with projection using the pymongo.collection.Collection.find() method in the PyMongo Python driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
db.inventory.insert_many([
{"item": "journal",
"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}]}])
This page provides examples of query operations with projection using the com.mongodb.client.MongoCollection.find method in the MongoDB Java Synchronous Driver.
Tip
The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.
The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
collection.insertMany(asList(
Document.parse("{ item: 'journal', 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 } ] }")
));
This page provides examples of query operations with projection using the Collection.find() method in the MongoDB Node.js Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
await db.collection('inventory').insertMany([
{
item: 'journal',
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 }]
}
]);
This page provides examples of query operations with projection using the MongoDB\Collection::find() method in the MongoDB PHP Library. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
$insertManyResult = $db->inventory->insertMany([
[
'item' => 'journal',
'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],
],
],
]);
This page provides examples of query operations with projection using the motor.motor_asyncio.AsyncIOMotorCollection.find()
method in the Motor driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
await db.inventory.insert_many([
{"item": "journal",
"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}]}])
This page provides examples of query operations with projection using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.
The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{ item: 'journal', 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 } ] }")
));
This page provides examples of query operations with projection using the MongoCollection.Find() method in the MongoDB C# Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
var documents = new[]
{
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);
This page provides examples of query operations with projection using the MongoDB::Collection::find() method in the MongoDB Perl Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
$db->coll("inventory")->insert_many(
[
{
item => "journal",
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 }
]
}
]
);
This page provides examples of query operations with projection using the Mongo::Collection#find() method in the MongoDB Ruby Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
client[:inventory].insert_many([{ item: 'journal',
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 }] }])
This page provides examples of query operations with projection using the collection.find() method in the MongoDB Scala Driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the following:
collection.insertMany(Seq(
Document("""{ item: "journal", 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()
返回匹配文档中的所有字段
If you do not specify a projection document, the db.collection.find() method returns all fields in the matching documents.
If you do not specify a projection document, Compass returns all fields in the matching documents.
If you do not specify a projection document, the find() method returns all fields in the matching documents.
If you do not specify a projection, the com.mongodb.client.MongoCollection.find method returns all fields in the matching documents.
If you do not specify a projection document, the find() method yields all fields in the matching documents.
If you do not specify a projection document, the find() method returns all fields in the matching documents.
If you do not specify a projection, the com.mongodb.reactivestreams.client.MongoCollection.find method returns all fields in the matching documents.
If you do not specify a projection filter, the MongoCollection.Find() method returns all fields in the matching documents.
If you do not specify a projection document, the find() method returns all fields in the matching documents.
If you do not specify a projection document, the find() method returns all fields in the matching documents.
If you do not specify a projection, the collection.find() method returns all fields in the matching documents.
下面的示例返回inventory
集合中所有文档的所有字段,其中status
等于"A"
:
db.inventory.find( { status: "A" } )
Copy the following expression into the Filter bar and click Find:
{ status: "A" }
cursor = db.inventory.find({"status": "A"})
FindIterable<Document> findIterable = collection.find(eq("status", "A"));
const cursor = db.collection('inventory').find({
status: 'A'
});
$cursor = $db->inventory->find(['status' => 'A']);
cursor = db.inventory.find({"status": "A"})
FindPublisher<Document> findPublisher = collection.find(eq("status", "A"));
var filter = Builders<BsonDocument>.Filter.Eq("status", "A");
var result = collection.Find(filter).ToList();
$cursor = $db->coll("inventory")->find( { status => "A" } );
client[:inventory].find(status: 'A')
var findObservable = collection.find(equal("status", "A"))
该操作对应于以下 SQL 语句:
SELECT * from inventory WHERE status = "A"
仅返回指定字段和_id 字段
通过在投影文档中将<field>
设置为1
,投影可以显式包括多个字段。以下操作返回与查询匹配的所有文档。在结果集中,只有item
,status
以及默认情况下_id
字段返回到匹配的文档中。
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
Copy the following expression into the Filter bar:
{ status: "A" }
Copy the following expression into the Project bar:
{ item: 1, status: 1 }
Click Find.
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A")).projection(include("item", "status"));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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})
findPublisher = collection.find(eq("status", "A")).projection(include("item", "status"));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { 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"
禁止_id 字段
您可以通过在投影中将其排除范围<field>
设置为0
来从结果中删除_id
字段,如以下示例所示:
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
Copy the following expression into the Filter bar:
{ status: "A" }
Copy the following expression into the Project bar:
{ item: 1, status: 1, _id: 0 }
Click Find.
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "_id": 0})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), excludeId()));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), excludeId()));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { 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"
返回所有除外字段
您可以使用投影排除特定字段,而不是列出要在匹配文档中返回的字段。下面的示例返回匹配文档中status
和instock
字段以外的所有字段:
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
Copy the following expression into the Filter bar:
{ status: "A" }
Copy the following expression into the Project bar:
{ status: 0, instock: 0 }
Click Find.
cursor = db.inventory.find(
{"status": "A"}, {"status": 0, "instock": 0})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A")).projection(exclude("item", "status"));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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})
findPublisher = collection.find(eq("status", "A")).projection(exclude("item", "status"));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { status => 0, instock => 0 } }
);
client[:inventory].find({ status: 'A' },
projection: { status: 0, instock: 0 })
findObservable = collection.find(equal("status", "A")).projection(exclude("item", "status"))
Note
除_id
字段外,您无法在投影文档中合并包含和排除语句。
返回嵌入式文档中的特定字段
您可以返回嵌入式文档中的特定字段。使用dot notation引用嵌入字段,并在投影文档中将其设置为1
。
以下示例返回:
_id
字段(默认情况下返回),item
字段,status
字段,size
文档中的uom
字段。
uom
字段仍嵌入在size
文档中。
db.inventory.find(
{ status: "A" },
{ item: 1, status: 1, "size.uom": 1 }
)
Copy the following expression into the Filter bar:
{ status: "A" }
Copy the following expression into the Project bar:
{ item: 1, status: 1, "size.uom": 1 }
Click Find.
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "size.uom": 1})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A")).projection(include("item", "status", "size.uom"));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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})
findPublisher = collection.find(eq("status", "A")).projection(include("item", "status", "size.uom"));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { 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"))
禁止嵌入文档中的特定字段
您可以隐藏嵌入式文档中的特定字段。使用dot notation引用投影文档中的嵌入字段并将其设置为0
。
下面的示例指定一个投影,以排除size
文档内的uom
字段。其他所有字段均在匹配的文档中返回:
db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
Copy the following expression into the Filter bar:
{ status: "A" }
Copy the following expression into the Project bar:
{ "size.uom": 0 }
Click Find.
cursor = db.inventory.find({"status": "A"}, {"size.uom": 0})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A")).projection(exclude("size.uom"));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 'size.uom': 0 });
$cursor = $db->inventory->find(
['status' => 'A'],
['projection' => ['size.uom' => 0]]
);
cursor = db.inventory.find({"status": "A"}, {"size.uom": 0})
findPublisher = collection.find(eq("status", "A")).projection(exclude("size.uom"));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" }, { projection => { "size.uom" => 0 } }
);
client[:inventory].find({ status: 'A' },
projection: { 'size.uom' => 0 })
findObservable = collection.find(equal("status", "A")).projection(exclude("size.uom"))
在数组中的嵌入式文档上投影
使用dot notation将特定字段投影在嵌入数组的文档中。
以下示例指定要返回的投影:
_id
字段(默认情况下返回),item
字段,status
字段,嵌入在
instock
数组中的文档中的qty
字段。
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
Copy the following expression into the Filter bar:
{ status: "A" }
Copy the following expression into the Project bar:
{ item: 1, status: 1, "instock.qty": 1 }
Click Find.
cursor = db.inventory.find(
{"status": "A"}, {"item": 1, "status": 1, "instock.qty": 1})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A")).projection(include("item", "status", "instock.qty"));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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})
findPublisher = collection.find(eq("status", "A")).projection(include("item", "status", "instock.qty"));
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();
$cursor = $db->coll("inventory")->find( { status => "A" },
{ projection => { 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"))
返回数组中的项目特定数组元素
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch, $slice, and $.
The following example uses the $slice projection operator to return the last element in the instock
array:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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 }
投影不会*用第一个元素投影数组。
[!tab|label:Compass]
当前,MongoDB Compass 不支持任何用于在返回的数组中投影特定数组元素的方法。
有关 MongoDB Compass 中投影的更多详细信息和示例,请参阅 Compass Query Bar文档。
[!tab|label:Python]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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 }
投影不会*用第一个元素投影数组。
[!tab|label:Java (Sync)]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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")
投影不会*用第一个元素投影数组。
[!tab|label:Node.js]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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 }
投影不会*用第一个元素投影数组。
[!tab|label:PHP]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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 ]
投影不会*用第一个元素投影数组。
[!tab|label:Java (Async)]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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")
投影不会*用第一个元素投影数组。
[!tab|label:C#]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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")
[!tab|label:Perl]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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 }
投影不会*用第一个元素投影数组。
[!tab|label:Ruby]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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 }
投影不会*用第一个元素投影数组。
[!tab|label:Scala]
对于包含数组的字段,MongoDB 提供以下用于操纵数组的投影运算符:$elemMatch,$slice和$。
下面的示例使用$slice投影运算符返回instock
数组中的最后一个元素:
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
cursor = db.inventory.find(
{"status": "A"},
{"item": 1, "status": 1, "instock": {"$slice": -1}})
To specify a projection document, chain the com.mongodb.client.FindIterable.projection method to the find
method. The example uses the com.mongodb.client.model.Projections class to create the projection documents.
findIterable = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
const cursor = db
.collection('inventory')
.find({
status: 'A'
})
.project({ 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}})
findPublisher = collection.find(eq("status", "A"))
.projection(fields(include("item", "status"), slice("instock", -1)));
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();
$cursor = $db->coll("inventory")->find(
{ status => "A" },
{ projection => { 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")
投影不会*用第一个元素投影数组。