我们可以在MongoDB中一起使用NOT和AND吗?
是的,我们可以在MongoDB中一起使用NOT和AND。语法如下
NOT X AND NOT Y = NOT (X AND Y) Let us see the working of above syntax. If both X and Y will be true then last result will be false. If one of the operands gives result false then last result will be true.
以下是使用文档创建集合的查询
> db.NotAndDemo.insertOne({"StudentName":"John","StudentCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c98746a330fd0aa0d2fe4a8")
}
> db.NotAndDemo.insertOne({"StudentName":"John","StudentCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c987478330fd0aa0d2fe4a9")
}
> db.NotAndDemo.insertOne({"StudentName":"David","StudentCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c987487330fd0aa0d2fe4aa")
}
> db.NotAndDemo.insertOne({"StudentName":"Chris","StudentCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9874ac330fd0aa0d2fe4ab")
}
> db.NotAndDemo.insertOne({"StudentName":"Chris","StudentCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9874b7330fd0aa0d2fe4ac")
}以下是在find()方法的帮助下显示集合中所有文档的查询
> db.NotAndDemo.find().pretty();
This will produce the following output:
{
"_id" : ObjectId("5c98746a330fd0aa0d2fe4a8"),
"StudentName" : "John",
"StudentCountryName" : "US"
}
{
"_id" : ObjectId("5c987478330fd0aa0d2fe4a9"),
"StudentName" : "John",
"StudentCountryName" : "UK"
}
{
"_id" : ObjectId("5c987487330fd0aa0d2fe4aa"),
"StudentName" : "David",
"StudentCountryName" : "AUS"
}
{
"_id" : ObjectId("5c9874ac330fd0aa0d2fe4ab"),
"StudentName" : "Chris",
"StudentCountryName" : "UK"
}
{
"_id" : ObjectId("5c9874b7330fd0aa0d2fe4ac"),
"StudentName" : "Chris",
"StudentCountryName" : "US"
}以下是将NOT和AND一起使用的查询,与NOT(XANDY)的NOTX或NOTY相同
> db.NotAndDemo.find({
... "$or": [
... {"StudentName": {"$ne": "Chris"}},
... {"StudentCountryName": {"$ne": "US"}}
... ]
... }).pretty();这将产生以下输出
{
"_id" : ObjectId("5c98746a330fd0aa0d2fe4a8"),
"StudentName" : "John",
"StudentCountryName" : "US"
}
{
"_id" : ObjectId("5c987478330fd0aa0d2fe4a9"),
"StudentName" : "John",
"StudentCountryName" : "UK"
}
{
"_id" : ObjectId("5c987487330fd0aa0d2fe4aa"),
"StudentName" : "David",
"StudentCountryName" : "AUS"
}
{
"_id" : ObjectId("5c9874ac330fd0aa0d2fe4ab"),
"StudentName" : "Chris",
"StudentCountryName" : "UK"
}热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特