最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get parent object from id that matches objects child array with mongoose in NodeJS - Stack Overflow

programmeradmin1浏览0评论

I'm working with mongoose in NodeJS and I have an id from a child array. I the models are defined like this:

var thingSchema = new schema({
    thingId: String,
    smallerThings : [smallerThingsSchema]
});

var smallerThingsSchema = new schema({
    smallerThingId: String,
    name : String,
    anotherName : String
});

I have the smallerThingId, but I want to get the thingId.

Right now I have a for loop that looks like this (pretty ineffective I suppose). Potentially, there could be 100,000 things.

//Find all things
thingModel.find({}, null, function (error, things) {
    if (error) {
        callback(error);
    }
    else {
        //Go through all things  
        for(var i = 0; i < things.length; i++){
            //check if a thing with the array of smaller things matches the id
            for(var j = 0; j<things[i].smallerThings.length; j++){
                if(things[i].smallerThings[j].id === smallerThingId){
                    //return it 
                    return things[i];
                }
            }
        }
    }
});

Grateful for any help or where I can look (docs/blog/other) to learn how to handle such scenario.

I'm working with mongoose in NodeJS and I have an id from a child array. I the models are defined like this:

var thingSchema = new schema({
    thingId: String,
    smallerThings : [smallerThingsSchema]
});

var smallerThingsSchema = new schema({
    smallerThingId: String,
    name : String,
    anotherName : String
});

I have the smallerThingId, but I want to get the thingId.

Right now I have a for loop that looks like this (pretty ineffective I suppose). Potentially, there could be 100,000 things.

//Find all things
thingModel.find({}, null, function (error, things) {
    if (error) {
        callback(error);
    }
    else {
        //Go through all things  
        for(var i = 0; i < things.length; i++){
            //check if a thing with the array of smaller things matches the id
            for(var j = 0; j<things[i].smallerThings.length; j++){
                if(things[i].smallerThings[j].id === smallerThingId){
                    //return it 
                    return things[i];
                }
            }
        }
    }
});

Grateful for any help or where I can look (docs/blog/other) to learn how to handle such scenario.

Share Improve this question asked May 26, 2016 at 12:09 peturpetur 1,3964 gold badges22 silver badges43 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

To get document by sub-document id you can use following code:

thingModel.find({"smallerThings.smallerThingId":smallerThingId}, null, function (error, things) {

});

This will return document having "smallerThingId".

You can use mongoose find like:

thingModel.find({"things.smallerThings.id": smallerThingId }, null, function (error, things) {
    if (error) {
        callback(error);
    }
    else {
        //do what you need
    }
});
发布评论

评论列表(0)

  1. 暂无评论