0

The users table contains an items array which is just a collection of Item ObjectId's.

Users

{
  state: 'active',
  items: [ObjectId("4ecc05e55dd98a436ddcc47c"), ObjectId("4ecc05e55dd98a436ddcc47d")]
}

Items

{
 name: 'weapon',
 creator: 'mark'
}

I want to write a query that finds all users which have an item with creator 'mark'. Is that possible in one query? I tried something like this but it does not work.

{
  'items.creator': 'mark
};
bejm
  • 499
  • 2
  • 6
  • 10
  • see mongoose population http://mongoosejs.com/docs/populate.html – Sarath Nair Dec 18 '15 at 05:22
  • @SarathNair the problem with populate is that I need all the items to return with the user with a query, but I need to filter based on items as well. – bejm Dec 18 '15 at 05:48
  • Population will not help you here. Possible dupe of http://stackoverflow.com/questions/19380738/mongoose-nested-query-on-model-by-field-of-its-referenced-model – JohnnyHK Dec 19 '15 at 04:27

1 Answers1

0

Firstly, you need to have the items collection either as embedded or reference. With this structure in mind, we can write the query as

db.Users.find({"items.creator" : 'mark'})

Nikhil
  • 83
  • 6