Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB $ne Explanation

The official MongoDB api wrote very little about $ne http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24ne

So when I encountered something like

db.papers.update({"authors cited" : {"$ne" : "Richie"}},
... {$push : {"authors cited" : "Richie"}})

I have no choice but to become utterly confused. Can someone please explain it to me?

like image 754
Max Avatar asked Dec 05 '25 14:12

Max


1 Answers

This would add "Richie" to the list of authors cited for a paper that does not already have "Richie" as an author.

An alternative would be to use $addToSet.

But then how would I know whether {"authors cited" : {"$ne" : "Richie"}} means the elements in the list corresponding to "author cited", vs the value corresponding to "author cited"?

That is a bit confusing. Generally (I am sure there are exceptions, but those should be documented), all selectors target the individual values for multi-values fields. In Mongo this is called "multikeys" .

Note that this led me to assume initially that your query would target all papers that have at least one author who is not Richie. Then I checked and this turned out to be wrong. +1 for your question, because this really needs to be documented better.

like image 138
Thilo Avatar answered Dec 08 '25 07:12

Thilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!