Max
Max

Reputation: 1459

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?

Upvotes: 3

Views: 357

Answers (1)

Thilo
Thilo

Reputation: 262534

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.

Upvotes: 2

Related Questions