【问题标题】:Mongo SORT with specific entry from list of attributeMongo SORT 与属性列表中的特定条目
【发布时间】:2020-10-15 14:06:14
【问题描述】:

我正在使用 Mongo 聚合/过滤/排序并使用 JAVA 进行限制。

我有以下模型对象。 CustomPOJO 是我的数据库集合。

@Document(collation = "CustomPOJO")
public class CustomPOJO {

    public List<KeyValueObject> valueList;
    public String attribute1;
    public String attribute2;
    public CustomObject attribute3;

}

public class CustomObject {
    public String attribute4;
    public String attribute5;
}

public class KeyValueObject {
    public String name;
    public String value;
}

现在我的数据库中有以下记录的条目

记录1:

{
    "valueList": [{
        "name": "field1",
        "value": "value1"
    }, {
        "name": "field2",
        "value": "value2"
    }, {
        "name": "field3",
        "value": "value3"
    }],
    "attribute1": "attribute1",
    "attribute2": "attribute2",
    "attribute3": {
        "attribute4": "attribute4",
        "attribute5": "attribute5"
    }

}

记录2:

{
    "valueList": [{
        "name": "field1",
        "value": "value4"
    }, {
        "name": "field2",
        "value": "value5"
    }, {
        "name": "field3",
        "value": "value6"
    }],
    "attribute1": "attribute11",
    "attribute2": "attribute22",
    "attribute3": {
        "attribute4": "attribute44",
        "attribute5": "attribute55"
    }

}

我可以使用 sort(DESC,"attribute1")sort(DESC,"attribute3.attribute4")sort(DESC,"attribute2") 对所有字段进行排序。但我无法对 valueList 进行排序。我想根据 valueList 中的特定 KeyValue 条目进行排序。

例如,record1 的 valueList 包含条目 field1,因此 DB 中的所有记录都应根据 field1 和其中的值进行排序。

一般来说,我想对列表属性内的字段进行排序。

任何帮助将不胜感激。

【问题讨论】:

    标签: java mongodb sorting mongo-java-driver


    【解决方案1】:

    您不能就地使用数组的键值对进行排序。您可以使用聚合框架。

    投影文档匹配键条目,然后对值进行排序并从文档中删除附加字段。

    类似

    db.collection.aggregate(
    [
      {"$set":{
        "sortdoc":{
          "$arrayElemAt":[
            {
              "$filter":{
                "input":"$valueList",
                "cond":{"$eq":["$$this.name","field1"]}
              }
            },0]}
      }},
      {"$sort":{"sortdoc.value":-1}},
      {"$project":{"sortdoc":0}}
    ])
    

    https://mongoplayground.net/p/gzLQm8m2wQW

    您也可以将 valueList 建模为对象,就像您对属性的方式一样。使用 Document 类或 HashMap 而不是 List of values 并使用 sort(DESC,"document.field1")

    @Document(collation = "CustomPOJO")
    public class CustomPOJO {
    
      public Document document;
      public String attribute1;
      public String attribute2;
      public CustomObject attribute3;
    
    }
    

    文档

    {
        "document": {
          "field1": "value1",
          "field2": "value2"
        },
        "attribute1": "attribute1",
        "attribute2": "attribute2",
        "attribute3": {
          "attribute4": "attribute4",
          "attribute5": "attribute5"
        }
    }
    

    【讨论】:

      【解决方案2】:

      您不能直接对 Arrays 内的字段中的文档进行排序。一般我做的是unwind数组然后进行排序。

      也许这对你有帮助。

      db.collection.aggregate([
        {
          "$unwind": "$valueList"
        },
        {
          "$sort": {
            "valueList.name": -1
          }
        }
      ])
      

      Mongo Playground

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-16
        • 2014-12-29
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多