【问题标题】:MongoDB Unwind array result manipulationMongoDB Unwind 数组结果操作
【发布时间】:2017-10-03 02:45:58
【问题描述】:

我有以下 MongoDB 查询:

它返回两个集合的一些行,一吨具有 362 网络代码,然后我将这两个集合“加入”到一个包含第二个信息的新数组中,这些数组中我提取了“组”。

     db.events.aggregate([

       { $match : { netcode : "N000362" } },

        {
          $lookup:
            {
              from: "nodes",
              localField: "name",
          foreignField: "name",
          as: "event_joined"


        }

   },


  { $unwind:  {path: "$event_joined"}},


  { $project : { name : 1, event : 1, "event_joined.group": 1 ,_id:0} },







])

它返回以下结果(还有更多行):

/* 1 */
{
    "name" : "GFS_3MSTAFE_RT01",
    "event" : "Proactive Interface Output Utilisation",
    "event_joined" : {
        "group" : "GFS-SUCURSALES"
    }
}

/* 2 */
{
    "name" : "GFS_3MSTAFE_RT01",
    "event" : "Proactive Interface Output Utilisation",
    "event_joined" : {
        "group" : "GFS-SUCURSALES"
    }
}

我想要什么:

   /* 1 */
{
    "name" : "GFS_3MSTAFE_RT01",
    "event" : "Proactive Interface Output Utilisation",
    "group" : "GFS-SUCURSALES"

}

/* 2 */
{
    "name" : "GFS_3MSTAFE_RT01",
    "event" : "Proactive Interface Output Utilisation",  
    "group" : "GFS-SUCURSALES"

}

有没有办法做到这一点?

我尝试了另一个放松,但没有。

【问题讨论】:

  • 试试{ $project : { name : 1, event : 1, group:"$event_joined.group", _id:0 } }
  • 确实是的,非常感谢:D 如此简单的语法应该在 MongoDB 文档中,在 $project 部分中... Pffff ...我不知道如何将其标记为答案,但绝对是答案

标签: mongodb aggregation-framework


【解决方案1】:

使用computed fields 投影嵌入文档中的值。

{ $project : { name : 1, event : 1, group:"$event_joined.group", _id:0 } }

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 1970-01-01
    • 2019-02-19
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    相关资源
    最近更新 更多