0

the documents looks like :

{
  name: 'abc',
  types: [
     {name:'Large',stock:true}, 
     {name:'XLarge',stock:false},
     {name:'XXLarge',stock:true}
  ]
}

I'm trying to figure out the query to return all documents which are out of stock.

Something like : .find({types:{{$nin:{stock:true}}}) Can I somehow do that?

xShirase
  • 11,975
  • 4
  • 53
  • 85
  • Do you want you example document to be returned or not? – undefined_variable Dec 21 '15 at 10:55
  • 1
    Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – styvane Dec 21 '15 at 11:12

1 Answers1

6

You can query using positional operator like this:

db.collection.find({'types.stock':{$ne:true}})

$nin operator is used for finding elements not in a particular array. $ne (not equal to) is a much better operation in your case.

Sarath Nair
  • 2,828
  • 2
  • 21
  • 36