0

I have Order document like this:

{ "_id" : ObjectId("55fdd76fe4b08bcb851be855"), "customer" : DBRef("customer", ObjectId("55fdd76fe4b08bcb851be853")) }

And I would like to "printout" names of referenced customer like this:

db.order.find({}, {"customer.firstName":1})

But this doesn't seem to work. Dot notation works for subdocuments but not for referenced documents.

Is there is way how to "join" those tables or just tell mongo shell to fetch the customer document?

svobol13
  • 1,842
  • 3
  • 25
  • 40

1 Answers1

0

The forEach() method on the find() cursor can be useful in loading the customer references. Something like the following:

db.orders.findOne().forEach(function (order){
    var customer = db[order.$ref].findOne({"_id": order.$id});
    printjson(customer.firstName);
});
chridam
  • 100,957
  • 23
  • 236
  • 235