Syntax to return multiple fields

db.zips.findOne({“state”:“WA”},{“city”:1},{“state”:1})
returns the ID and city but does not show other fields
Example equivalent SQL "Select city,state from zips where state =‘WA’ "

If add more fields to view , the only reply only gives the first field .
example ; db.zips.findOne({“state”:“WA”},{“city”:1},{“pop”:1}) give city but not pop

How is read of multiple field done ?
thanks !

If you look at the findOne documentation you will find that the syntax should be:

db.zips.findOne( Query , Projection )

In

you have 3 parameters rather than 2. Being JS, the 3rd is ignored so what you do is equivalent to

db.zips.findOne({“state”:“WA”},{“city”:1})

You are doing the same mistake in

The object {pop:1} being the 3rd parameter is ignored. It should be {city:1,pop1}.

1 Like

Thanks ! excellent - yes db.zips.findOne({“state”:“WA”},{“city”:1,“pop”:1,zip:1}) brings back all 3 values. Thanks for explaining the 2 parameters - it will be key to understanding other usages !

1 Like