Skip to content

Instantly share code, notes, and snippets.

@masa2146
Created November 25, 2021 18:57
Show Gist options
  • Save masa2146/8ec74a92474a21b1273484b400b12c33 to your computer and use it in GitHub Desktop.
Save masa2146/8ec74a92474a21b1273484b400b12c33 to your computer and use it in GitHub Desktop.
mongo_query.md
## 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
```
##### 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 } "})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment