Reputation: 515
I have a query,
SomeCollection.collection.find(:$or => [{tags:'cool'},{tags:'awesome'},{tags:'neat'},...])
I'd like to modify the opposite collection. So all the elements except the ones found in the previous statement. So in this example, I'd like all the results that are not tags:'cool' || tags:'awesome' || tags:'neat' || ...
.
I tried SomeCollection.collection.find(:$not => {:$or => [{tags:'cool'},{tags:'awesome'},{tags:'neat'},...]})
which gave syntax errors. I am also considering SomeCollection.collection.find(:$and => [{tags:{:$ne => 'cool'}}, {tags:{:$ne => 'awesome'}}, {tags:{:$ne => 'neat'}}])
which is the explicit inverse and gives me the correct answer. Is there a short hand way to get the inverse collection?
Upvotes: 1
Views: 1208
Reputation: 1932
You can use $ne in the query.
SomeCollection.collection.find($or : [ {tags: {$ne:'cool}}, ..]
Since the values you are comparing values for the same field, you can use even $nin. The query would now look like,
SomeCollection.collection.find($or : [ {tags : {$nin : ['cool','awesome',..]} .. ]
Upvotes: 4