如何在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"
}
,我需要 精确搜索 从任何 field {它可以来自“ songname”或“ Albumname”} ,我该怎么做?
谢谢你:)
I have a collection in which 70 documents are present like this:-
{
"_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"
}
I want to search strictly through these 70 documents, as of now my code looks like this:-
db.songDetails.aggregate([{$search:{index:"searchDB",text:{query:"deep end",path:{"wildcard":"*"}}}}])
when I run this, I'm getting results as:-
[
{
"_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"
}
]
But I only want the result as
{
"_id" : ObjectId("629090f9772c8f43cd264662"),
"songName" : "Deep End",
"albumName": "Deep End"
},
{
"_id" : ObjectId("629090f9772c8f43cd264665"),
"songName" : "The World",
"albumName": "Deep End"
}
I mean, I need the exact search for the query from any field {it may be from "songName" or "albumName"}, how can I do this?
Thank you:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人在搜索相同的查询,这是答案: -
正如@prasad_提到的那样,您可以使用
$或
运算符,例如: -注意: - 查询是案例敏感的,
以防您要搜索查询中case内敏感敏感性然后,您可以使用以下方式使用以下方式: -
确保您在正则态度中添加了 i ,因为它使您的查询内case敏感并注意,这将导致每个条目包含 deeld端的条目匹配查询(深端,深端xyz,现在深处)
Here is the answer if anyone is searching for the same query:-
as @prasad_ mentioned you can use
$or
operator, like this:-Note:- the query is case-sensitive
In case you want to search the query in-case sensitive then you can use regex as follows:-
make sure that you have added i in the regex thing as it makes your query in-case sensitive and note that this will cause every entry containing deep end to match the query (deep end, deep end XYZ, deep end now)