2016-12-14 57 views
0

我在尝试查找适当的查询以查看数据在我的表中时遇到了一些问题,我已经尝试过使用elementMatch,在其他问题中阅读了很多内容。尝试$编辑,但我只能得到2个文件中的1个。我做错了什么?为什么我只有一个? 我有这个文件在集合中查找多个元素匹配

{ 
"_id" : "dff26f9c-350b-4bd5-bc62-62c19f21100c", 
"sensorId" : "123456", 
"sensorModel" : "LOOP", 
    "attachments" : [ 
    { 
     "type" : "StructAttachment", 
     "data" : "STRUCT DATA" 
    }, 
    { 
     "type" : "BlobAttachment", 
     "data" : { "$binary" : "QkxPQl9EQVRB", "$type" : "00" } 
    }, 
    { 
     "type" : "otherData", 
     "data" : { "$binary" : "QkxPQl9EQVRB", "$type" : "00" } 
    } 
] 

}

我期待的结果

{ 
"_id" : "dff26f9c-350b-4bd5-bc62-62c19f21100c", 
"sensorId" : "123456", 
"sensorModel" : "LOOP", 
    "attachments" : [ 
    { 
     "type" : "StructAttachment", 
     "data" : "STRUCT DATA" 
    }, 
    { 
     "type" : "BlobAttachment", 
     "data" : { "$binary" : "QkxPQl9EQVRB", "$type" : "00" } 
    } 
] 

}

我想通过_id和attachment.type(”过滤structAttachment“或”blobAttachment“)

回答

1

使用$filter$setIsSubset如下:

db.collectionName.aggregate({ 
    "$project": { 
    "sensorId": 1, 
    "sensorModel": 1, 
    "attachments": { 
     "$filter": { 
    "input": "$attachments", 
    "as": "el", 
    "cond": { 
     "$setIsSubset": [ 
     ["$$el.type"], 
     ["StructAttachment", "BlobAttachment"] 
     ] 
    } 
     } 
    } 
    } 
}).pretty() 
+1

谢谢你的回复快,你错过了ID,但是我能够与 db.getCollection(“测量”)加总( { \t $比赛:{ “_id”: “dff26f9c-350B-4bd5-bc62-62c19f21100c”}} , { “$项目”:{ “sensorId”:1, “sensorModel”:1, “附件”:{ “$ filter”:{ “input”:“$ attachments”, “as”:“attachment”, “COND”:{ “$ setIsSubset”:[ [ “$$ attachment.type”], [ “StructAttachment”, “BlobAttachment”] ] } } } } }) – Guel135

相关问题