Using the MongoDB 3.2.0 Java driver, which offers a Document object that seems different from a "BasicDBObject" or "DBObject", I would like to extract all the fields in a given document. For example, I have this document:
{ "sponsor" : "ABC Bicycles", "start" : "Herndon", "miles" : 50 }
I'd like to extract the values of "sponsor", "start", and "miles" using an Iterator. I've tried:
theSponsor = cursor.next().get("sponsor").toString();
which works fine for the "sponsor" field, but how can I pull the values of "start" and "miles" for this same question, using the same cursor? Perhaps I need something other than
MongoCursor<Document> cursor = collection.find().iterator();
while (cursor.hasNext()) { Document doc = cursor.next(); theSponsor = doc.get("sponsor").toString(); theStart = doc.get("start").toString(); theMiles = doc.get("miles").toString();}?