在mongodb中是否可以通过子对象进行查询?

时间:2015-02-23 16:40:41

标签: mongodb mongodb-query

我有mongo的文档:

{
  "_id" : ObjectId("54eb5189ad9685bbb622ca52"),
  "header" : {
    "title" : "Project Name 1",
    "header_img" : "project_name_1.png",
    "project_stats" : {
        "sqFt" : 20000,
        "tons" : 300,
        "duration" : "6 months",
        "type" : "education facility",
        "summary" : "Give quick summary of problem solved."
    }
},
"row_project_detail" : {
    "project_logo" : "project_name_1_logo.png",
    "header" : "Project Name 1 was a project where...",
    "paragraph" : "blah blah blah blah"
},
"row_1" : {
    "img" : "project_name_2.png"
},
"row_2" : {
    "img_1" : "project_name_3.png",
    "img_2" : "project_name_4.png"
},
"row_3" : {
    "img" : "project_name_5.png"
},
"row_4" : {
    "img" : "project_name_6.png"
}
}

我尝试按db.projects.find({ header: { title: "Project Name 1"} });进行查询,但不会产生结果。如何通过title对象中的header键进行查询?这是可能的还是我只需要复制title子对象中的header密钥并将其放在doc的根目录下?

1 个答案:

答案 0 :(得分:1)

使用点符号:

db.projects.find({ "header.title": "Project Name 1"});