Convert mongo aggregate(with group and size) to query in spring data
up vote
0
down vote
favorite
sin my MongoDB I have documents like this:
{
"_id" : ObjectId("5bf3cd1c6b86db2c84d"),
"_class" : "Log",
"data" : {
"devices" : [
{
"id": "12364",
"name":"name"
},
{
"id": "12346",
"name":"name"
},
{
"id": "12345",
"name":"name"
}
]
}
}
I wanted to return number of devices, so I created query:
db.getCollection('coll').aggregate(
{$group: { _id: null, totalSize: { $sum: { $size: "$data.devices"}} }})
which returns me:
{ "_id" : null, "totalSize" : 6 }
But how to create method to return "totalSize" in Spring Data?
I get stuck and I dont know how to solve it:
public Long countDevices() {
GroupOperation groupOperation= Aggregation.group("_id").sum("_id");
Aggregation aggregation = Aggregation.newAggregation(groupOperation);
mongoOperations.aggregate(matchOperation,)
return ...
}
below solution seems to work, but its not looks so pretty and performance:
public Long count(Criteria searchCriteria) {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(searchCriteria),
Aggregation.unwind("data.device")
);
AggregationResults aggregationResults = mongoOperations.aggregate(aggregation,"coll", DBObject.class);
return (long) aggregationResults.getMappedResults().size();
}
spring mongodb spring-data spring-data-mongodb
add a comment |
up vote
0
down vote
favorite
sin my MongoDB I have documents like this:
{
"_id" : ObjectId("5bf3cd1c6b86db2c84d"),
"_class" : "Log",
"data" : {
"devices" : [
{
"id": "12364",
"name":"name"
},
{
"id": "12346",
"name":"name"
},
{
"id": "12345",
"name":"name"
}
]
}
}
I wanted to return number of devices, so I created query:
db.getCollection('coll').aggregate(
{$group: { _id: null, totalSize: { $sum: { $size: "$data.devices"}} }})
which returns me:
{ "_id" : null, "totalSize" : 6 }
But how to create method to return "totalSize" in Spring Data?
I get stuck and I dont know how to solve it:
public Long countDevices() {
GroupOperation groupOperation= Aggregation.group("_id").sum("_id");
Aggregation aggregation = Aggregation.newAggregation(groupOperation);
mongoOperations.aggregate(matchOperation,)
return ...
}
below solution seems to work, but its not looks so pretty and performance:
public Long count(Criteria searchCriteria) {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(searchCriteria),
Aggregation.unwind("data.device")
);
AggregationResults aggregationResults = mongoOperations.aggregate(aggregation,"coll", DBObject.class);
return (long) aggregationResults.getMappedResults().size();
}
spring mongodb spring-data spring-data-mongodb
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
sin my MongoDB I have documents like this:
{
"_id" : ObjectId("5bf3cd1c6b86db2c84d"),
"_class" : "Log",
"data" : {
"devices" : [
{
"id": "12364",
"name":"name"
},
{
"id": "12346",
"name":"name"
},
{
"id": "12345",
"name":"name"
}
]
}
}
I wanted to return number of devices, so I created query:
db.getCollection('coll').aggregate(
{$group: { _id: null, totalSize: { $sum: { $size: "$data.devices"}} }})
which returns me:
{ "_id" : null, "totalSize" : 6 }
But how to create method to return "totalSize" in Spring Data?
I get stuck and I dont know how to solve it:
public Long countDevices() {
GroupOperation groupOperation= Aggregation.group("_id").sum("_id");
Aggregation aggregation = Aggregation.newAggregation(groupOperation);
mongoOperations.aggregate(matchOperation,)
return ...
}
below solution seems to work, but its not looks so pretty and performance:
public Long count(Criteria searchCriteria) {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(searchCriteria),
Aggregation.unwind("data.device")
);
AggregationResults aggregationResults = mongoOperations.aggregate(aggregation,"coll", DBObject.class);
return (long) aggregationResults.getMappedResults().size();
}
spring mongodb spring-data spring-data-mongodb
sin my MongoDB I have documents like this:
{
"_id" : ObjectId("5bf3cd1c6b86db2c84d"),
"_class" : "Log",
"data" : {
"devices" : [
{
"id": "12364",
"name":"name"
},
{
"id": "12346",
"name":"name"
},
{
"id": "12345",
"name":"name"
}
]
}
}
I wanted to return number of devices, so I created query:
db.getCollection('coll').aggregate(
{$group: { _id: null, totalSize: { $sum: { $size: "$data.devices"}} }})
which returns me:
{ "_id" : null, "totalSize" : 6 }
But how to create method to return "totalSize" in Spring Data?
I get stuck and I dont know how to solve it:
public Long countDevices() {
GroupOperation groupOperation= Aggregation.group("_id").sum("_id");
Aggregation aggregation = Aggregation.newAggregation(groupOperation);
mongoOperations.aggregate(matchOperation,)
return ...
}
below solution seems to work, but its not looks so pretty and performance:
public Long count(Criteria searchCriteria) {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(searchCriteria),
Aggregation.unwind("data.device")
);
AggregationResults aggregationResults = mongoOperations.aggregate(aggregation,"coll", DBObject.class);
return (long) aggregationResults.getMappedResults().size();
}
spring mongodb spring-data spring-data-mongodb
spring mongodb spring-data spring-data-mongodb
asked 16 hours ago
Adrian Wąt
568
568
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407868%2fconvert-mongo-aggregatewith-group-and-size-to-query-in-spring-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown