mercredi 25 février 2015

mongoDB query for multiple fields in a collection



I am trying to query for multiple fields in a collection that looks like:



{ "_id" : ObjectId("54ee37f3a4cd7c1ce4483124"), "imageblock" : "http://someimage.jpg", "textblock" : "Lorem ipsum dolor sit amet, in eget aliquam, odio ut leo mi turpis a felis, adipiscing neque maecenas ac nulla nec curabitur. A hac neque magnis integer arcu ut, per bibendum, aliquet metus nec ante orci amet. Feugiat quis sit" }


What would be the best approach to retrieve the values for both the "imageblock" and "textblock" keys? Very new to working with Mongodb.



Mongo mg = new Mongo("localhost", 27017);
DB db = mg.getDB("deepdivedb");
DBCollection collection = db.getCollection("contentblocks");
BasicDBObject me = new BasicDBObject();
DBCursor cursor = collection.find();

try {
while(cursor.hasNext()) {
System.out.println(cursor.next().get("imageblock"));
// out.print("<img src="+ cursor.next().get("imageblock") + ">");
}

} finally {
//mongoClient.dropDatabase("test");
cursor.close();

}


Thanks for any tips.




Aucun commentaire:

Enregistrer un commentaire