2

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?

1 Answer 1

3

You will need to use the aggregate framework and group by the 'name' field and use addToSet to construct an array of those names.

db.user.aggregate(
    [
        { $match : {  id : "11" } },
        { $group : { _id : "$id", name : { $addToSet : "$name" } } }

    ])

This will give the following document:

{
    "_id" : "11",
    "name" : [ "a2", "a1"] 
}

You then use the cursor to retrieve the name field array.

EDIT

You can construct an aggregate function in Java like so:

    MongoClient mongoClient = null;

    try {
        mongoClient = new MongoClient("localhost", 27017);
    } catch (UnknownHostException e) {}

    DB db = mongoClient.getDB("test");

    DBCollection collection = db.getCollection("user");

    DBObject match = new BasicDBObject("$match", new BasicDBObject("id", "11"));
    DBObject groupFields = new BasicDBObject("_id", "$name");
    groupFields.put("name", new BasicDBObject("$addToSet", "$name"));
    DBObject group = new BasicDBObject("$group", groupFields);

    AggregationOutput output = collection.aggregate(match, group);

    Iterable<DBObject> itResult = output.results();

    for (DBObject dbo : itResult) {
        List<String> items = (List<String>) dbo.get("name");
        for(String item : items){
            System.out.println(item);
        }

    }

By using DBObjects you can build up your aggregate function. Take a look here for more details on how to do aggregate functions in Java. http://docs.mongodb.org/ecosystem/tutorial/use-aggregation-framework-with-java-driver/

Follow this code through and if anything is not clear let me know and will try to clarify it for you.

Sign up to request clarification or add additional context in comments.

1 Comment

I cant change in MongoDb is there a way to access it that way in Java?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.