Lets say I have a collection with two documents in…
{"_id": ..., "msg": "hello world"}
{"_id": ..., "name": "bob dylan"}
and a query…
db.collection.find({}, {"text": 1})
Why does this return both documents? Is there a way to return only when the msg field exists?
Your query returns both documents because your criteria is empty (
{}):If you only want to find documents where the
msgfield exists, you can use$exists:Note that for the second parameter to
find(), you are only requesting the value of thetextfield to be displayed.