MongoDB在同一密钥中查找具有多个条件的嵌套子文档
我有一个集合,其中有许多以下格式的项目MongoDB在同一密钥中查找具有多个条件的嵌套子文档,mongodb,Mongodb,我有一个集合,其中有许多以下格式的项目 { '_id': ObjectId(), 'info': [{ 'type': "type1", 'foo': foo }, { 'type': "type2", 'bar': bar }, {
{
'_id': ObjectId(),
'info': [{
'type': "type1",
'foo': foo
},
{
'type': "type2",
'bar': bar
},
{
'type': "type3",
'foo': foo
}]
}
我希望能够找到所有包含type1和type2子文档的文档,并将它们投影为只包含type1和type2
因此,反应如下:
[{ '_id': ObjectId(), 'info': [{'type': "type1", 'foo': foo}, {'type': "type2", 'bar': bar}]}, ...]
yourTable.find({$or:[{“info.type”:“type1”},{“info.type”:“type2”},{“info.type”:“type3”}]})
你可以试试这样的
以下是链接:
[1] :yourTable.find({$or:[{“info.type”:“type1”},{“info.type”:“type2”},{“info.type”:“type3”})
你可以试试这样的
以下是链接:
[1] :这将返回所有类型为1或2的文档,我需要的文档信息包含类型为1的子文档和类型为2的子文档。我还需要投影。它返回所有类型为1或2的文档,我需要info包含类型为1的子文档和类型为2的子文档的文档。我还需要投影。