My Mongo collection is like:
db.user.find()
{"id":"11","name" : "a1", "age":"12", "add":"assddd"}
{"id":"11","name" : "a2", "age":"12", "add":"assddsaddd"}
{"id":"10","name" : "b2", "age":"12", "add":"assddsaddd"}
I need to fetch the data only for name and make an array.
Example I need to fetch data only for id = 11 So my output should be like:
name:[a1,a2]
My Java code is like:
//Mongo Connection
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("test");
DBCollection table = db.getCollection("user");
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("id", "11");
DBCursor cursor = table.find(searchQuery);
DBObject resultElement = null;
resultElement = cursor.next();
Object son = JSON.parse(resultElement.toString());
System.out.println(son);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
How can i get particular key value pair in array here?