Is there anyway existing to change normal Mongodb queries to Java Mongodb driver specific queries? I want to know whether there exist an equivalent query in Java Mongodb driver for every query of normal MongoDB? Because we have examples querying array of subdocuments in normal Mongodb but to do the same via Java MongoDB driver we do not have sufficient examples .
-
Can you provide an example for the query you likely to implement in JAva and have in shell format? While it would fit much more here, and probably you would get faster answer. Also would be great if you can put some info where you struggling, and what are the symptoms while you cannot go forward. – attish Aug 30 '13 at 09:04
-
For example queries where $elemmatch , $slice ,$push are used (all array indexing operators ).Is there any way to do the same in Java Mongodb based queries? – user2702205 Sep 01 '13 at 08:04
2 Answers
The Java drivver along with the python is the most developed one, so you can check it in the driver DOCS. Usually the idea (structure of the commands) is the same as in the shell you just need helpers to construct the command.
In Java this documentation can make some hints about how it works:DOCS
so for
$push:
Mongoshell DOCS example:
db.students.update(
{ name: "joe" },
{ $push: { scores: 89 } }
)
Where { name: "joe" } is a query identifing the right document to update and the scores is an array field and 89 will be abbended.
Java DOCS
example: check out this question : (MongoDB Java) $push into array
$elemmatch:
Mongoshell DOCS
example: check out this question:Convert MongoDB query into Java
$slice:
Mongoshell DOCS
syntax:
db.collection.update( <query>,
{ $push: {
<field>: {
$each: [ <value1>, <value2>, ... ],
$slice: <num>
}
}
}
)
In Java something like (Just cause i have not find for this exact update uitilized slice an example in java this is constructed by myself):
final MongoClient mongoClient = new MongoClient();
final DBCollection coll = mongoClient.getDB("TheDatabase").getCollection("TheCollection");
coll.update(<query>, new BasicDBObject("$push",
new BasicDBObject(<field>,
new BasicDBObject("$each",
new BasicDBList()
.put(0,<value1>)
.put(1,<value2>)
.put(3,<value3>))
.append('$slice',-5)));
example: check out these questions too for usage of slice in other cases: $slice mongoDB Java , Mongo java driver - retrieve slice of array without any other field or this thread :https://groups.google.com/forum/#!topic/mongodb-user/4c3P0_FOzyM
-
Yeah I checked out the same in Spring data Mongodb. I found the equivalent existing in that also. But for using $slice in Spring data MongoDB i could not find a reference ?? Can you help me with it? – user2702205 Sep 03 '13 at 10:59
-
I am sorry, unfortunately i do not have too much exp. with the Spring data MongoDB, i checked shortly and found nothing. – attish Sep 04 '13 at 08:02
There's no automated way to convert shell queries to Java queries, but all queries that are supported in the shell are supported in the Java driver. As in the example Attish has given, everywhere you see
{ "$someOperator" : { "someKey": "someValue" } }
you need to convert those to use BasicDBObject:
new BasicDBObject("$someOperator", new BasicDBObject("someKey", "someValue"));
effectively
{ key : value }
->
new BasicDBObject (key, value);

- 3,891
- 1
- 25
- 39