0

I want to create java code for the below mongoDB query. Since i am new to mongoDB cananyone help on this

db.getCollection('dum_stats').distinct("engineNo").sort();
1
  • please add anything you have tried so far Commented Dec 5, 2016 at 10:34

1 Answer 1

1

Here is a sample example for Mongo 3.x version.

MongoClient mongoClient = new MongoClient();
MongoDatabase db  = mongoClient.getDatabase("yourdb")
MongoCollection<Document> collection = db.getCollection("dum_stats");
FindIterable<Document> distinct = collection.distinct("engineNo", Document.class);
distinct.sort(Sorts.descending("engineNo"));
Sign up to request clarification or add additional context in comments.

4 Comments

There is no method as sort. FindIterable<Document> distinct = collection.distinct("engineNo", Document.class).sort(null);
Updated. Try now.
Yes it is available, but it expects a Bson object as argument, i want to provide sort like distinct.sort(-1);
Hi, @user2683814 Your solution is giving me an error "Type mismatch: cannot convert from DistinctIterable<Document> to FindIterable<Document>" for collection.distinct. What should I do regarding it? Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.