在名称和电子邮件字段上以特定字母开头的“喜欢”实现查询MongoDB?
对于MongoDB中的“赞”实现,请使用//并在两者之间设置该特定字母。例如-
/J/
让我们创建一个包含文档的集合-
> db.demo554.insertOne({"UserName":"John","UserMailId":"John@gmail.com"});{
"acknowledged" : true, "insertedId" : ObjectId("5e8f1cfed1d72c4545cb8679")
}
> db.demo554.insertOne({"UserName":"Chris","UserMailId":"Chris@gmail.com"});{
"acknowledged" : true, "insertedId" : ObjectId("5e8f1d0cd1d72c4545cb867a")
}
> db.demo554.insertOne({"UserName":"Jace","UserMailId":"Jace@gmail.com"});{
"acknowledged" : true, "insertedId" : ObjectId("5e8f1d1cd1d72c4545cb867b")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo554.find();
这将产生以下输出-
{ "_id" : ObjectId("5e8f1cfed1d72c4545cb8679"), "UserName" : "John", "UserMailId" : "John@gmail.com" }
{ "_id" : ObjectId("5e8f1d0cd1d72c4545cb867a"), "UserName" : "Chris", "UserMailId" : "Chris@gmail.com" }
{ "_id" : ObjectId("5e8f1d1cd1d72c4545cb867b"), "UserName" : "Jace", "UserMailId" : "Jace@gmail.com" }以下是对“like”实现的查询-
> db.demo554.find({
... "$or": [
... { "UserName": /J/ },
...
... { "UserMailId": /J/ }
... ]
... }
... );这将产生以下输出-
{ "_id" : ObjectId("5e8f1cfed1d72c4545cb8679"), "UserName" : "John", "UserMailId" : "John@gmail.com" }
{ "_id" : ObjectId("5e8f1d1cd1d72c4545cb867b"), "UserName" : "Jace", "UserMailId" : "Jace@gmail.com" }