Java is taking 4000 ms to get one record where as from command prompt it took 25 ms. Why?

Java is taking 4000 ms to get one record where as from command prompt it took 25 ms. Why?
My code is;

BasicDBObject getQuery = new BasicDBObject();
			getQuery.put(pkfield, pkvalue.longValue());
			FindIterable<Document> cursor = collection.find(getQuery).projection(Projections.fields(Projections.include(fieldvalue), Projections.excludeId()));
			
			Iterator it = cursor.iterator();
			Document doc=(Document)it.next();
			BigInteger rs=new BigInteger(String.valueOf(doc.get(fieldvalue)));

From both JAVA and command prompt i am connecting to Atlas cluster (shared) running on AWS mumbai.

Please help

Share what you do the command prompt. We need to be sure you are doing the same thing. Share the document you are querying. Share the query. Details about the size of your collections and installation are important.

Note that the same query performs twice in a row may leads to different execution time if the index used in the query and/or the documents queried need to be fetch from disk.

1 Like