[英]$not not performing $and operation
I just started exploring mongoDB. 我刚刚开始探索mongoDB。 During that time I managed to create a simple collection with the following data,
在此期间,我设法使用以下数据创建了一个简单的集合,
db.test.insert({name : "joe", age : 20});
db.test.insert({name: "james", age : 25});
Now I have queried it by using, 现在我已经通过使用查询了
db.test.find({name : {$not : {$eq : "joe", $eq: "james"}}})
It yielded me the following result, 它给我带来了以下结果,
{name : "joe", age : 20}
I am confused here, Is it not an and operation like below, 我在这里感到困惑,是不是和下面的操作一样,
SELECT * FROM test where not name = "joe" and not name = "james"
Doubt1: If yes then how the above mentioned mongo query can be interpreted as a normal mysql query? 疑问1:如果是,那么上述mongo查询如何解释为普通的mysql查询? [Note : I am coming from a mysql background.] Please explain.
[注意:我来自mysql背景。]请解释。
Doubt2: Also explain about the following mongo query, 疑问2:还要说明有关以下mongo查询的信息,
db.test.find({name : "joe", age : 20});
Is the above query equals to select * from test where name='joe' and age=20
? 上面的查询是否等于
select * from test where name='joe' and age=20
? If not then how it would be interpreted as a normal mysql query? 如果没有,那么它将如何解释为普通的mysql查询?
When you are creating an object with {$eq : "joe", $eq: "james"}
you are using the same object key twice. 当使用
{$eq : "joe", $eq: "james"}
创建对象时,您使用相同的对象键两次。 So james
will overwrite joe
because both use the same object key $eq
. james
将覆盖joe
因为它们都使用相同的对象键$eq
。
To do your query, you can try something like NOT (name = "joe" OR name = "james")
. 要进行查询,您可以尝试使用
NOT (name = "joe" OR name = "james")
。 So you add the OR
to your mongo query: 因此,您将
OR
添加到mongo查询中:
db.test.find({name : {$nor: [{$eq: "joe"}, $eq: "james"]}})
I guess Christoph cleared your doubts. 我猜克里斯多夫(Christoph)消除了您的疑虑。
For SQL to Mongo Query reference visit this link 有关SQL to Mongo Query的参考,请访问此链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.