子集合上的精确包含匹配

发布于 2024-10-06 08:27:54 字数 1112 浏览 5 评论 0原文

使用带有NoRM驱动程序的mongodb我有这个文档:

{
    "_id" : ObjectId("0758030341b870c019591900"),
    "TmsId" : "EP000015560091",
    "RootId" : "1094362",
    "ConnectorId" : "SH000015560000",
    "SeasonId" : "7894681",
    "SeriesId" : "184298",
    "Titles" : [
            {
                    "Size" : 120,
                    "Type" : "full",
                    "Lang" : "en",
                    "Description" : "House"
            },
            {
                    "Size" : 10,
                    "Type" : "red",
                    "Lang" : "en",
                    "Description" : "House M.D."
            }
    ], yadda yadda yadda

并且我正在查询:

var query = new Expando();
query["Titles.Description"] = Q.In(showNames);
var fuzzyMatches = db.GetCollection<Program>("program").Find(query).ToList();

其中showNames是一个字符串[]包含类似{“House”,“Glee”,“30 Rock”}的内容

我的结果包含模糊匹配。例如,术语“House”返回标题中包含“House”一词的每个节目(就像它执行“Contains”一样)。

我想要的是直接比赛。因此,如果 document.Titles 包含“A big blue House”,它不会返回匹配项。仅当 Titles.Description 包含“House”时,我才会想要匹配。

Using mongodb with the NoRM driver I have this document:

{
    "_id" : ObjectId("0758030341b870c019591900"),
    "TmsId" : "EP000015560091",
    "RootId" : "1094362",
    "ConnectorId" : "SH000015560000",
    "SeasonId" : "7894681",
    "SeriesId" : "184298",
    "Titles" : [
            {
                    "Size" : 120,
                    "Type" : "full",
                    "Lang" : "en",
                    "Description" : "House"
            },
            {
                    "Size" : 10,
                    "Type" : "red",
                    "Lang" : "en",
                    "Description" : "House M.D."
            }
    ], yadda yadda yadda

and I am querying like:

var query = new Expando();
query["Titles.Description"] = Q.In(showNames);
var fuzzyMatches = db.GetCollection<Program>("program").Find(query).ToList();

where showNames is a string[] contain something like {"House", "Glee", "30 Rock"}

My results contain fuzzy matches. For example the term "House" returns every show with a Title with the word House in it ( like its doing a Contains ).

What I would like is straight matches. So if document.Titles contains "A big blue House" it does not return a match. Only if Titles.Description contains "House" would I like a match.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

感性不性感 2024-10-13 08:27:54

我无法重现该问题,可能是因为我们使用不同版本的 MongoDB 和/或 NoRM。但是,这里有一些步骤可以帮助您找到模糊结果的根源。

  1. 使用 MongoDB shell 打开分析:

    <前><代码>> db.setProfilingLevel(2)

  2. 再次运行您的代码。
  3. 将分析级别设置回 0。
  4. 查看已执行的查询:

    <前><代码>> db.system.profile.find()

分析信息应如下所示:

{
  "ts" : "Wed Dec 08 2010 09:13:13 GMT+0100",
  "info" : "query test.program ntoreturn:2147483647 reslen:175 nscanned:3  \nquery: { query: { Titles.Description: { $in: [ \"House\", \"Glee\", \"30 Rock\" ] } } }  nreturned:1 bytes:159",
  "millis" : 0
}

实际查询位于 info 属性中,并且应为:

{ Titles.Description: { $in: [ "House", "Glee", "30 Rock" ] } }

如果您的查询看起来不同,则“问题”出在 NoRM 驱动程序中。例如,如果 NoRM 将您的代码转换为以下正则表达式查询,它将执行子字符串匹配:

{ Titles.Description: { $in: [ /House/, /Glee/, /30 Rock/ ] } }

我自己使用过 NoRM,但我还没有遇到控制它的设置。也许您使用的是不同的版本,它确实具有此类功能。

如果您的查询与应有的查询没有不同,请尝试从 shell 运行查询。如果结果仍然模糊,那么我们肯定使用了不同版本的 MongoDB ;)

I haven't been able to reproduce the problem, perhaps because we're using different versions of MongoDB and/or NoRM. However, here are some steps that may help you to find the origin of the fuzzy results.

  1. Turn on profiling, using the MongoDB shell:

    > db.setProfilingLevel(2)
    
  2. Run your code again.
  3. Set the profiling level back to 0.
  4. Review the queries that were executed:

    > db.system.profile.find()
    

The profiling information should look something like this:

{
  "ts" : "Wed Dec 08 2010 09:13:13 GMT+0100",
  "info" : "query test.program ntoreturn:2147483647 reslen:175 nscanned:3  \nquery: { query: { Titles.Description: { $in: [ \"House\", \"Glee\", \"30 Rock\" ] } } }  nreturned:1 bytes:159",
  "millis" : 0
}

The actual query is in the info property and should be:

{ Titles.Description: { $in: [ "House", "Glee", "30 Rock" ] } }

If your query looks different, then the 'problem' is in the NoRM driver. For example, if NoRM translates your code to the following regex query, it will do a substring match:

{ Titles.Description: { $in: [ /House/, /Glee/, /30 Rock/ ] } }

I have used NoRM myself, but I haven't come across a setting to control this. Perhaps you're using a different version, that does come with such functionality.

If your query isn't different from what it should by, try running the query from the shell. If it still comes up with fuzzy results, then we're definitely using different versions of MongoDB ;)

灼疼热情 2024-10-13 08:27:54

在 shell 语法中:

db.mycollection.find( { "Titles.Description" : "House" } )

in shell syntax:

db.mycollection.find( { "Titles.Description" : "House" } )

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