子集合上的精确包含匹配
使用带有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法重现该问题,可能是因为我们使用不同版本的 MongoDB 和/或 NoRM。但是,这里有一些步骤可以帮助您找到模糊结果的根源。
使用 MongoDB shell 打开分析:
<前><代码>> db.setProfilingLevel(2)
查看已执行的查询:
<前><代码>> db.system.profile.find()
分析信息应如下所示:
实际查询位于
info
属性中,并且应为:如果您的查询看起来不同,则“问题”出在 NoRM 驱动程序中。例如,如果 NoRM 将您的代码转换为以下正则表达式查询,它将执行子字符串匹配:
我自己使用过 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.
Turn on profiling, using the MongoDB shell:
Review the queries that were executed:
The profiling information should look something like this:
The actual query is in the
info
property and should be: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:
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 ;)
在 shell 语法中:
in shell syntax: