1

我有这样的记录:

{
    "_id" : ObjectId("5f99cede36fd08653a3d4e92"),
    "accessions" : {
        "sample_accessions" : {
            "5f99ce9636fd08653a3d4e86" : {
                "biosampleAccession" : "SAMEA7494329",
                "sraAccession" : "ERS5250977",
                "submissionAccession" : "ERA3032827",
                "status" : "accepted"
            },
            "5f99ce9636fd08653a3d4e87" : {
                "biosampleAccession" : "SAMEA7494330",
                "sraAccession" : "ERS5250978",
                "submissionAccession" : "ERA3032827",
                "status" : "accepted"
            }
         }
     }
}

如何通过 mongo id 查询sample_accessions?我认为这应该有效,但事实并非如此。我应该做什么?

db.getCollection('collection').find({"accessions.sample_accessions":"5f99ce9636fd08653a3d4e86"})
4

1 回答 1

2

id 是 key 并检查 key 是否存在$exists,使用 project 自定义响应以获取特定对象

db.getCollection('collection').find(
  {
    "accessions.sample_accessions.5f99ce9636fd08653a3d4e86": {
      $exists: true
    }
  },
  { sample_doc: "$accessions.sample_accessions.5f99ce9636fd08653a3d4e86" }
)

操场

于 2020-11-13T17:25:33.303 回答