Skip to content

Instantly share code, notes, and snippets.

@masa2146
Last active January 6, 2022 10:08
Show Gist options
  • Save masa2146/daee1b6f1cb5652c8b985a2069f4ab39 to your computer and use it in GitHub Desktop.
Save masa2146/daee1b6f1cb5652c8b985a2069f4ab39 to your computer and use it in GitHub Desktop.
Get nested arrays in Spring Boot Mongo

Add Data Only Nested Array Without Update All Data

mongoTemplate.updateFirst(
        Query.query(Criteria.where("machineId").is(machineId).and("sensorData.key").is(key)),
        new Update().push("sensorData.$.sensorValues", sensorValue),
        MachineData.class);

Remove First Data From Nested Array Without Get All Data

mongoTemplate.updateFirst(query,
        new Update().pop("sensorData.$.sensorValues", Update.Position.FIRST),
        MachineData.class);

Filter Data By Nested Array Value

Using With MongoTemplate
AggregationOperation match = Aggregation.match(Criteria.where("machineId").is(machineId));
AggregationOperation unwind = Aggregation.unwind("sensorData");
MatchOperation sensorMatch = Aggregation.match(Criteria.where("sensorData.key").is(sensorKey));
AggregationOperation replaceRoot = Aggregation.replaceRoot("sensorData");
Aggregation aggregation = Aggregation.newAggregation(match, unwind, sensorMatch, replaceRoot);

mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(MachineData.class), SensorData.class).getMappedResults();
Using With Spring Data Operations
@Aggregation(pipeline = {"{ $match: { 'machineId': ?0 } }", "{ $unwind: '$sensorData' }",
        "{ $match: { 'sensorData.key' : ?1 } }", "{ $replaceRoot: { newRoot: '$sensorData' } }"})

Filter Data By Timestamp of Sub Array of Nested Array

Using With MongoTemplate
AggregationOperation match = Aggregation.match(Criteria.where("machineId").is(machineId));
AggregationOperation sensorDataUnwind = Aggregation.unwind("sensorData");
MatchOperation sensorMatch = Aggregation.match(Criteria.where("sensorData.key").is(sensorKey));

AggregationOperation sensorValuesUnwind = Aggregation.unwind("sensorData.sensorValues");
MatchOperation sensorValuesMatch = Aggregation.match(Criteria.where("sensorData.sensorValues.timeStamp").is(OffsetDateTime.parse("2021-05-02T20:31:59Z")));


AggregationOperation replaceRoot = Aggregation.replaceRoot("sensorData.sensorValues");

Aggregation aggregation = Aggregation.newAggregation(match, sensorDataUnwind, sensorValuesUnwind, sensorMatch, sensorValuesMatch, replaceRoot);

mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(MachineData.class), SensorValue.class).getMappedResults();
Using With Spring Data Operations
@Aggregation(pipeline = {"{ $match: { 'machineId': ?0}}", "{ $unwind: '$sensorData' }",
        "{ $match: { 'sensorData.key' : ?1 } }", "{ $unwind: '$sensorData.sensorValues' }",
        "{ $match: { 'sensorData.sensorValues.timeStamp' : '2021-05-02T20:31:59Z' } }",
        "{ $replaceRoot: { newRoot: '$sensorData.sensorValues' } }"})
Using With Spring Data Operations With 'Skip' and 'Limit'
@Aggregation(pipeline = {"{ $match: { 'machineId': ?0}}", "{ $unwind: '$sensorData' }",
        "{ $match: { 'sensorData.key' : ?1 } }", "{ $unwind: '$sensorData.sensorValues' }",
        "{ $replaceRoot: { newRoot: '$sensorData.sensorValues' } }",
        "{ $skip: ?2 }", " { $limit: ?3 } "})

Sample Payload

[{
  "_id": "b01ec008-e548-4a2d-96f1-b20f64b364ba",
  "machineId": "c323220b-84c8-4476-87f0-e2f61dff8c20",
  "sensorData": [
    {
      "key": "weight",
      "sensorValues": [
        {
          "value": 1.3979623,
          "timeStamp": "2021-03-02T20:30:59Z"
        }
      ]
    },
    {
      "key": "weightKey",
      "sensorValues": [
        {
          "value": 31,
          "timeStamp": "2021-05-02T20:31:59Z"
        },
        {
          "value": 31,
          "timeStamp": "2021-05-02T20:30:59Z"
        },
        {
          "value": 313,
          "timeStamp": "2021-05-02T20:30:59Z"
        },
        {
          "value": 571,
          "timeStamp": "2021-05-02T20:30:59Z"
        },
        {
          "value": 571,
          "timeStamp": "2021-05-02T20:30:59Z"
        }
      ]
    },
    {
      "key": "temperatureKey",
      "sensorValues": [
        {
          "value": " 23",
          "timeStamp": "2021-05-02T20:30:59Z"
        },
        {
          "value": " 23",
          "timeStamp": "2021-05-02T20:30:59Z"
        },
        {
          "value": " 23",
          "timeStamp": "2021-05-02T20:30:59Z"
        },
        {
          "value": " 23",
          "timeStamp": "2021-05-02T20:30:59Z"
        },
        {
          "value": " 23",
          "timeStamp": "2021-05-02T20:30:59Z"
        }
      ]
    }
  ],
  "_class": "com.ef.monitoring.backend.model.machine.MachineData"
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment