[英]Mongo query to find documents where any of the field's elements match any of the query param elements
I've scoured the docs and google.我已经搜索了文档和谷歌。 Maybe I just need help formulating the question.
也许我只是需要帮助来制定这个问题。
I have a field that stores an array of values.我有一个存储值数组的字段。 We'll call it "tags" for simplicity.
为简单起见,我们将其称为“标签”。 I want to be able to search for all documents that match ANY of the multiple tags I search on.
我希望能够搜索与我搜索的多个标签中的任何一个匹配的所有文档。 Put another way, I want to find all records that are tagged with at least one (not necessarily all) of these tags.
换句话说,我想找到所有标记有至少一个(不一定是全部)这些标签的记录。 Here's an example.
这是一个例子。
Documents :文件:
{name: "My Thing 1", tags: ["cool", "important", "anothertag"]},
{name: "My Thing 2", tags: ["cool"]},
{name: "My Thing 3", tags: ["cool", "important"]}
{name: "My Thing 4", tags: ["important", "anothertag"]}
{name: "My Thing 5", tags: []}
{name: "My Thing 6", tags: ["anothertag"]}
Query: ["important", "cool"]
查询:
["important", "cool"]
Result: documents 1-4 (not 5-6)结果:文档 1-4(不是 5-6)
See comment by whoami.请参阅 whoami 的评论。 $in does the trick.
$in 可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.