MongoDB queries using Java

13 views
Skip to first unread message

Gareth Edwards

unread,
Aug 12, 2019, 8:46:36 PM8/12/19
to mongodb-user
I am working on a project that uses a mongoDB database to store and extract data from. I currently have a situation where I want to query all documents (with specific conditions) in a collection and return the value of a single field. 

For example: There is a field called "marks" in each document and I want to return the value in  "marks". So if the "marks" field has the value of 5 in each of the 3 documents I want to obtain [5,5,5] (i.e 5 three times). So the distinct method doesn't work and so far that's the only method I've been finding on the internet so far.

Any help will be much appreciated.
Thanks

Chris Carman

unread,
Aug 13, 2019, 7:54:46 AM8/13/19
to mongod...@googlegroups.com
What's wrong with this example from the docs
(https://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/)
?
Seems pretty straightforward.

Block<Document> printBlock = new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};

collection.find(gt("i", 50)).forEach(printBlock);
Reply all
Reply to author
Forward
0 new messages