Java equivalent of scala query in mongodb -
Java equivalent of scala query in mongodb -
what equivalent cde in java:
var result = collectionname.findone() println(result.get("name").tostring)
to elaborate, sample db:
{ "_id" : objectid("4ca039f7a5b75ab98a44b149"), "name" : "kaustubh", "country" : "india" } { "_id" : objectid("4ca03a85a12344a5e47bcc5c"), "name" : "rahul", "country" : "pakistan" } { "_id" : objectid("4ca03a9ea12344a5e47bcc5d"), "name" : "swapnil", "country" : "lanka" } { "_id" : objectid("4ca03b19a12344a5e47bcc5e"), "name" : "sagar", "country" : "nepal" }
i running next query on it:
query.put("country", "india"); dbcursor cursor = collection.find(query); while (cursor.hasnext()) { system.out.println(cursor.next()); }
that prints:
{ "_id" : { "$oid" : "4ca04b6b37a85ab92557218a"} , "name" : "kaustubh" , "country" : "india"}
as many times pair exists in collection.
how formulate query names, 1 time , count them.i read docs, , didnt stumble upon way it.
try this
query.put("name", "kaustubh"); dbobject mydoc = collection.findone(query); system.out.println(mydoc);
java mongodb
Comments
Post a Comment