Query for all the docs in the people
collection that have a name
field with a value of 'Tom'
:
db.people.find({name: 'Tom'})
Or just the first one:
db.people.findOne({name: 'Tom'})
You can also specify which fields to return by passing a field selection parameter. The following will exclude the _id
field and only include the age
field:
db.people.find({name: 'Tom'}, {_id: 0, age: 1})
Note: by default, the _id
field will be returned, even if you don't ask for it. If you would like not to get the _id
back, you can just follow the previous example and ask for the _id
to be excluded by specifying _id: 0
(or _id: false
).If you want to find sub record like address object contains country, city, etc.
db.people.find({'address.country': 'US'})
& specify field too if required
db.people.find({'address.country': 'US'}, {'name': true, 'address.city': true})Remember that the result has a `.pretty()` method that pretty-prints resulting JSON:
db.people.find().pretty()