通过在mongodb中提供数组名称从嵌套数组中获取整个数组

时间:2014-01-27 12:51:50

标签: arrays mongodb

假设这是我的收藏品

db.collection.insert([
    {
        "5496":[
            {
                "Asset Name":"HiScanSQ",
                "Chiller Temperature (deg)":"2",
                "Trays Used (nos)":"12",
                "Power (volts)":"123",
                "Pressure (psi)":"15",
                "Usage (hours)":"140",
                "Volume (ml)":"150",
                "Viscosity":"1.8",
                "Cartridge Revolution (rpm)":"1000"
            }
        ]
    },
    {
        "4963":[
            {
                "Asset Name":"Genome Analyzer",
                "Chiller Temperature (deg)":"451",
                "Trays Used (nos)":"3",
                "Power (volts)":"82",
                "Pressure (psi)":"55",
                "Usage (hours)":"280",
                "Volume (ml)":"1000",
                "Viscosity":"1.2",
                "Cartridge Revolution (rpm)":"976"
            }
        ]
    }
]);

如何获取整个数组?说“5496”的整个值。我需要获取与特定资产相关的所有值。因此,如果我提供资产ID mongo应返回所有资产详细信息。

2 个答案:

答案 0 :(得分:0)

试试这个:

db.collection.find( {}, { fields: { "5496": 1 }, function( err ,result ) {
    ...
});

答案 1 :(得分:0)

db.collection.find( { "5496": { $exists: true } }, { "5496": 1, _id: 0 } ).pretty()
{
        "5496" : [
                {
                        "Asset Name" : "HiScanSQ",
                        "Chiller Temperature (deg)" : "2",
                        "Trays Used (nos)" : "12",
                        "Power (volts)" : "123",
                        "Pressure (psi)" : "15",
                        "Usage (hours)" : "140",
                        "Volume (ml)" : "150",
                        "Viscosity" : "1.8",
                        "Cartridge Revolution (rpm)" : "1000"
                }
        ]
}