我在java中使用Mongodb。我想要檢索文檔內部的文檔。Mongodb文檔檢索
我使用這個代碼爲:
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);
但我在DBOBJECT OBJ1 =(DBOBJECT)obj.get( 「LoginRequest」)獲得空值;.每個集合的所有obj1對象都是空的。我打印obj,它出來是正確的。但obj1現在是空的。
我的數據庫是這樣的形式:
{
"_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
}
}
你只想通過日期查詢獲得「LoginRequest」對象嗎? –
看我的LoginRequest是另一個DBObject ryt嗎?所以,我只是希望它存儲在一個DBObject變量中。但是當我通過鑄造來存儲它時。我把它當作null。但是,當我做obj.get(「LoginRequest」)。我得到了正確打印。 – shalki
你能來這個聊天室嗎? http://chat.stackoverflow.com/rooms/12986/discussion-between-shalki-and-parvin – shalki