TheCrazyProgrammer
TheCrazyProgrammer

Reputation: 8588

How to retrieve all values from DBCursor in mongoDB

I do not know what are the attribute names in my collection.

DBCollection objDBC = db.getCollection(collectionName);
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put(attributeName, attributeValue);
DBCursor cursor = objDBC.find(searchQuery);

Now from DBCursor how to retrieve values of all attributes?

Upvotes: 1

Views: 2330

Answers (1)

noamt
noamt

Reputation: 7805

The DBCursor is actually a result iterator, and every result element is a DBObject so it can be converted to a map; to retrieve all values you can do something like:

while(cursor.hasNext()) {
    DBObject resultElement = cursor.next();
    Map resultElementMap = resultElement.toMap();
    Collection resultValues = resultElementMap.values();
    //Do something with the values
}

Upvotes: 4

Related Questions