How to use $query, $hint or $explain from Java

Sophia

I am using MongoDB 3.4 My methods are:-

MongoDatabase db = mongo.getDatabase(mongoDBLogs);
MongoIterable<String> allCollections = db.listCollectionNames();
str==FROM COLLECTION
MongoCollection<Document> table = db.getCollection(str);
MongoCursor<Document> cursor = table.find(queryForLogs).iterator();

The problem is in OLD version I can use the

DB db = mongo.getDB(mongoDBLogs);
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
DBCollection table = db.getCollection(s);
cursor = table.find().explain(); <<<<---- I have this explain method
AND ALSO I have
cursor = table.find().hint(indexKeys);<<<<---- I have this hint method
}

NOW in 3.4 I am not able to access these options by using

MongoCursor<Document> cursor = table.find(queryForLogs).explain() <-- this is type  FindIterable<Document>

OR

MongoCursor<Document> cursor = table.find(queryForLogs).hint(Some value); <---This is type  FindIterable<Document>

I refer the link:- https://docs.mongodb.com/manual/reference/operator/meta/hint/ https://docs.mongodb.com/manual/reference/method/cursor.explain/

So I make another method:-

    public  BasicDBObject generateQueryForSessionIDLogs(String service_name, String node_name, Date gtDate, Date lteDate, String session_id,List<String> logLevel, String errorMsg){
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();

        //Use session to search. If session is unknown, start and end date must be provided
        if(!session_id.isEmpty() && session_id != null){
            String[] sessionarray = session_id.split(",");

            if(sessionarray.length == 1){
                Long val = Long.valueOf(sessionarray[0].trim()).longValue();
                obj.add(new BasicDBObject("s", val));

            } else{ 
                ArrayList<Long> vals =  new ArrayList<Long>();
                for(String strn : sessionarray){
                    vals.add(Long.valueOf(strn.trim()).longValue());
                }

                obj.add(new BasicDBObject("s", new BasicDBObject("$in", vals )));
            }
        }else{      
            if((gtDate != null) && (lteDate != null)){
                obj.add(new BasicDBObject("d",  new BasicDBObject("$gt", gtDate).append("$lte", lteDate)));
            }
        }

        if(!node_name.isEmpty()){
            obj.add(new BasicDBObject("i", node_name));
        }
        if(!service_name.isEmpty()){
            obj.add(new BasicDBObject("n", service_name ));
        }
        if(!errorMsg.isEmpty()) {
            obj.add(new BasicDBObject("m", Pattern.compile(errorMsg)));
        }

        if(!logLevel.isEmpty()){
//          obj.add(new BasicDBObject("r", new BasicDBObject("$in", logLevel )));
            obj.add(new BasicDBObject("r", logLevel.get(0) ));
        }

        if (obj.size() > 1){ //append 'AND' if there is >1 conditions
            andQuery.put("$and", obj);
        }

        BasicDBObject andQuery2 = new BasicDBObject();
        andQuery2.put("$query", andQuery);
        List<BasicDBObject> obj2 = new ArrayList<BasicDBObject>();
        obj2.add(new BasicDBObject("i",1));
        andQuery2.put("$hint", obj2);
        andQuery2.put("$explain", 1);
        logger.debug("Query String for Logs: " + andQuery2);
        return andQuery;
    }

My final query make like:-

Query String for Logs: { "$query" : { "$and" : [ { "d" : { "$gt" : { "$date" : "2017-06-19T04:00:00.000Z"} , "$lte" : { "$date" : "2017-06-20T04:00:00.000Z"}}} , { "i" : "WM96BEPSIT02"}]} , "$hint" : [ { "i" : 1}] , "$explain" : 1}

With error:- Exception in thread "pool-2-thread-16" Exception in thread "pool-2-thread-15" com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'unknown top level operator: $query' on server

Please also suggest any alternative way to call the indexed values first. As in my case all search values are not indexed.

Neil Lunn

To send things like $hint or $explain to the Java driver you actually use the .modifiers() method from FindIterable. For instance:

MongoCursor<Document> iterator = collection.find()
    .modifiers(new Document("$explain",1)).iterator();

while (iterator.hasNext()) {
  System.out.println(iterator.next().toJson());
}

This will print the explain stats output.

Any BsonDocument type is valid to provide as an argument. The valid list is on Query Modifiers in the core documentation.

Generally speaking, $query is not something that you actually use from the modifiers list, since you are actually constructing that with any argument to .find(). But all the other modifiers are valid for use here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use index hint in this query?

From Dev

How to use Oracle query hint in Hibernate

From Dev

How to use $hint in MongoDB aggregation query?

From Dev

How to use the hint operator from mongoDB in strongloop loopback?

From Dev

How do I use `Explain` with a `find_by_sql` query?

From Dev

How do I use `Explain` with a `find_by_sql` query?

From Dev

Need hint with MySQL query from 2 Tables

From Dev

Spring Batch JpaPagingItemReader: how to set a query hint?

From Dev

My query is not using my indexes, how do i use explain plan and fix this slow query with MySQL

From Dev

How can I run Hive Explain command from java code?

From Java

How should I use the Optional type hint?

From Dev

How should I use the Optional type hint?

From Dev

Explain how to use TextGetTargetedSentiment of AlechmyAPI

From Dev

Hint on how extract a text from a List<string>

From Dev

Hint on how extract a text from a List<string>

From Dev

How to create a yellow hint with Java swt?

From Dev

Use index hint and join hint

From Dev

How to explain this behaviour from DirectoryIterator?

From Dev

How to explain multiple inheritance in Java

From Dev

How to use column from main query in subquery?

From Dev

How to use JSON to return array from a query?

From Dev

How to use JSON to return array from a query?

From Dev

Using Query Hint on FOR XML

From Dev

Add a $hint to rmongodb query

From Dev

How to use prepared statement for select query in Java?

From Dev

How to use IN clause in JPA query in Java?

From Dev

How to use name() and/or node() in Java XPath query?

From Dev

How to use elasticsearch has child query in Java?

From Dev

how to use a field from a main query within the WHERE of a sub query

Related Related

HotTag

Archive