2

I have a bunch of documents in NeDB, let's say of the form:

{ _id : "3HDl4vDjQhcWvM76", type : "customer", name : "Bob" },
{ _id : "65byNNj7578B9hyu", type : "action", customer : "3HDl4vDjQhcWvM76", ... },

(So an 'action' references a 'customer')

And I want to do something like list all actions along with their customer names. The obvious way to do this would be:

db.find({ type: 'action' }, function (err, actions) {
  actions.forEach(function(action) {
    db.findOne({ type: 'customer', _id : action.customer }, function (err, customer) {
      console.log(action, customer.name);
    });
  });
 });

But that gets painful quite quickly. Is there a better way of doing this? Something like:

db.find({ type: 'action' }, {join : ["customer"]}, function (err, actions) {
  console.log(action, action.customer.name); 
});

would be awesome.

It seems like a really common thing to want to do, but I can't seem to find any information on doing it.

Thanks!

Gordon Williams
  • 1,856
  • 1
  • 17
  • 31

0 Answers0