1

I have constructed the node path as /57639afa5961debc1b256745/, /5763c5d17d05688c1838c7a3/, but when I try to subtitute with nodepath it gets the result as empty array even though it has the children:

Degree.prototype.findByPath = function(dbObj, degrees) {
    var nodePath = '';

    for(var i in degrees) {
        nodePath = nodePath + '/' + degrees[i]['_id'] + '/, ';
    }

    return new Promise(function (resolve, reject) {
        console.log(nodePath);
        dbObj.collection('degree').find({ 
            'path': {
                '$in': 
                    [ nodePath ] 
                } 
            }).toArray((err, results) => {
                if(err) {
                    reject(err);
                } else {
                    var nodes;

                    if(results != null)
                        nodes = degrees.concat(results);
                    else
                        nodes = degrees;

                    resolve(nodes);
                }               
            });
    }); 
};

1 Answer 1

1

You need to build up nodePath as an array of strings rather than one comma-separated string. And if you want the id values treated as regular expressions, use the RegExp constructor instead of adding slash characters:

var nodePath = [];

for(var i in degrees) {
    nodePath.push(new RegExp(degrees[i]['_id']));
}

return new Promise(function (resolve, reject) {
    console.log(nodePath);
    dbObj.collection('degree').find({ 
        'path': {
            '$in': nodePath
            } 
        }).toArray((err, results) => {...
Sign up to request clarification or add additional context in comments.

3 Comments

Degree.prototype.findByPath = function(dbObj, degrees) { var nodePath = ''; for(var i in degrees) { nodePath = nodePath + degrees[i]['_id'] + ', '; } if(nodePath != '') nodePath = '[' +nodePath + ']'; return new Promise(function (resolve, reject) { console.log(nodePath); dbObj.collection('degree').find({ 'path': { '$regex': nodePath } }).toArray((err, results) => {
@Sethu Sorry, I don't understand your comment.
your code not worked as expected. So i try with $regex it works as expected.

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.