Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
if i give name in my programm i need to get the age with corresponding age also {"Name":"msr","age" :24} like these


What I have tried:

package mongojava;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class javaconn {

    public static void main(String[] args) {


        MongoClient client = new MongoClient("localhost",27017); 
        DB db = client.getDB( "admin" );
        DBCollection collection = db.getCollection("admin");
        DBObject dbo = collection.findOne();
        //String name = (String) dbo.get("Name");
        //String age = (String) dbo.get("age");
        BasicDBObject query = new BasicDBObject();
        query.put("Name", "Siddareddy");
        DBCursor cursor = collection.find(dbo);


        BasicDBObject newDocument = new BasicDBObject();
        newDocument.put("age", "24");
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }

}
Posted
Comments
Richard MacCutchan 15-May-18 9:58am    
Don't do it like that. Store the date of birth, then you can calculate the correct age any time.
Member 13809409 15-May-18 10:17am    
in my mongodb i created with age only so i need to get age ..Thanks ..helpme out

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900