简体   繁体   中英

Mongodb document retrieval

I am using Mongodb in java. And I want to retrieve a document which is inside a document.

I am using this code for that:

        DB db = null;
        int count = 0;

        //Enter the day for which you want to search for logged on users
        String dayToSearch = "27-06-2012"; //Enter in the format dd-MM-yyyy

        db = DatabaseConnection.getDatabaseObject();
        Set<String> colls = db.getCollectionNames();

        for (String s : colls) {

            DBCollection coll = db.getCollection(s);

            DBObject obj = coll.findOne();

            if(obj==null)
                System.out.println("This is only null.");

            DBObject obj1 = (DBObject)obj.get("LoginRequest");
            if(obj1!=null){
                String time = (String)obj1.get("Time");
                if(time==dayToSearch){
                    BasicDBObject query = new BasicDBObject();
                    query.put("LoginResponse", new BasicDBObject());
                    DBCursor cur1 = coll.find(query);
                    if(cur1.hasNext()){
                        count++;
                    }
                }
            }
            else
                System.out.println("It is null again!!!");
            System.out.println(s);
        }

        System.out.println("No.of users logged in successfully: " + count);

But i am getting null value at DBObject obj1 = (DBObject)obj.get("LoginRequest");. All my obj1 objects for each collection is coming out to be null. I printed obj and it's coming out to be correct. but obj1 is coming out to be null.

My database is of this form:

{
        "_id" : ObjectId("4fead665b79eedd6a5776f38"),
        "test" : "hi",
        "NBC Connection id" : "6f829b01382d556eaf16c34617C2",
        "LoginRequest" : {
                "Time" : "27-06-2012",
                "Model" : null,
                "Version" : null,
                "Username" : "acpptu20000",
                "Password" : "*******",
                "Language" : "en",
                "Manufacturer" : null
        }
}
{
        "_id" : ObjectId("4fead67cb79eedd6a5776f5b"),
        "GetInfoRequest" : {
                "MaxCallLogs" : 45,
                "MaxNewVoicemails" : 15,
                "GetInfoDateOptions" : 7,
                "MaxSavedVoicemails" : 15,
                "SubscribeForUpdates" : false
        }
}

I think your code is much more complex than needed.
Try something like that:

        for(String s : colls)
        {
            DBCollection coll = db.getCollection(s);
            DBCursor whatIHaveBeenLookingFor = coll.find(
                new BasicDBObject().append("LoginRequest",
                new BasicDBObject().append("Time", dayToSearch)));
        }

I did not test this because I don't have your database around. So it might be possible that you have to change some things. But this is how you can easily retrieve something from a MongoDB.

PS: Make sure you really have to iterate over all collections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM