I am new to java. How can I build this mongo query in java. Any help/hint will be appreciated.
db.places.find({loc : { $near :[ -122.934326171875,37.795268017578] , $maxDistance : 50 } ,$or:[{"uid":"at"},{"myList.$id" :ObjectId("4fdeaeeede2d298262bb80") } ] ,"searchTag" : { $regex : "Union", $options: "i"} } );
By using the QueryBuilder you can create the query you wanted. I have created it as follows.
QueryBuilder query = new QueryBuilder();
query.put("loc").near(-122.934326171875, 37.795268017578, 50);
query.or(
QueryBuilder.start("uid").is("at").get(),
QueryBuilder.start("myList.$id").is(new ObjectId("5024f2f577a59dd9736ddc38")).get()
);
query.put("searchTag").regex(Pattern.compile("Union",Pattern.CASE_INSENSITIVE));
When I print the query into the console it looks like :
{ "loc" : { "$near" : [ -122.934326171875 , 37.795268017578 , 50.0]} ,
"$or" : [ { "uid" : "at"} , { "myList.$id" : { "$oid" : "5024f2f577a59dd9736ddc38"}}] ,
"searchTag" : { "$regex" : "Union" , "$options" : "i"}
}
I didn't try it but hope it will work.
Related
I have following aggregation query method:-
public List<String> fetchRelativeIds(String externalId) {
ProjectionOperation projectionOperation = project().and("ancestors._id")
.concatArrays("descendants._id").as("weNeedRelations").andInclude("_id");
MatchOperation filterByIds = Aggregation.match(new Criteria("externalIds.fenergo").is(externalId));
Aggregation aggregation = newAggregation(filterByIds, projectionOperation);
return mongoTemplate.aggregate(aggregation, Master.class.getSimpleName(), String.class).getMappedResults();
}
This gives me output as below:-
{ "_id" : { "$oid" : "5aafb705f3fabe7c915bca14"} , "weNeedRelations" : [ { "$oid" : "5aafb718f3fabe7c915bca1f"} , { "$oid" : "5aafb719f3fabe7c915bca23"} , { "$oid" : "5aafb6fbf3fabe7c915bca10"}]}
I just want the $oid value part as string nothing else. E.g.
{ "_id" : "5aafb705f3fabe7c915bca14" , "weNeedRelations" : [ "5aafb718f3fabe7c915bca1f" , "5aafb719f3fabe7c915bca23", "5aafb6fbf3fabe7c915bca10"]}
How could I do that?
I am trying to read the document of nested list of another list of a document using mongoTemplate of Java code, I have tried from almost a day for the same but facing difficulty in doing that, kindly help me to sort it out.
Document in my database is:
{
"_id" : "DEPT5",
"subDepartmentList" : [
{
"subDepartmentId" : "SUBDEPT19",
"subDepartmentName" : "ABCD",
"labServiceList" : [
{
"_id" : "123abc",
"departmentId" : "DEPT5",
"subDepartmentId" : "SUBDEPT19",
"labServiceName" : "serviceOne"
},
{
"_id" : "123def",
"departmentId" : "DEPT5",
"subDepartmentId" : "SUBDEPT19",
"labServiceName" : "hello"
}
]
},
{
"subDepartmentId" : "SUBDEPT21",
"subDepartmentName" : "IJKL",
"labServiceList" : [
{
"_id" : "456abc",
"departmentId" : "DEPT5",
"subDepartmentId" : "SUBDEPT21"
"labServiceName" : "hello"
}
]
}
]
}
Query in my shell window is as follows:
db.labServiceMasters.aggregate([{$unwind: '$subDepartmentList'},
{$unwind: '$subDepartmentList.labServiceList'},
{$match: {'subDepartmentList.labServiceList._id': '123def'}},
{$project: {_id: 0, labService: '$subDepartmentList.labServiceList'}}
])
The above query works fine in my shell window. I tried to implement the same query using Java code below, but it did not work.
DBObject unwind1 = new BasicDBObject("$unwind" , "$subDepartmentList");
unwind1 = new BasicDBObject("$unwind" , "$subDepartmentList.labServiceList");
DBObject match = new BasicDBObject("$match", new BasicDBObject("_id", "123def"));
DBObject project = new BasicDBObject("$project", new BasicDBObject("_id", 0)
.append("subDepartmentList.labServiceList", 1));
AggregationOutput output=mongoTemplate.getCollection(MasterCollection).aggregate(unwind1, match, project);
System.out.println("OUTPUT: "+output);
for(DBObject result: output.results()){
System.out.println("INside for each loop of Results.");
System.out.println(result);
}
Please help me to solve the above query. Any suggestions would be appreciable. Thanks in advance.
I have a collection with a dataset that looks like:
{
"resource" : "10.10.10.10",
"statistics" : {
"connections" : 17
}
}
{
"resource" : "10.10.10.10",
"statistics" : {
"connections" : 24
}
}
I want to use Mongo's $group/$push mechanism to return a dataset that looks like:
{ "_id" : "10.10.10.10", "statTotals" : [ 17, 24 ] }
In Mongo shell, I can that by doing:
db.testcol.aggregate([ { "$project" : { "resource" : 1 , "total" : "$statistics.connections"}} , { "$group" : { "_id" : "$resource" , "statTotals" : { "$push" : "$total"}}} ])
Now I want to do this using Spring's Mongo data solution in Java. The operations I'm currently trying to use are:
ProjectionOperation projOper = Aggregation.project("resource").and("statistics.connections").as("total");
GroupOperation groupOper = Aggregation.group("resource").push("total").as("statTotals");
Unfortunately, this is generating a pipeline which looks like:
{ "aggregate" : "event" , "pipeline" : [ { "$project" : { "resource" : 1 , "total" : "$statistics.connections"}} , { "$group" : { "_id" : "$resource" , "statTotals" : { "$push" : "$statistics.connections"}}}]}
In the $group, the $push is being done against $statistics.connections instead of $total so the results come back blank.
Any help would be greatly appreciated on this. At first I thought this might be https://jira.spring.io/browse/DATAMONGO-1254 but I've tried using both 1.7.2 as well as 1.8.1 and get the same results.
i have a following Document in mongodb
{
"_id" : "1999",
"Email" : "mail#example.com",
"FirstName" : "personFirstNmae",
"LastName" : "personLastName",
"UserStatus" : "INACTIVE",
"FollowingItems" : [
{
"FollowingItemUuid" : "g12345",
"FollowingItemUuidType" : "GALLERY"
}
]
}
i want to achive this
{
"_id" : "1999",
"Email" : "mail#example.com",
"FirstName" : "personFirstNmae",
"LastName" : "personLastName",
"UserStatus" : "INACTIVE",
"FollowingItems" : [
{
"FollowingItemUuid" : "g12345",
"FollowingItemUuidType" : "GALLERY"
},
{
"FollowingItemUuid" : "M121",
"FollowingItemUuidType" : "MUSEUM"
}
]
}
here is my code
val q=QueryBuilder.start("_id").is("1999")
var update=collection.update(q.get,new BasicDBObject("$set",new BasicDBObject("FollowingItems.$.FollowingItemUuid","M121").append("FollowingItems.$.FollowingItemUuidType","MUSEUM")))
but it throws following exception
com.mongodb.WriteConcernException: { "serverUsed" : "Localhost:27017" , "ok" : 1 , "n" : 0 , "updatedExisting" : false , "err" : "cannot use the part (FollowingItems of FollowingItems.FollowingItemUuid) to traverse the element ({FollowingItems: [ { FollowingItemUuid: \"g12345\", FollowingItemUuidType: \"GALLERY\" } ]})" , "code" : 16837}
at com.mongodb.CommandResult.getWriteException(CommandResult.java:90)
at com.mongodb.CommandResult.getException(CommandResult.java:79)
at com.mongodb.DBCollectionImpl.translateBulkWriteException(DBCollectionImpl.java:316)
at com.mongodb.DBCollectionImpl.update(DBCollectionImpl.java:274)
at com.mongodb.casbah.MongoCollectionBase$class.update(MongoCollection.scala:882)
at com.mongodb.casbah.MongoCollection.update(MongoCollection.scala:1162)
Please guide me how can i achive my desried result and what i am doing wrong
You need to use the $push operator to use this. This is the MongoDB shell command:
db.data.update({
"_id": "1999"
}, {
"$push": {
"FollowingItems": {
"FollowingItemUuid": "M121",
"FollowingItemUuidType": "MUSEUM"
}
}
})
And this is your equivalent QueryBuilder syntax:
val q=QueryBuilder.start("_id").is("1999")
var update=collection.update(q.get,new BasicDBObject("$push",new BasicDBObject("FollowingItems.$.FollowingItemUuid","M121").append("FollowingItems.$.FollowingItemUuidType","MUSEUM")))
I am new to MongoDB, and I have to create simple site using jsp/servlet.
I need to a create query that will return count of how many times some site has been visited.
My DB looks like this:
{ "_id" : { "$oid" : "5117fa92f1d3a4093d0d3902"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:50.051Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/" , "dolaznaStr" : "localhost:8080/mongoProjekat/treca"}<br>
{ "_id" : { "$oid" : "5117fa92f1d3a4093d0d3903"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:50.796Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/treca.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/peta"}<br>
{ "_id" : { "$oid" : "5117fa93f1d3a4093d0d3904"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:51.141Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/peta.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/treca"}<br>
{ "_id" : { "$oid" : "5117fa93f1d3a4093d0d3905"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:51.908Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/treca.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/cetvrta"}<br>
{ "_id" : { "$oid" : "5117fa94f1d3a4093d0d3906"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:52.035Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/treca.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/cetvrta"}<br>
{ "_id" : { "$oid" : "5117fa94f1d3a4093d0d3907"} , "ip" : "127.0.0.1" , "datum" : { "$date" : "2013-02-10T19:52:52.197Z"} , "odlaznaStr" : "localhost:8080/mongoProjekat/cetvrta.jsp" , "dolaznaStr" : "localhost:8080/mongoProjekat/treca"}
What I need is a result that will look something like this:
page: localhost:8080/mongoProjekat/treca visited: n(times)<br>
page: localhost:8080/mongoProjekat/druga visited: n(times)
...and so on for every page that has been visited.
I am using Java, by the way.
You may find this SQL to MongoDB chart http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/ helpful.
As for your immediate question:
// getCollection
DBCollection myColl = db.getCollection("toskebre");
// for the $group operator
// note - the collection still has the field name "dolaznaStr"
// but, to we access "dolaznaStr" in the aggregation command,
// we add a $ sign in the BasicDBObject
DBObject groupFields = new BasicDBObject( "_id", "$dolaznaStr");
// we use the $sum operator to increment the "count"
// for each unique dolaznaStr
groupFields.put("count", new BasicDBObject( "$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields );
// You can add a sort to order by count descending
DBObject sortFields = new BasicDBObject("count", -1);
DBObject sort = new BasicDBObject("$sort", sortFields );
AggregationOutput output = myColl.aggregate(group, sort);
System.out.println( output.getCommandResult() );
The println will print:
{ "serverUsed" : "localhost/127.0.0.1:27017" ,
"result" : [ { "_id" : "localhost:8080/mongoProjekat/treca" , "count" : 3} ,
{ "_id" : "localhost:8080/mongoProjekat/cetvrta" , "count" : 2} ,
{ "_id" : "localhost:8080/mongoProjekat/peta" , "count" : 1}] ,
"ok" : 1.0}
Ty for answer Kay. I allready found solution that is similar to yours.
I have done it this way:
DBObject match = new BasicDBObject("stranica","$dolaznaStr");
DBObject project = new BasicDBObject("$project",match);
DBObject id = new BasicDBObject("_id",new BasicDBObject("stranica","$stranica"));
id.put("posete", new BasicDBObject("$sum", 1));
DBObject group = new BasicDBObject("$group",id);
DBObject srt = new BasicDBObject("posete",-1);
DBObject sort = new BasicDBObject("$sort", srt);
AggregationOutput ao = collection.aggregate(project, group, sort);