Java - MongoDB use DBRef fetch DBObject

摘要:Java - MongoDB use DBRef fetch DBObject

將當初Insert的DBRef取出,並取得對映的資料物件
 
            mongo = this.dao.getMongo();  //自行撰寫取得Mongo物件
            db = this.dao.getDB(mongo);    //自行撰寫取得DB物件
            DBCollection collection = db.getCollection(this.collection_name);   //取得Collection
 
            BasicDBObject query = new BasicDBObject();
            query.append("field_key",field_key);
            DBCursor cursor = collection.find(query);
            BasicDBObject object = null;
 
            if(cursor.hasNext()) {
                object = (BasicDBObject)cursor.next();
                DBRef ref_obj = (DBRef)object.get("ref_obj");
                BasicDBObject objRefData = (BasicDBObject)ref_obj.fetch();               
           }
 
當初insert DBRef時的方式
 
obj.append("ref_obj",new DBRef(db,"collection_name",obj.getString("ref_field_id")));