简体   繁体   English

Mongodb文件检索

[英]Mongodb document retrieval

I am using Mongodb in java. 我在Java中使用Mongodb。 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");. 但是我在DBObject obj1 =(DBObject)obj.get(“ LoginRequest”);处获得空值。 All my obj1 objects for each collection is coming out to be null. 我每个集合的所有obj1对象都为null。 I printed obj and it's coming out to be correct. 我打印了obj,它出来是正确的。 but obj1 is coming out to be null. 但obj1出来为空。

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. 但这是您可以轻松地从MongoDB中检索内容的方法。

PS: Make sure you really have to iterate over all collections. PS:请确保您确实必须遍历所有集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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