Created
November 25, 2021 18:57
-
-
Save masa2146/8ec74a92474a21b1273484b400b12c33 to your computer and use it in GitHub Desktop.
mongo_query.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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