簡體   English   中英

Mongodb嵌入式文檔查詢

[英]Mongodb Embedded document querying

嗨,我有一個這樣的數據模型

module{
    name: "xx",
    sa: [
        {
            sa_name: "yy",
            fact: [
                fact_name: "zz"
            ],
            dim: [
                dim_name: "qq"
            ]
        }
    ]
}

我有一個嵌入模塊和事實和暗淡嵌入sa。

我試過db.coll.find({"module.sa.fact.name":"zz"},{})不能用於單嵌套db.coll.find({"module.sa.name":"yy"},{})工作正常。 如何在子文檔中查詢此子文檔。

將您的查詢更新為:

db.coll.find({"module.sa" : {$elemMatch : {"fact.fact_name": "zz"}}})

對我而言,查詢工作正常,請參閱mongo 2.4.8 shell:

> db.coll.insert({module: {name:"xx", sa: [{sa_name:"yy", fact:[{fact_name:"zz"}], dim:[{dim_name:"qq"}]}]}})
> db.coll.findOne()
{
        "_id" : ObjectId("52a85b44fd0b335ab0546ca4"),
        "module" : {
                "name" : "xx",
                "sa" : [
                        {
                                "sa_name" : "yy",
                                "fact" : [
                                        {
                                                "fact_name" : "zz"
                                        }
                                ],
                                "dim" : [
                                        {
                                                "dim_name" : "qq"
                                        }
                                ]
                        }
                ]
        }
}
> db.coll.findOne({"module.sa.fact.fact_name":"zz"})
{
        "_id" : ObjectId("52a85b44fd0b335ab0546ca4"),
        "module" : {
                "name" : "xx",
                "sa" : [
                        {
                                "sa_name" : "yy",
                                "fact" : [
                                        {
                                                "fact_name" : "zz"
                                        }
                                ],
                                "dim" : [
                                        {
                                                "dim_name" : "qq"
                                        }
                                ]
                        }
                ]
        }
}

可能是因為你正在查詢"module.sa.fact.name" ,它應該是"module.sa.fact.fact_name" ,你只是遇到某種拼寫錯誤的問題。 但是,這個問題對於適合您的查詢也是如此......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM