在 MongoDB 中搜索多个字段的多个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21417711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search multiple fields for multiple values in MongoDB
提问by jlmcdonald
I've got a MongoDB collection where a particular string may appear in any of a number of fields:
我有一个 MongoDB 集合,其中一个特定的字符串可能出现在多个字段中的任何一个中:
{"_id":1, "field1":"foo","field2":"bar","field3":"baz", "otherfield":"stuff"},
{"_id":2, "field1": "bar", "field2": "baz", "field3": "foo", "otherfield":"morestuff"},
{"_id":3, "field1": "baz", "field2": "foo", "field3": "bar", "otherfield":"you get the idea"}
I need to query so that I am returned all records where any one of a set of fields is equal to any value in an array ... basically, if I have ["foo","bar"]
I need it to match if either of those strings are in field1 or field2 (but not any other field).
我需要查询以便我返回所有记录,其中一组字段中的任何一个字段等于数组中的任何值......基本上,如果我有["foo","bar"]
我需要它来匹配这些字符串中的任何一个是否在 field1 或 field2 中(但不是任何其他领域)。
Obviously I can do this with a series of multiple queries
显然我可以通过一系列的多个查询来做到这一点
db.collection.find({"field1":{"$in":["foo","bar"]}})
db.collection.find({"field2":{"$in":["foo","bar"]}})
db.collection.find({"field1":{"$in":["foo","bar"]}})
db.collection.find({"field2":{"$in":["foo","bar"]}})
etc., and I've also made a very large $or query that concatenates them all together, but it seems far too inefficient (my actual collection needs to match any of 15 strings that can occur in any of 9 fields) ... but I'm still new to nosql DBs and am not sure of the best paradigm I need to use here. Any help is greatly appreciated.
等等,我还做了一个非常大的 $or 查询,将它们连接在一起,但它似乎效率太低了(我的实际集合需要匹配可能出现在 9 个字段中的任何一个的 15 个字符串中的任何一个)......但我还是 nosql DB 的新手,不确定我需要在这里使用的最佳范例。任何帮助是极大的赞赏。
采纳答案by jlmcdonald
Found another answer through poring over the documentation that seems to hit a sweet spot -- text indexes.
通过仔细研究似乎达到最佳点的文档找到了另一个答案 - 文本索引。
db.collection.ensureIndex({"field1":"text","field2":"text"})
db.records.runCommand("text",{search:"foo bar"})
When I run my actual query with many more strings and fields (and about 100,000 records), the $or/$in
approach takes 620 milliseconds while the text index takes 131 milliseconds. The one drawback is that it returns a different type of document as a result; luckily the actual documents are a parameter of each result object.
当我使用更多字符串和字段(以及大约 100,000 条记录)运行我的实际查询时,该$or/$in
方法需要 620 毫秒,而文本索引需要 131 毫秒。一个缺点是它会返回不同类型的文档。幸运的是,实际文档是每个结果对象的参数。
Thanks to those who took the time to make suggestions.
感谢那些花时间提出建议的人。
回答by a14m
回答by heinob
I would collect all the relevant fields in one field (i.e. collected
) by adding their values like
我会collected
通过添加它们的值来收集一个字段中的所有相关字段(即)
"foo:field1",
"bar:field2",
"baz:field3",
"stuff:otherfield",
"bar:field1",
"baz:field2"
...
into that field.
进入那个领域。
If you search for bar
existing in any field you can use:
如果您bar
在任何字段中搜索existing ,您可以使用:
db.collection.find( { collected: { $regex: "^bar" } }, ... );
Your example in the question would look like:
您在问题中的示例如下所示:
db.collection.find( collected: { { $all: [ "foo:field1", "foo:field2", "bar:field1", "bar:field2" ] } }, ... );