I want to get data from MongoDB through Java application using Spring Data.
I did following MongoDB query and converted successfully to Java code
db.getCollection('financialMessage').aggregate([{
$match:{ createdDate: {
$gte: ISODate("2017-11-03 00:00:00.000Z"),
$lt: ISODate("2017-11-04 00:00:00") }}}, {
$group: { _id: {
consolidatedBatchId: "$consolidatedBatchId",
version: "$version"},
messages: { $addToSet: "$message" }}}, {
$sort: {
"_id.consolidatedBatchId": 1,
"_id.version": 1}
}])
The results looks like :
{
"_id" : {
"consolidatedBatchId" : "5f4e1d16-2070-48ef-8369-00004ec3e8ee",
"version" : 4
},
"messages" : [
"message1",
"message2",
"message3"
]
}
Java code for above query looks like :
Criteria filterCriteria = Criteria.where(CREATED_DATE)
.gte(startDate)
.lt(endDate);
Sort sort = new Sort(Sort.Direction.DESC, "consolidatedBatchId" ,"version");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(filterCriteria),
Aggregation.group("consolidatedBatchId", "version")
.addToSet("message").as("messages"),
Aggregation.sort(sort)
);
AggregationResults<FinancialMessageKey> aggregationResults =
mongoTemplate.aggregate(agg, FinancialMessage.class, FinancialMessageKey.class);
return aggregationResults.getMappedResults();
Now I do not find how to convert following MongoDB query code to Java code :
db.getCollection('financialMessage').aggregate([{
$match:{ createdDate: {
$gte: ISODate("2017-11-03 00:00:00.000Z"),
$lt: ISODate("2017-11-04 00:00:00")
}}}, {
$group: { _id: {
consolidatedBatchId: "$consolidatedBatchId",
version: "$version"},
messages: { $addToSet: {message: "$message",
createdDate: "$createdDate",
sender: "$sender",
receiver: "$receiver" }}}}, {
$sort: {
"_id.consolidatedBatchId": 1,
"_id.version": 1}
}])
With the following output :
{
"_id" : {
"consolidatedBatchId" : "5f4e1d16-2070-48ef-8369-00004ec3e8ee",
"version" : 4
},
"messages" : [
{
"message" : "message1",
"createdDate" : ISODate("2017-11-03T07:13:08.074Z"),
"sender" : "sender",
"receiver" : "receiver"
},
{
"message" : "message2",
"createdDate" : ISODate("2017-11-03T07:13:08.111Z"),
"sender" : "sender",
"receiver" : "receiver"
},
{
"message" : "message3",
"createdDate" : ISODate("2017-11-03T07:13:07.986Z"),
"sender" : "sender",
"receiver" : "receiver"
}
]
}
How this addToSet() could be write in Java in order to get List<'complex object'> instead of simple List ?
After few minutes of research on Google, I finally found how to do it.
public List<FinancialMessageKey> findFinancialMessageKeys(FinancialMessageQueryDTO financialMessageQueryDTO) {
Criteria filterCriteria = Criteria.where(CREATED_DATE)
.gte(startDate)
.lt(endDate);
Sort sort = new Sort(Sort.Direction.DESC, "consolidatedBatchId", "version");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(filterCriteria),
Aggregation.group(CONSOLIDATED_BATCH_ID, VERSION)
.addToSet(new BasicDBObject() {
{
put(CREATED_DATE, "$" + CREATED_DATE);
put(SENDER, "$" + SENDER);
put(RECEIVER, "$" + RECEIVER);
put(MESSAGE, "$" + MESSAGE);
}
}
).as("messages"),
Aggregation.sort(sort));
AggregationResults<FinancialMessageKey> aggregationResults =
mongoTemplate.aggregate(agg, FinancialMessage.class, FinancialMessageKey.class);
return aggregationResults.getMappedResults();
}
Related
Let's assume that there are some data like this:
{
"orderId": 1,
"manager" : [
{
"userId" : "UserId1"
}
],
"employee" : [
{
"userId" : "UserId3"
}
]
}
{
"orderId": 2,
"manager" : [
{
"userId" : "UserId1"
}
],
"employee" : [
{
"userId" : "UserId2"
}
]
}
{
"orderId": 3,
"manager" : [
{
"userId" : "UserId1"
}
],
"employee" : [
{
"userId" : "UserId2"
}
]
}
The result should be:
{
"Agg":[
{
"userId":"UserId1",
"total":3
},
{
"userId":"UserId2",
"total":2
},
{
"userId":"UserId3",
"total":1
}
]
}
I want to get all aggregated count of userId engaged in some process. I need to group by userId from "employee" and "manager" objects and sum them. Only what I can dois group by userId from one list:
Aggregation agg = newAggregation(
match(Criteria.where("project").is("project")),
unwind("manager"),
group("manager.userId").count().as("total"),
project("total").and("userId").previousOperation(),
sort(Sort.Direction.DESC, "total")
);
How can I aggregate count from "manager" and "employee" fields?
Use $concatArrays.
Aggregation agg = newAggregation(
match(Criteria.where("project").is(project)),
project("employee", "manager"),
project().and("manager").concatArrays("employee").as("merged"),
unwind("merged"),
group("merged.userId").count().as("total"),
project("total").and("userId").previousOperation(),
sort(Sort.Direction.DESC, "total")
);
Try something like:
"$group' : {
'_id' : '$id',
'totalManager' : { '$sum' : '$manager.total' },
'totalemployee' : { '$sum' : '$employee.total' }
} }
{ "$project" : {
'totalManager' : '$totalManager',
'totalemployee' : '$totalemployee',
'totalSum' : { '$add' : [ '$totalManager', '$totalemployee' ] },
}
I need to find the record/count where eventDetails.eventDelivery.stateCode = "Y-FINISH". It could be in any element in the array list.
Just want to find where stateCode = "Y-FINISH" present in the array list.
Sample Data #1:
{
"_id" : ObjectId("5a2fb09736cf07f6a1146691"),
"activityID" : "",
"eventDetails" : [
{
"eventDelivery" : {
"digital" : {
"secureid" : "1231321212"
},
"stateCode" : "X-FINISH",
"state" : "SUCCESS"
},
{
"eventDelivery" : {
"digital" : {
"secureid" : "8762871121"
},
"stateCode" : "Y-FINISH",
"state" : "SUCCESS"
}
],
}
Sample Data #2:
{
"_id" : ObjectId("5a2fb09736cf07f6a1146691"),
"activityID" : "",
"eventDetails" : [
{
"eventDelivery" : {
"digital" : {
"secureid" : "1231321212"
},
"stateCode" : "X-FINISH",
"state" : "SUCCESS"
},
{
"eventDelivery" : {
"digital" : {
"secureid" : "8762871121"
},
"stateCode" : "Y-FINISH",
"state" : "SUCCESS"
},
{
"eventDelivery" : {
"digital" : {
"secureid" : "7651327152
},
"stateCode" : "Z-FINISH",
"state" : "SUCCESS"
}
],
}
Need to read this using Java.
Using a 3.x version of the Mongo Java driver ...
MongoClient mongoClient = ...;
MongoCollection<Document> collection = mongoClient.getDatabase("...")
.getCollection("...");
Bson filter = Filters.eq("eventDetails.stateCode", "Y-FINISH");
long count = collection.count(filter);
I have following documents,
like,
{
"_id" : ObjectId("58a5446dddadff101aea67c6"),
"SubMenu" : [
{
"subMenuIconPath" : "images/hr/emp.png",
"screenID" : "Service Maintenance",
"subMenuName" : "Service Maintenance",
"orderBy" : 1.0,
"menuID" : "300"
},
{
"subMenuIconPath" : "images/hr/emp.png",
"screenID" : "Process Template Maintenance",
"subMenuName" : "Process Template Maintenance",
"orderBy" : 2.0,
"menuID" : "300"
}
],
"level" : "PARENT",
"orderBy" : 3,
"menuType" : "List",
"appID" : "dais-admin",
"menuName" : "Process and Service",
"menuID" : "300",
},
{
"_id" : ObjectId("58a5446dddadff101aea67c7"),
"SubMenu" : [
{
"subMenuIconPath" : "images/hr/emp.png",
"screenID" : "User Maintenance",
"subMenuName" : "User Maintenance",
"orderBy" : 1.0,
"menuID" : "100"
},
{
"subMenuIconPath" : "images/hr/emp.png",
"screenID" : "Role/ACL Maintenance",
"subMenuName" : "Role/ACL Maintenance",
"menuID" : "100"
}],
"level" : "PARENT",
"orderBy" : 1,
"menuType" : "List",
"appID" : "dais-admin",
"menuName" : "Security Settings",
"menuID" : "100",
}
Execute mongo command, change converted DOUBLE value to INTEGER for within array element of SubMenu.orderBy.
Note : SubMenu have Multiple Documents. And sometime SubMenu.orderBy Value is null also.
Can someone help me on this.
To convert dataType you can execute command in Mongo-shell,
By using update statment in forEach we can alter every single document.
db.collection.find({
SubMenu: {
$exists: true
}
}).forEach(function(myDoc) {
var child = myDoc.SubMenu;
for (var i = 0; i < child.length; i++) {
var ob = child[i];
if ('orderBy' in ob) {
ob.orderBy = NumberInt(ob.orderBy);
child[i] = ob;
}
}
db.collection.update({
_id: myDoc._id
}, {
$set: {
subMenu: child
}
});
printjson(myDoc);
});
you can try this command
db.menu.find({
SubMenu: {
$exists: true
}
}).forEach(function(obj) {
obj.SubMenu.forEach(function(childobject) {
if (childobject.orderBy != null) {
childobject.orderBy = NumberInt(childobject.orderBy);
}
});
db.menu.save(obj);
printjson(obj);
});
I have a MongoDB collection of places. A typical place has most of the following fields:
{
"_id" : ObjectId("575014dc6b028f07bef53681"),
"_class" : "domain.model.PlaceV1",
"name" : "Γιασεμί",
"primary_photo_url" : "https://irs0.4sqi.net/img/general/original/34666238_STHSh6CHiC7hpAuB4rztRVg6cFc5ylfi15aRaR7zUuQ.jpg",
"seenDetails" : NumberLong(0),
"foursquare_checkins" : 646,
"foursquare_tips" : 28,
"keywords" : [
""
],
"verified" : 1,
"location" : {
"loc" : {
"type" : "Point",
"coordinates" : [
25.898318,
36.831486
]
},
"formattedAddress" : "Χώρα",
"locality" : "Amorgos",
"first_neighbourhood" : "Katapola",
"greek_locality" : "Αμοργός",
"greek_first_neighbourhood" : "Κατάπολα"
},
"contact" : {
"phone_numbers" : [
"+30 2285 074017"
]
},
"price" : {
"priceVotes" : NumberLong(0),
"price" : 0,
"priceVotesSum" : NumberLong(0)
},
"rating" : {
"rating" : 8,
"ratingVotes" : NumberLong(0),
"ratingVotesSum" : NumberLong(0)
},
"categories" : [
{
"cat_id" : NumberLong(10310061000),
"category" : "Café",
"greek_category" : "Καφετέρια",
"weight" : 4
},
{
"cat_id" : NumberLong(11610021000),
"category" : "Bar",
"greek_category" : "Μπαρ",
"weight" : 4
}
]
}
I want to make queries where the sorting will be based on a score that is a result of some expressions and conditions. From the mongo shell I have tried this:
db.place.aggregate([
{$match:{"location.locality":"Athens"}},
{$project:
{name:1, location:1, score:{
$let: {
vars:{ foursquare: {
$cond: { if: { $gte: [ "$foursquare_checkins", 500 ] }, then: 500, else: "$foursquare_checkins" }
},
rating: {$multiply:["$rating.rating", 100]},
},
in:{$add:["$$foursquare", "$$rating", "$seenDetails"]}
}
}
}
},
{$sort: {score: -1}}]).pretty();
This is a simple example of my queries. The score will contain more complex expressions like the distance from a location. The problem is that I cannot find a way to use the $let and the $cond operator in my Java code with Spring. Could anybody help?
You should be able to do this using nested DBObject and a Custom Aggregation Operation.
For Example:
Map operations = new HashMap();
operations.put("name", 1);
operations.put("location", 1);
operations.put("score", new BasicDBObject("$let", new BasicDBObject("vars", new BasicDBObject())));
Then you can create a CustomAggregationOperation to add this to your project
CustomAggregationOperation project = new CustomAggregationOperation(new BasicDBObject("$project", operation));
This will give you the following pipeline:
{ "$project" : { "score" : { "$let" : { "vars" : { }}} , "name" : 1 , "location" : 1}}
Then you can add your other stages:
Aggregation aggregate = Aggregation.newAggregation(match, project, sort);
public class CustomAggregationOperation implements AggregationOperation {
private DBObject operation;
public CustomAggregationOperation (DBObject operation) {
this.operation = operation;
}
#Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
}
I'm new in mongo and use mongodb aggregation framework for my queries. I need to retrieve some records which satisfy certain conditions(include pagination+sorting) and also get total count of records.
Now, I perform next steps:
Create $match operator
{ "$match" : { "year" : "2012" , "author.authorName" : { "$regex" :
"au" , "$options" : "i"}}}
Added sorting and pagination
{ "$sort" : { "some_field" : -1}} , { "$limit" : 10} , { "$skip" : 0}
After querying I receive the expected result: 10 documents with all fields.
For pagination I need to know the total count of records which satisfy these conditions, in my case 25.
I use next query to get count : { "$match" : { "year" : "2012" , "author.authorName" : { "$regex" : "au" , "$options" : "i"}}} , { "$group" : { "_id" : "$all" , "reviewsCount" : { "$sum" : 1}}} , { "$sort" : { "some_field" : -1}} , { "$limit" : 10} , { "$skip" : 0}
But I don't want to perform two separate queries: one for retrieving documents and second for total counts of records which satisfy certain conditions.
I want do it in one single query and get result in next format:
{
"result" : [
{
"my_documets": [
{
"_id" : ObjectId("512f1f47a411dc06281d98c0"),
"author" : {
"authorName" : "author name1",
"email" : "email1#email.com"
}
},
{
"_id" : ObjectId("512f1f47a411dc06281d98c0"),
"author" : {
"authorName" : "author name2",
"email" : "email2#email.com"
}
}, .......
],
"total" : 25
}
],
"ok" : 1
}
I tried modify the group operator : { "$group" : { "_id" : "$all" , "author" : "$author" "reviewsCount" : { "$sum" : 1}}}
But in this case I got : "exception: the group aggregate field 'author' must be defined as an expression inside an object". If add all fields in _id then reviewsCount always = 1 because all records are different.
Nobody know how it can be implement in single query ? Maybe mongodb has some features or operators for this case? Implementation with using two separate query reduces performance for querying thousand or millions records. In my application it's very critical performance issue.
I've been working on this all day and haven't been able to find a solution, so thought i'd turn to the stackoverflow community.
Thanks.
You can try using $facet in the aggregation pipeline as
db.name.aggregate([
{$match:{your match criteria}},
{$facet: {
data: [{$sort: sort},{$skip:skip},{$limit: limit}],
count:[{$group: {_id: null, count: {$sum: 1}}}]
}}
])
In data, you'll get your list with pagination and in the count, count variable will have a total count of matched documents.
Ok, I have one example, but I think it's really crazy query, I put it only for fun, but if this example faster than 2 query, tell us about it in the comments please.
For this question i create collection called "so", and put into this collection 25 documents like this:
{
"_id" : ObjectId("512fa86cd99d0adda2a744cd"),
"authorName" : "author name1",
"email" : "email1#email.com",
"c" : 1
}
My query use aggregation framework:
db.so.aggregate([
{ $group:
{
_id: 1,
collection: { $push : { "_id": "$_id", "authorName": "$authorName", "email": "$email", "c": "$c" } },
count: { $sum: 1 }
}
},
{ $unwind:
"$collection"
},
{ $project:
{ "_id": "$collection._id", "authorName": "$collection.authorName", "email": "$collection.email", "c": "$collection.c", "count": "$count" }
},
{ $match:
{ c: { $lte: 10 } }
},
{ $sort :
{ c: -1 }
},
{ $skip:
2
},
{ $limit:
3
},
{ $group:
{
_id: "$count",
my_documets: {
$push: {"_id": "$_id", "authorName":"$authorName", "email":"$email", "c":"$c" }
}
}
},
{ $project:
{ "_id": 0, "my_documets": "$my_documets", "total": "$_id" }
}
])
Result for this query:
{
"result" : [
{
"my_documets" : [
{
"_id" : ObjectId("512fa900d99d0adda2a744d4"),
"authorName" : "author name8",
"email" : "email8#email.com",
"c" : 8
},
{
"_id" : ObjectId("512fa900d99d0adda2a744d3"),
"authorName" : "author name7",
"email" : "email7#email.com",
"c" : 7
},
{
"_id" : ObjectId("512fa900d99d0adda2a744d2"),
"authorName" : "author name6",
"email" : "email6#email.com",
"c" : 6
}
],
"total" : 25
}
],
"ok" : 1
}
By the end, I think that for big collection 2 query (first for data, second for count) works faster. For example, you can count total for collection like this:
db.so.count()
or like this:
db.so.find({},{_id:1}).sort({_id:-1}).count()
I don't fully sure in first example, but in second example we use only cursor, which means higher speed:
db.so.find({},{_id:1}).sort({_id:-1}).explain()
{
"cursor" : "BtreeCursor _id_ reverse",
"isMultiKey" : false,
"n" : 25,
"nscannedObjects" : 25,
"nscanned" : 25,
"nscannedObjectsAllPlans" : 25,
"nscannedAllPlans" : 25,
"scanAndOrder" : false,
!!!!!>>> "indexOnly" : true, <<<!!!!!
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
...
}
For completeness (full discussion was on the MongoDB Google Groups) here is the aggregation you want:
db.collection.aggregate(db.docs.aggregate( [
{
"$match" : {
"year" : "2012"
}
},
{
"$group" : {
"_id" : null,
"my_documents" : {
"$push" : {
"_id" : "$_id",
"year" : "$year",
"author" : "$author"
}
},
"reviewsCount" : {
"$sum" : 1
}
}
},
{
"$project" : {
"_id" : 0,
"my_documents" : 1,
"total" : "$reviewsCount"
}
}
] )
By the way, you don't need aggregation framework here - you can just use a regular find. You can get count() from a cursor without having to re-query.