从数组属性检索属性到pymongo对象

时间:2019-04-19 17:59:53

标签: arrays django mongodb pymongo

如何使用pymongo和django Framework检索contactos.0.direcciones.0.id属性?

{ 
    "_id" : ObjectId("5cb8c6827033b71491a296e3"), 
    "id" : NumberInt(89), 
    "contactos" : [
        {
            "id" : NumberInt(1), 
            "nombre" : "",  
            "direcciones" : [
                {
                    "id" : NumberInt(1),
                    "calle":"Street 1"

                }
         }
    ]
}

此代码为contactos.id运行,并检索contactos的最大id值

use esco_mongo;
db.getCollection("clientes").aggregate(
    [
        { 
            "$match" : {
                "id" : NumberInt(89)
            }
        }, 
        { 
            "$project" : {
                "_id" : 1.0, 
                "contactos.id" : 1.0
            }
        }, 
        { 
            "$unwind" : {
                "path" : "$contactos"
            }
        }, 
        { 
            "$sort" : {
                "contactos.id" : -1.0
            }
        }, 
        { 
            "$limit" : 1.0
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);

但是如果我更改为contactos.direcciones.id,则不会检索结果:

use esco_mongo;
db.getCollection("clientes").aggregate(
    [
        { 
            "$match" : {
                "id" : NumberInt(89)
            }
        }, 
        { 
            "$project" : {
                "_id" : 1.0, 
                "contactos.direcciones.id" : 1.0
            }
        }, 
        { 
            "$unwind" : {
                "path" : "$contactos.direcciones"
            }
        }, 
        { 
            "$sort" : {
                "contactos.direcciones.id" : -1.0
            }
        }, 
        { 
            "$limit" : 1.0
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);

是否有可能在一次查询中从direcciones数组中检索值到mongodb数据库?

预先感谢

0 个答案:

没有答案
相关问题