如何在MongoDB中使用“不喜欢”运算符?
为此,请在MongoDB中使用$not运算符。为了理解这个概念,让我们用文档创建一个集合。使用文档创建集合的查询如下-
> db.notLikeOperatorDemo.insertOne({"StudentName":"John Doe"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a29c393b406bd3df60dfc")
}
> db.notLikeOperatorDemo.insertOne({"StudentName":"John Smith"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a29cc93b406bd3df60dfd")
}
> db.notLikeOperatorDemo.insertOne({"StudentName":"John Taylor"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a29df93b406bd3df60dfe")
}
> db.notLikeOperatorDemo.insertOne({"StudentName":"Carol Taylor"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a2a1693b406bd3df60dff")
}
> db.notLikeOperatorDemo.insertOne({"StudentName":"David Miller"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a2a2693b406bd3df60e00")
}在find()method的帮助下显示集合中的所有文档。查询如下-
> db.notLikeOperatorDemo.find().pretty();
以下是输出-
{ "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" }
{
"_id" : ObjectId("5c8a29cc93b406bd3df60dfd"),
"StudentName" : "John Smith"
}
{
"_id" : ObjectId("5c8a29df93b406bd3df60dfe"),
"StudentName" : "John Taylor"
}
{
"_id" : ObjectId("5c8a2a1693b406bd3df60dff"),
"StudentName" : "Carol Taylor"
}
{
"_id" : ObjectId("5c8a2a2693b406bd3df60e00"),
"StudentName" : "David Miller"
}这是在MongoDB中使用NOTLIKE运算符的查询-
> db.notLikeOperatorDemo.find( { StudentName: { $not: /^John Taylor.*/ } } );以下是输出-
{ "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" }
{ "_id" : ObjectId("5c8a29cc93b406bd3df60dfd"), "StudentName" : "John Smith" }
{ "_id" : ObjectId("5c8a2a1693b406bd3df60dff"), "StudentName" : "Carol Taylor" }
{ "_id" : ObjectId("5c8a2a2693b406bd3df60e00"), "StudentName" : "David Miller" }
You can use the following query also:
> db.notLikeOperatorDemo.find({StudentName: {$not: /John Taylor/}});
The following is the output:
{ "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" }
{ "_id" : ObjectId("5c8a29cc93b406bd3df60dfd"), "StudentName" : "John Smith" }
{ "_id" : ObjectId("5c8a2a1693b406bd3df60dff"), "StudentName" : "Carol Taylor" }
{ "_id" : ObjectId("5c8a2a2693b406bd3df60e00"), "StudentName" : "David Miller" }热门推荐
10 新婚红包祝福语简短男
11 送礼祝福语男性生日简短
12 老师新婚快乐祝福语简短
13 孩子满月随礼简短祝福语
14 发给导师的祝福语简短
15 庆祝国家的祝福语简短
16 请灵回向祝福语简短
17 开业祝福语简短大气的
18 周二祝福语大全简短