helion3
helion3

Reputation: 37391

Auto-fetch DBRef in mongo-java 3

Past versions of the mongo-java driver offered a .fetch method on DBRef objects. However it seems that 3.0 does not.

I can still store and retrieve the DBRef value with the table, but short of manually querying that data while iterating the results of my query, is there any other method I'm missing? It seems inefficient to make a new query for every returned result.

Upvotes: 2

Views: 539

Answers (1)

d0x
d0x

Reputation: 11571

Yes, it is inefficient. But you did exactly the same on 2.xx.x. Maybe the reason why it got depricated is, because it may implies that there is some optimized stuff going on. But if you check drivers sourcecode of 2.13.1 you see that it queries with .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;
}

That proofs that the .fetch() method was just a convenience method. The same is true for the static .fetch(xxx).

If the performance becomes bad, you need to think about denormalizing.

Btw. in the next Mongo Version (3.2.x) a $lookup is comming for the Aggregaton Framework.

Upvotes: 2

Related Questions