Okay, I've been trying to wrap my head around this for a while and still cannot find the answer. Let's say I have a collection of Mongo documents containing a field with an array of strings called tags.
好吧,我已经想了好一阵子了,但还是找不到答案。假设我有一个Mongo文档集合,其中包含一个带有称为标记的字符串数组的字段。
{
_id: ObjectId("64fdff612ee6d1467c4501f5"),
title: "Document1",
"tags": [
"tagOne"
]
},
{
_id: ObjectId("64fdff612ee6d1467c4501f6"),
title: "Document2",
"tags": [
"tagOne",
"tagTwo"
]
},
{
_id: ObjectId("64fdff612ee6d1467c4501f7"),
title: "Document3",
"tags": [
"tagOne",
"tagTwo",
"tagThree"
]
}
I have no problem returning the documents that contain "tagOne" OR "tagTwo". For example, using
我可以毫不费力地返回包含“tag One”或“tag Two”的文档。例如,使用
db.getCollection("documents").find({ 'tags': { $in: ['tagOne',tagTwo'] }})
Db.getCollection(“Documents”).find({‘tag’:{$in:[‘tag One’,tag Two‘]})
would return all three documents. However, I'm seeking to query for documents that contain at least those two tags. Therefore, a query that would only return the last two documents since they contain at least those two tags (but could contain any other tags as well).
会退还所有三份文件。但是,我正在寻求查询至少包含这两个标记的文档。因此,只返回最后两个文档的查询,因为它们至少包含这两个标记(但也可以包含任何其他标记)。
更多回答
优秀答案推荐
我是一名优秀的程序员,十分优秀!