如何在MongoDB中严格搜索?

发布于 2025-02-02 20:22:23 字数 1512 浏览 3 评论 0原文

我有一个集合,其中有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2025-02-09 20:22:23

如果有人在搜索相同的查询,这是答案: -
正如@prasad_提到的那样,您可以使用$或运算符,例如: -

注意: - 查询是案例敏感的

var search="Deep End"
db.songDetails.find({$or:[{songName:search},{albumName:search}]})

以防您要搜索查询中case内敏感敏感性然后,您可以使用以下方式使用以下方式: -

db.songDetails.find({$or:[{songName:{$regex:/deep end/i}},{albumName:{$regex:/deep end/i}}]})

确保您在正则态度中添加了 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

var search="Deep End"
db.songDetails.find({$or:[{songName:search},{albumName:search}]})

In case you want to search the query in-case sensitive then you can use regex as follows:-

db.songDetails.find({$or:[{songName:{$regex:/deep end/i}},{albumName:{$regex:/deep end/i}}]})

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文