简体   繁体   中英

Equivalent of $objectToArray using Mongodb java driver 3

I am moving a Mongodb query from Javascript to Java. The object format is as follows:

{
  "record": {
    "unknownName1": {
      "count": 5,
      "domain": "domain1"
    }, {
    ...
    }, {
    "unknownNameN": {
      "count": 3,
      "domain": "domainN"
    }
  }
}

The Javascript query has the following portion:

[
  { 
    $project: {
      record: { 
        $objectToArray: "$record" 
      }
    }
  }, {
    $unwind: { "$record"
  }, {
    $group: {
      device: "$record.k"
    }, 
    count: {
      $sum: "$record.v.count"
    },
    domain: {
      $min: "$record.v.domain"
    }
  }
]

I have translated the above to use the Mongodb Java Driver 3 api and have the following:

List<Bson> query = Arrays.asList(
    project(include("record")),
    unwind("$record"),
    group(computed("device", "$record.k"),
        sum("count", "$record.v.count"),
        min("domain", "$record.v.domain"))
);

The issue I am having is I can't seem to find an equivalent to $objectToArray using the Mongodb Java Driver and the subsequent sum and min operations depend on dot operating the k and v values generated from using $objectToArray.

Is there an equivalent way to map an object with unknown key names into the k and v format used by $objectToArray using the Mongodb Java Driver, preferrably version 3+?

下面会做。

project(computed("record", eq("$objectToArray", "$record")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM