- Project Fields to Return from Query
- Return All Fields in Matching Documents
- Return the Specified Fields and the _id Field Only
- Suppress _id Field
- Return All But the Excluded Fields
- Return Specific Fields in Embedded Documents
- Suppress Specific Fields in Embedded Documents
- Projection on Embedded Documents in an Array
- Project Specific Array Elements in the Returned Array
Project Fields to Return from Query
This page provides examples in:
By default, queries in MongoDB return all fields in matching documents.To limit the amount of data that MongoDB sends to applications, you caninclude a projection document to specify or restrict fields toreturn.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
This page provides examples of query operations with projection using thedb.collection.find()
method in themongo
shell. The examples on this page use theinventory
collection. To populate the inventory
collection, run the following:
This page provides examples of query operations with projection usingMongoDB Compass. The examples on thispage use the inventory
collection. Populate theinventory
collection with the following documents:
This page provides examples of query operations with projection using thepymongo.collection.Collection.find()
method in thePyMongoPython driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run thefollowing:
This page provides examples of query operations with projection using thecom.mongodb.client.MongoCollection.find method in the MongoDBJava Synchronous Driver.
Tip
The driver provides com.mongodb.client.model.Filtershelper methods to facilitate the creation of filterdocuments. The examples on this page use these methods tocreate the filter documents.
The examples on this page use the inventory
collection. To populate the inventory
collection, run thefollowing:
This page provides examples of query operations with projection using theCollection.find() method inthe MongoDB Node.js Driver.The examples on this page use the inventory
collection. Topopulate the inventory
collection, run the following:
This page provides examples of query operations with projection using theMongoDB\Collection::find()
method in theMongoDB PHP Library.The examples on this page use the inventory
collection. Topopulate the inventory
collection, run the following:
This page provides examples of query operations with projection using themotor.motor_asyncio.AsyncIOMotorCollection.find()
method in the Motordriver. The examples on this page use the inventory
collection. To populate the inventory
collection, run thefollowing:
This page provides examples of query operations with projection using thecom.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 thefollowing:
This page provides examples of query operations with projection using theMongoCollection.Find()method in theMongoDB C# Driver.The examples on this page use the inventory
collection. Topopulate the inventory
collection, run the following:
This page provides examples of query operations with projection using theMongoDB::Collection::find() methodin theMongoDB Perl Driver.The examples on this page use the inventory
collection. Topopulate the inventory
collection, run the following:
This page provides examples of query operations with projection using theMongo::Collection#find()method in theMongoDB Ruby Driver.The examples on this page use the inventory
collection. Topopulate the inventory
collection, run the following:
This page provides examples of query operations with projection using thecollection.find()(implicite:org.mongodb.scala.bson.DefaultHelper.DefaultsTo[C,TResult],implicitct:scala.reflect.ClassTag[C]):org.mongodb.scala.FindObservable[C]) methodin theMongoDB Scala Driver.The examples on this page use the inventory
collection. Topopulate the inventory
collection, run the following:
This page provides examples of query operations with projection using theCollection.Findfunction in theMongoDB Go Driver.The examples on this page use the inventory
collection. Topopulate the inventory
collection, run the following:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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:
- [
- { 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.
- 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}]}])
- 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 } ] }")
- ));
- 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 }]
- }
- ]);
- $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],
- ],
- ],
- ]);
- 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}]}])
- 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 } ] }")
- ));
- 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);
- $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 }
- ]
- }
- ]
- );
- 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()
- 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.Background(), docs)
Return All Fields in Matching Documents
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Java (Async)
- C#
- Perl
- Ruby
- Scala
If you do not specify a projection document, thedb.collection.find()
method returns all fields in thematching documents.
If you do not specify a projection document, Compassreturns all fields in the matching documents.
If you do not specify a projection document, thefind()
method returnsall fields in the matching documents.
If you do not specify a projection, thecom.mongodb.client.MongoCollection.find method returns allfields in the matching documents.
If you do not specify a projection document, thefind() method yields allfields in the matching documents.
If you do not specify a projection document, thefind()
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, theMongoCollection.Find()method returns all fields in the matching documents.
If you do not specify a projection document, thefind()method returns all fields in the matching documents.
If you do not specify a projection document, thefind()method returns all fields in the matching documents.
If you do not specify a projection, thecollection.find()(implicite:org.mongodb.scala.bson.DefaultHelper.DefaultsTo[C,TResult],implicitct:scala.reflect.ClassTag[C]):org.mongodb.scala.FindObservable[C])method returns all fields in the matching documents.
The following example returns all fields from all documents in theinventory
collection where the status
equals "A"
:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- db.inventory.find( { status: "A" } )
Copy the following expression into the Filter barand 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"))
- cursor, err := coll.Find(
- context.Background(),
- bson.D{{"status", "A"}},
- )
The operation corresponds to the following SQL statement:
- SELECT * from inventory WHERE status = "A"
Return the Specified Fields and the _id Field Only
A projection can explicitly include several fields by setting the<field>
to 1
in the projection document. The followingoperation returns all documents that match the query. In the resultset, only the item
, status
and, by default, the _id
fieldsreturn in the matching documents.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 Projectbar:
- { item: 1, status: 1 }
Click Find.
- cursor = db.inventory.find(
- {"status": "A"}, {"item": 1, "status": 1})
To specify a projection document, chain thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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"))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
The operation corresponds to the following SQL statement:
- SELECT _id, item, status from inventory WHERE status = "A"
Suppress _id Field
You can remove the _id
field from the results by setting it to0
in the projection, as in the following example:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 Projectbar:
- { 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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()))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"_id", 0},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
The operation corresponds to the following SQL statement:
- SELECT item, status from inventory WHERE status = "A"
Note
With the exception of the _id
field, you cannot combine inclusionand exclusion statements in projection documents.
Return All But the Excluded Fields
Instead of listing the fields to return in the matching document, youcan use a projection to exclude specific fields. The following examplewhich returns all fields except for the status
and the instock
fields in the matching documents:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 Projectbar:
- { status: 0, instock: 0 }
Click Find.
- cursor = db.inventory.find(
- {"status": "A"}, {"status": 0, "instock": 0})
To specify a projection document, chain thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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"))
- projection := bson.D{
- {"status", 0},
- {"instock", 0},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
Note
With the exception of the _id
field, you cannot combine inclusionand exclusion statements in projection documents.
Return Specific Fields in Embedded Documents
You can return specific fields in an embedded document. Use thedot notation to refer to the embeddedfield and set to 1
in the projection document.
The following example returns:
- The
_id
field (returned by default), - The
item
field, - The
status
field, - The
uom
field in thesize
document.
The uom
field remains embedded in the size
document.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 Projectbar:
- { 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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"))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"size.uom", 1},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
Suppress Specific Fields in Embedded Documents
You can suppress specific fields in an embedded document. Use thedot notation to refer to the embeddedfield in the projection document and set to 0
.
The following example specifies a projection to exclude the uom
field inside the size
document. All other fields are returned inthe matching documents:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- db.inventory.find(
- { status: "A" },
- { "size.uom": 0 }
- )
Copy the following expression into the Filter bar:
- { status: "A" }
Copy the following expression into the Projectbar:
- { "size.uom": 0 }
Click Find.
- cursor = db.inventory.find({"status": "A"}, {"size.uom": 0})
To specify a projection document, chain thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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"))
- projection := bson.D{
- {"size.uom", 0},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
Projection on Embedded Documents in an Array
Use dot notation to project specificfields inside documents embedded in an array.
The following example specifies a projection to return:
- The
_id
field (returned by default), - The
item
field, - The
status
field, - The
qty
field in the documents embedded in theinstock
array.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 Projectbar:
- { 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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"))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock.qty", 1},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
Project Specific Array Elements in the Returned Array
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- PHP
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.{ "instock.0": 1 }
projection will _not project the arraywith the first element.
Currently, MongoDB Compass does not support any methods forprojecting specific array elements in a returned array.
For additional details and examples on projection in MongoDBCompass, refer to the CompassQuery Bar documentation.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.{ "instock.0": 1 }
projection will _not project the arraywith the first element.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.include("instock.0")
projection will _not project the arraywith the first element.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.{ "instock.0": 1 }
projection will _not project the arraywith the first element.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.[ "instock.0" => 1 ]
projection will _not project the arraywith the first element.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.include("instock.0")
projection will _not project the arraywith the first element.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array.
For example, the following operation will not project the arraywith the first element:
- Builders<BsonDocument>.Projection.Include("instock.0")
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.{ "instock.0" => 1 }
projection will _not project the arraywith the first element.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.{ "instock.0" => 1 }
projection will _not project the arraywith the first element.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.include("instock.0")
projection will _not project the arraywith the first element.
For fields that contain arrays, MongoDB provides the followingprojection operators for manipulating arrays: $elemMatch
,$slice
, and $
.
The following example uses the $slice
projection operatorto return the last element in the instock
array:
- Mongo Shell
- Python
- Java (Sync)
- Node.js
- PHP
- Other
- Motor
- Java (Async)
- C#
- Perl
- Ruby
- Scala
- Go
- 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 thecom.mongodb.client.FindIterable.projection method to thefind
method. The example uses thecom.mongodb.client.model.Projections class to create theprojection 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)))
- projection := bson.D{
- {"item", 1},
- {"status", 1},
- {"instock", bson.D{
- {"$slice", -1},
- }},
- }
- cursor, err := coll.Find(
- context.Background(),
- bson.D{
- {"status", "A"},
- },
- options.Find().SetProjection(projection),
- )
$elemMatch
, $slice
, and$
are the only way to project specific elementsto include in the returned array. For instance, you cannot_project specific array elements using the array index; e.g.include("instock.0")
projection will _not project the arraywith the first element.
See also