1

Using Mongoose with MongoDB, I need to return a series of lookups based on a field of an initial lookup. My Schema is as follows:

var PartSchema = new Schema({
    partcode: String,
    children: [String]
});

And the data looks like the following:

[{"partcode":"A1","children":["B1","B2","B3","B4"]},
 {"partcode":"B1","children":["C11","C21","C31","C41"]},
 {"partcode":"B2","children":["C12","C22","C32","C42"]},
 {"partcode":"B3","children":["C13","C23","C33","C43"]},
 {"partcode":"B4","children":["C14","C24","C34","C44"]}]

I can query for A1's children field by using the following static call:

var childrenOnly =
  {children: 1, _id: 0};
PartSchema.static('getChildren', function (partcode, callback) {
  return this.find({ partcode: partcode }, childrenOnly, callback);
});

This returns (via express)

[{"children":["B1","B2","B3","B4"]}]

What I need it to return is

[{"partcode":"B1","children":["C11","C21","C31","C41"]},
 {"partcode":"B2","children":["C12","C22","C32","C42"]},
 {"partcode":"B3","children":["C13","C23","C33","C43"]},
 {"partcode":"B4","children":["C14","C24","C34","C44"]}]

I'm guessing an iterative call is required with the first query returning the children array, and then iterating over the children array to get each of the child records.

1 Answer 1

1

The general idea is to first try getting the array with the children codes using the findOne() method and then use that array as the query with the $in operator in the find() method to return the full result.

var childrenOnly = {children: 1, _id: 0};
PartSchema.static('getChildren', function (partcode, callback) {
    var self = this;
    this.findOne({ partcode: partcode }, childrenOnly)
        .exec(function (err, doc) {
             console.log(doc.children); // {"children":["B1","B2","B3","B4"]}
             return self.find({"partcode": {"$in": doc.children} }, callback);
        });
});

-- EDIT --

Look into using promises. I haven't tested this yet but I believe it should also do the trick:

var childrenOnly = {children: 1, _id: 0};
PartSchema.static('getChildren', function (partcode, callback) {
    var self = this,
        promise = this.findOne({ partcode: partcode }, childrenOnly).exec();
    promise.then(function (doc) {
        return self.find({"partcode": {"$in": doc.children} }, callback);            
    });               
});
Sign up to request clarification or add additional context in comments.

6 Comments

I added a variable var self = this; before the this.findone (which I changed to self.findone) and then called return self.find({"partcode": {"$in": doc.children} }, exclId, callback);. This works and seems sensible. If this is idiomatic, I'll accept an answer that uses it. Thanks for all your help!
@Terry Nice one! Initially thought of adding that but decided to use promises instead. That should be the solution. Nonetheless I will shortly update my answer yet again to reflect this.
Thanks Chridam - appreciate your help.
@Terry No worries, happy to be of help.
Both options work perfectly. The Promises version feels more idiomatic, and that's the one I'll go with. Again, thanks for all your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.