Mongo聚合,投影数组中第一个元素的子字段 [英] Mongo aggregation, project a subfield of the first element in the array

查看:104
本文介绍了Mongo聚合,投影数组中第一个元素的子字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元素子集合,我想在该集合中投影FIRST项目的某个子字段.我有以下内容,但它仅投影数组中所有元素的字段.

I have a sub collection of elements and I want to project a certain subfield of the FIRST item in this collection. I have the following but it only projects the field for ALL elements in the array.

Items是Orders的子集合,每个Item对象都有一个Details子对象和一个在其下的ItemName.我只想返回列表中FIRST项目的项目名称.这将返回列表中每个项目的项目名称.

Items is the subcollection of Orders and each Item object has a Details sub object and an ItemName below that. I want to only return the item name of the FIRST item in the list. This returns the item name of every item in the list.

我该如何调整?

db.getCollection('Orders').aggregate
([
{ $match : { "Instructions.1" : { $exists : true }}},
{ $project: { 
    _id:0, 
    'UserId': '$User.EntityId', 
    'ItemName': '$Items.Details.ItemName'
   }
}
]);

已更新:

{
    "_id" : "order-666156",
    "State" : "ValidationFailed",
    "LastUpdated" : {
        "DateTime" : ISODate("2017-09-26T08:54:16.241Z"),
        "Ticks" : NumberLong(636420128562417375)
    },
    "SourceOrderId" : "666156",
    "User" : {
        "EntityId" : NumberLong(34450),
        "Name" : "Bill Baker",
        "Country" : "United States",
        "Region" : "North America",
        "CountryISOCode" : "US",
    },
    "Region" : null,
    "Currency" : null,
    "Items" : [ 
        {
            "ClientOrderId" : "18740113",
            "OrigClientOrderId" : "18740113",
            "Quantity" : NumberDecimal("7487.0"),
            "TransactDateTime" : {
                "DateTime" : Date(-62135596800000),
                "Ticks" : NumberLong(0)
            },
            "Text" : null,
            "LocateRequired" : false,
            "Details" : {
                "ItemName" : "Test Item 1",
                "ItemCost" : 1495.20
            }
        },
        {
            "ClientOrderId" : "18740116",
            "OrigClientOrderId" : "18740116",
            "Quantity" : NumberDecimal("241.0"),
            "TransactDateTime" : {
                "DateTime" : Date(-62135596800000),
                "Ticks" : NumberLong(0)
            },
            "Text" : null,
            "LocateRequired" : false,
            "Details" : {
                "ItemName" : "Test Item 2",
                "ItemCost" : 2152.64
            }
        }
    ]

}

推荐答案

如果您至少使用MongoDB v3.2,则可以使用过滤器会删除样本文档.

In case your are using at least MongoDB v3.2 you can use the $arrayElemAt operator for that. The below query does what you want. It will, however, not return any data for the sample you provided because the "Instructions.1": { $exists: true } filter removes the sample document.

db.getCollection('Orders').aggregate([{
    $match: {
        "Instructions.1": {
            $exists: true
        }
    }
}, {
    $project: { 
        "_id": 0, 
        "UserId": "$User.EntityId", 
        "ItemName": { $arrayElemAt: [ "$Items.Details.ItemName", 0 /* first item! */] }
    }
}])

这篇关于Mongo聚合,投影数组中第一个元素的子字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆