如何在 mongodb 中严格搜索?
how to search strictly in mongodb?
我有一个集合,其中有 70 个文档,如下所示:-
{
"_id" : ObjectId("629090f9772c8f43cd264662"),
"songName" : "Deep End",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "Deep Blue",
"albumName": "Pages"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The World",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The River",
"albumName": "A Different Kind Of Human - Step 2"
}
我想严格搜索这 70 个文档,目前我的代码如下所示:-
db.songDetails.aggregate([{$search:{index:"searchDB",text:{query:"deep end",path:{"wildcard":"*"}}}}])
当我 运行 这样做时,我得到的结果是:-
[
{
"_id" : ObjectId("629090f9772c8f43cd264662"),
"songName" : "Deep End",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "Deep Blue",
"albumName": "Pages"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The World",
"albumName": "Deep End"
}
]
但我只想要结果
{
"_id" : ObjectId("629090f9772c8f43cd264662"),
"songName" : "Deep End",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The World",
"albumName": "Deep End"
}
我的意思是,我需要 精确搜索 以从任何 字段 {可能来自“songName”或“albumName”},我该怎么做?
谢谢:)
如果有人正在搜索相同的查询,这里是答案:-
正如@prasad_ 提到的,您可以使用 $or
运算符,如下所示:-
注意:-查询是case-sensitive
var search="Deep End"
db.songDetails.find({$or:[{songName:search},{albumName:search}]})
如果您想搜索 in-case 敏感的查询,那么您可以使用正则表达式,如下所示:-
db.songDetails.find({$or:[{songName:{$regex:/deep end/i}},{albumName:{$regex:/deep end/i}}]})
确保您在正则表达式中添加了 i,因为它会使您的查询 in-case 敏感,请注意,这将导致每个包含 [=21= 的条目]deep end匹配查询(deep end,deep end XYZ,deep end now)
我有一个集合,其中有 70 个文档,如下所示:-
{
"_id" : ObjectId("629090f9772c8f43cd264662"),
"songName" : "Deep End",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "Deep Blue",
"albumName": "Pages"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The World",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The River",
"albumName": "A Different Kind Of Human - Step 2"
}
我想严格搜索这 70 个文档,目前我的代码如下所示:-
db.songDetails.aggregate([{$search:{index:"searchDB",text:{query:"deep end",path:{"wildcard":"*"}}}}])
当我 运行 这样做时,我得到的结果是:-
[
{
"_id" : ObjectId("629090f9772c8f43cd264662"),
"songName" : "Deep End",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "Deep Blue",
"albumName": "Pages"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The World",
"albumName": "Deep End"
}
]
但我只想要结果
{
"_id" : ObjectId("629090f9772c8f43cd264662"),
"songName" : "Deep End",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The World",
"albumName": "Deep End"
}
我的意思是,我需要 精确搜索 以从任何 字段 {可能来自“songName”或“albumName”},我该怎么做?
谢谢:)
如果有人正在搜索相同的查询,这里是答案:-
正如@prasad_ 提到的,您可以使用 $or
运算符,如下所示:-
注意:-查询是case-sensitive
var search="Deep End"
db.songDetails.find({$or:[{songName:search},{albumName:search}]})
如果您想搜索 in-case 敏感的查询,那么您可以使用正则表达式,如下所示:-
db.songDetails.find({$or:[{songName:{$regex:/deep end/i}},{albumName:{$regex:/deep end/i}}]})
确保您在正则表达式中添加了 i,因为它会使您的查询 in-case 敏感,请注意,这将导致每个包含 [=21= 的条目]deep end匹配查询(deep end,deep end XYZ,deep end now)