嵌套对象的Mongoose Schema

时间:2015-01-07 11:59:54

标签: mongodb mongoose

有没有办法为这种类型的数据创建架构:

 "documents" : {

            "54983c4c78c824eb0ac7a0d8" : {
                "completed" : true
            },
            "54983c4c78c824eb0ac7a0dd" : {
                "completed" : true
            },
            "54983c4c78c824eb0ac7a0d7" : {
                "completed" : true
            },
            "54983c4c78c824eb0ac7a0de" : {
                "completed" : true,
                "comments" : [ 
                    {
                        "author" : "54973a31cfb18d60089e9403",
                        "text" : "hello"
                    }
                ]
            }
        }

1 个答案:

答案 0 :(得分:1)

这个怎么样:

"documents":{[
             {"id":54983c4c78c824eb0ac7a0d8,
             "status":"completed"},
             {"id":54983c4c78c824eb0ac7a0dd,
             "status":"completed"},
             {"id":54983c4c78c824eb0ac7a0d7,
             "status":"completed"},
             {"id":54983c4c78c824eb0ac7a0de,
             "status":"completed",
             "comments":[
                         {"author_id":54973a31cfb18d60089e9403,
                          "text":"hello"}]
             }]}

然后你可以查询你想要的任何东西:

db.documents.find({"id":54983c4c78c824eb0ac7a0d8})

或按状态:

db.documents.find({"status":"completed"})