Question

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 .

Was it helpful?

Solution

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

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top