Mongodb Mongo-查询数组内部

Mongodb Mongo-查询数组内部,mongodb,Mongodb,我有这个数据库结构 { "_id": 107, "standard": {"name": "building", "item": [{"code": 151001, "quantity": 10, "delivered": 8, "um": "kg" }, {"

我有这个数据库结构

{
"_id": 107,
"standard": {"name": "building",
               "item": [{"code": 151001,
                          "quantity": 10,
                          "delivered": 8,
                          "um": "kg" },
                          {"code": 151001,
                          "quantity": 20,
                          "delivered": 6,
                          "um": "kg" }]
              }
}
我想找到所有代码为151001的对象,只显示交付的字段

例如,它将显示如下内容:

{delivered: 8}
{delivered: 6}
到目前为止,我得到了这个查询,但它没有准确显示我想要的:

db.test.find(
        {
            "standard.item.code": 151001
        }
        ).pretty()

由于您的项目位于一个数组中,因此最好的方法是使用

示例代码:

db.test.aggregate(
    // Find matching documents (could take advantage of an index)
    { $match: {
        "standard.item.code" : 151001,
    }},

    // Unpack the item array into a stream of documents
    { $unwind: "$standard.item" },

    // Filter to the items that match the code
    { $match: {
        "standard.item.code" : 151001,
    }},

    // Only show the delivered amounts
    { $project: {
        _id: 0,
        delivered: "$standard.item.delivered"
    }}
)
结果:

{
    "result" : [
        {
            "delivered" : 8
        },
        {
            "delivered" : 6
        }
    ],
    "ok" : 1
}

您会注意到聚合中有两个
$match
步骤。第一个是匹配包含该项目代码的文档。在阵列上使用
$unwind
后,第二个
$match
限制具有该代码的项目。

由于您的项目位于阵列中,因此最好的方法是使用

示例代码:

db.test.aggregate(
    // Find matching documents (could take advantage of an index)
    { $match: {
        "standard.item.code" : 151001,
    }},

    // Unpack the item array into a stream of documents
    { $unwind: "$standard.item" },

    // Filter to the items that match the code
    { $match: {
        "standard.item.code" : 151001,
    }},

    // Only show the delivered amounts
    { $project: {
        _id: 0,
        delivered: "$standard.item.delivered"
    }}
)
结果:

{
    "result" : [
        {
            "delivered" : 8
        },
        {
            "delivered" : 6
        }
    ],
    "ok" : 1
}

您会注意到聚合中有两个
$match
步骤。第一个是匹配包含该项目代码的文档。在阵列上使用
$unwind
后,第二个
$match
限制具有该代码的项目。

由于您的项目位于阵列中,因此最好的方法是使用

示例代码:

db.test.aggregate(
    // Find matching documents (could take advantage of an index)
    { $match: {
        "standard.item.code" : 151001,
    }},

    // Unpack the item array into a stream of documents
    { $unwind: "$standard.item" },

    // Filter to the items that match the code
    { $match: {
        "standard.item.code" : 151001,
    }},

    // Only show the delivered amounts
    { $project: {
        _id: 0,
        delivered: "$standard.item.delivered"
    }}
)
结果:

{
    "result" : [
        {
            "delivered" : 8
        },
        {
            "delivered" : 6
        }
    ],
    "ok" : 1
}

您会注意到聚合中有两个
$match
步骤。第一个是匹配包含该项目代码的文档。在阵列上使用
$unwind
后,第二个
$match
限制具有该代码的项目。

由于您的项目位于阵列中,因此最好的方法是使用

示例代码:

db.test.aggregate(
    // Find matching documents (could take advantage of an index)
    { $match: {
        "standard.item.code" : 151001,
    }},

    // Unpack the item array into a stream of documents
    { $unwind: "$standard.item" },

    // Filter to the items that match the code
    { $match: {
        "standard.item.code" : 151001,
    }},

    // Only show the delivered amounts
    { $project: {
        _id: 0,
        delivered: "$standard.item.delivered"
    }}
)
结果:

{
    "result" : [
        {
            "delivered" : 8
        },
        {
            "delivered" : 6
        }
    ],
    "ok" : 1
}

您会注意到聚合中有两个
$match
步骤。第一个是匹配包含该项目代码的文档。在数组上使用
$unwind
后,第二个
$match
限制使用该代码的项。

最好将聚合框架与
$unwind
$project
$match
一起使用。您能给我一个例子吗?最好将聚合框架与
$unwind
一起使用,
$project
$match
你能给我举个例子吗?最好将聚合框架与
$unwind
$project
$match
一起使用吗?你能给我举个例子吗?最好将聚合框架与
$unwind
$project
$match
一起使用吗?