Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Projection of a subdocument

Tags:

mongodb

How do I project the author's first name using a MongoDB query

{
 name: "Wings Of Fire",
 author:
 {
  first: "Abdul",
  last: "Kalam"
 }
}
like image 524
Honorificabilitudinitas Avatar asked Feb 25 '13 23:02

Honorificabilitudinitas


People also ask

How do I add a projection in MongoDB query?

Adding a new projection queryIn the MongoDB integration, click the Collections to Replicate tab. Navigate to the desired collection. Click the checkbox to the left of the collection to set it to replicate.

What is a projection in MongoDB terminology?

In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.

How do I display only one column in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});


1 Answers

You can use dot notation in the field selector to project subdocument fields. In the shell:

db.test.find({}, {'author.first': 1})
like image 137
JohnnyHK Avatar answered Oct 08 '22 01:10

JohnnyHK