MongoDB 查询嵌套文档

MongoDB querying nested documents

我有这样的记录:

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

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

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

id 是一个键,检查键是否存在使用$exists,使用项目自定义响应以获取特定对象

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

Playground