Project Fields to Return from Query

By default, queries in MongoDB return all fields in matching documents. To limit the amount of data that MongoDB sends to applications, you can include a projection document to specify or restrict fields to return.

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

https://mws.mongodb.com/?version=3.6
[
  { 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"<
首页