2015-04-27 38 views
2

以前版本的mongo-java驅動程序在DBRef對象上提供了.fetch方法。但是,似乎3.0並沒有。在mongo-java中自動獲取DBRef 3

我仍然可以使用表存儲和檢索DBRef值,但是在迭代查詢結果的同時手動查詢數據,是否還有其他我缺少的方法?對每個返回的結果進行新的查詢似乎效率低下。

回答

2

是的,效率低下。但是你在2.xx.x上完全一樣。 也許它被剝奪的原因是,因爲它可能意味着有一些優化的東西正在進行。但是,如果你檢查的2.13.1驅動程序源代碼,你看到它與.findOne(...)

/** 
* Fetches the object referenced from the database 
* 
* @return the document that this references. 
* @throws MongoException 
* @deprecated use {@link com.mongodb.DBCollection#findOne(Object)} instead 
*/ 
@Deprecated 
public DBObject fetch() throws MongoException { 
    if (_loadedPointedTo) 
     return _pointedTo; 

    if (_db == null) 
     throw new MongoInternalException("no db"); 

    final DBCollection coll = _db.getCollectionFromString(_ns); 

    _pointedTo = coll.findOne(_id); 
    _loadedPointedTo = true; 
    return _pointedTo; 
} 

這證明,證明該方法.fetch()只是一個方便的方法查詢。 對於靜態.fetch(xxx)也是如此。

如果性能變差,您需要考慮反規範化。

Btw。在next Mongo Version(3.2.x)中,$lookup正在爲Aggregaton Framework進行合作。