简体   繁体   中英

How to retrieve only one column from collection in mongodb using java?

I want to retrieve only ID column with value less than 4,

MongoDatabase database = client.getDatabase("d");

     MongoCollection<org.bson.Document> contCol = database.getCollection("test");
     BasicDBObject query = new BasicDBObject();
     query.put("ID",1);

 FindIterable<org.bson.Document> it = contCol.find().projection(excludeId());

Doesn't give me the required output. The collection test has two columns, namely ID and name ; the query is returning both the columns, I want to get only the ID column.

To specify which columns you want included, you need to specify that in your projection; for example using the include() method .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM