user1934044
user1934044

Reputation: 526

mongodb query array of elemnts in a field

I have array of _id's like

arr=["xxxx","yyyyy",zzzz"]

NOw I want to return all the docs of these ids from a collection like

coll.find({_id:{$in:{arr}})//must return xxx.yyy.zzz docs

this is returning all fields from collection, How to do this?

these are my docs

  {_id:"xxx",
         bvalue:"val",
         cval:"val"
    }
     {_id:"yyy",
         bvalue:"val",
         cval:"val"
    }
     {_id:"zzz",
         bvalue:"val",
         cval:"val"
    }

I need a query which returns all documents with id's in array In my array I have id's xxx,yyy,zzz so I want all these docs to return

Upvotes: 0

Views: 47

Answers (1)

wdberkeley
wdberkeley

Reputation: 11671

The query is doing exactly what it is supposed to - returning the documents that match your query criteria. If you just want the _id's back, use projection:

db.coll.find({ "_id" : { "$in" : arr }, { "_id" : 1 })

Upvotes: 2

Related Questions