1

I have this kind of elements:

{  "_id" : 1,
  "docs" : [
    { "key": "blah", "count": 2},
    { "key": "wow", "count": 10}
  ]
},
{  "_id" : 2,
  "docs" : [
    { "key": "blah", "count": 11}
}

I want to retrive elements which has NOT the docs.key == "wow" defined. In this case the element "_id": 2.

Whith this query I get the opposite:

db.getCollection('myCollection').find(
  {
    "docs.key": "wow"
  }
);

I have tried combinations of $exists and aggregate but I don't find the proper solution.

fguillen
  • 36,125
  • 23
  • 149
  • 210
  • Related but not the same: http://stackoverflow.com/q/5719408/316700, http://stackoverflow.com/q/16221599/316700 because in my example I an looking for an complex element into an Array. – fguillen Mar 14 '16 at 17:44

2 Answers2

4

You could do :

db.getCollection('myCollection').find({
    "docs.key": {$ne: "wow"}
});
Till
  • 4,183
  • 3
  • 16
  • 18
0

Have you tried the $ne operator

db.getCollection('myCollection').find(
  {
    "docs.key": {$ne : "wow"}
  }
);
Rahul
  • 15,979
  • 4
  • 42
  • 63