【问题标题】:Mongo aggregation in javajava中的Mongo聚合
【发布时间】:2014-06-16 14:35:16
【问题描述】:

嗨,我的 mongo 收藏包含以下文档:

  {
      "_id" : ObjectId("539efd5b254bb5f65c9da94e"),
      "iInfo" : [ 
          { "ifout" : 0, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 },
          { "ifout" : 0, "iferror" : 10, "ifdes" : "N/A", "ifin" : 0 },
          { "ifout" : 0, "iferror" : 4, "ifdes" : "N/A", "ifRemotePort" : "0", "ifin" : 0 },
          { "ifout" : 0, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 } 
      ]
  }
  { 
      "_id" : ObjectId("539efd76254bb5f65c9da94f"),
      "iInfo" : [ 
          { "ifout" : 0, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 },
          { "ifout" : 0, "iferror" : 7, "ifdes" : "N/A", "ifin" : 0 },
          { "ifout" : 0, "iferror" : 4, "ifdes" : "N/A", "ifRemotePort" : "0", "ifin" : 0 },
          { "ifout" : 110, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 }
          { "ifout" : 90, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 }
      ]
  }
  {
      "_id" : ObjectId("539efed7254bb5f65c9da950"),
     "iInfo" : [ 
         { "ifout" : 10, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 }, 
         { "ifout" : 0, "iferror" : 10, "ifdes" : "N/A", "ifin" : 0 },
         { "ifout" : 0, "iferror" : 4, "ifdes" : "N/A", "ifRemotePort" : "0", "ifin" : 0 },
         { "ifout" : 0, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 }
     ] 
  }
  { 
      "_id" : ObjectId("539efeed254bb5f65c9da951"),
      "iInfo" : [
          { "ifout" : 0, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 },
          { "ifout" : 0, "iferror" : 10, "ifdes" : "N/A", "ifin" : 0 },
          { "ifout" : 0, "iferror" : 4, "ifdes" : "N/A", "ifRemotePort" : "0", "ifin" : 0 },
          { "ifout" : 100, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 },
          { "ifout" : 210, "iferror" : 0, "ifdes" : "N/A", "ifin" : 0 }
      ]
  }

现在我想对这些文档进行如下排序:

  { 
      "_id" : ObjectId("539efeed254bb5f65c9da951"),
      "iInfo" : [ 
          { "out" : 210,"iferror" : 0,"ifdes" : "N/A","ifin" : 0} 
      ]
  }
{ 
      "_id" : ObjectId("539efd76254bb5f65c9da94f"),
      "iInfo" : [ 
          { "out" : 110,"iferror" : 0,"ifdes" : "N/A","ifin" : 0} 
      ]
  }
{ 
      "_id" : ObjectId("539efeed254bb5f65c9da951"),
      "iInfo" : [ 
          { "out" : 100,"iferror" : 0,"ifdes" : "N/A","ifin" : 0} 
      ]
  }
  { 
      "_id" : ObjectId("539efed7254bb5f65c9da950"),
      "iInfo" : [
          { "out" : 90,"iferror" : 0,"ifdes" : "N/A","ifin" : 0} 
      ]
  }

为此,我将我的 mongo 查询编写如下

db.demo.aggregate([
    { "$project": { "_id": 1, "out": "$iInfo.ifout"} },
    { "$unwind": "$out" },
    { "$sort": { "_id": 1, "out": -1 } },
    { "$group": { "_id": "$_id" , "iInfo": { "$push": { "out":"$out" } } } }
 ])

上面的查询返回如下输出:

{
    "_id" : ObjectId("539efeed254bb5f65c9da951"),
    "iInfo" : [ 
        { "out" : 100 }, 
        { "out" : 0 },
        { "out" : 0 }, 
        { "out" : 0 }
    ]
}
{ 
    "_id" : ObjectId("539efed7254bb5f65c9da950"),
    "iInfo" : [ 
        { "out" : 10 },
        { "out" : 0 },
        { "out" : 0 },
        { "out" : 0 }
    ]
}
{
    "_id" : ObjectId("539efd76254bb5f65c9da94f"),
    "iInfo" : [
        { "out" : 90 },
        { "out" : 0 },
        { "out" : 0 },
        { "out" : 0 } 
    ]
}
{
    "_id" : ObjectId("539efd5b254bb5f65c9da94e"),
    "iInfo" : [
        { "out" : 0 },
        { "out" : 0 },
        { "out" : 0 },
        { "out" : 0 }
    ]
}

那么,如何使用 mongo 和 java 代码获得期望的输出?

【问题讨论】:

标签: java mongodb aggregation-framework mongo-java


【解决方案1】:

或多或少在正确的轨道上,但是由于您似乎想要最高的排序值,因此您需要在 $group 时使用 $first 运算符进行过滤:

db.demo.aggregate([
    // Match documents with non 0 values in the array
    { "$match": { "iInfo.ifout": { "$ne": 0 } }},

    // Unwind the array
    { "$unwind": "$iInfo" },

    // Filter any array elements that have 0 - also removes documents where all are 0
    { "$match": { "iInfo.ifout": { "$ne": 0 } }},

    // Sort the array elements within documents
    { "$sort": { "_id": -1, "iInfo.ifout": -1 } },

    // Take only the "first" array element per document
    { "$group": {
        "_id": "$_id",
        "Iifout": { "$first": "$iInfo.ifout" },
        "Iiferror": { "$first": "$iInfo.iferror" },
        "Iifdes": { "$first": "$iInfo.ifdes" },
        "Iifin": { "$first": "$iInfo.ifin" }
    }},

    // Group to push those results as an array
    { "$group": {
        "_id": "$_id",
        "iInfo": {
            "$push": {
                "ifout": "$Iifout",
                "iferror": "$Iiferror",
                "Iifdes": "$Iifdes",
                "Iifin": "$Iifin"
            }
        }
    }}
])

还要注意您想要的输出是_id 降序排列。

$group 的聚合操作是分开的,并且您不能在“组”阶段使用子文档键,因此如果您希望将事物作为数组返回,您可以按照第二个$group 管道进行操作,如图所示$push 值作为数组字段。

【讨论】:

  • 嗨,它工作正常,而不是 "$sort": { "_id": -11 我替换 "$sort": { "_id": -1 它工作
  • @yogesh 只是一个错字。我一定是在打字的时候把那个“胖手指”了。
  • @yogesh 我敢肯定。当有人帮助你时,接受和投票是这里常用的货币。
  • 嗨,我可以在 mongo 中更改我的输出结构,有什么提示或帮助吗?
  • @yogesh 我在输出中看不出与您发布的内容有什么不同。唯一可能的区别是出现所有 0 值的文档。现在还有其他 $match 过滤器正在为此准备中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 2017-05-02
  • 2019-10-14
  • 2021-12-21
  • 2018-07-15
相关资源
最近更新 更多