Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autopopulate - Select options argument(return all fields), issue with latest version of mongoose > 4.11.0 #44

Closed
santoshmondal opened this issue Jul 27, 2018 · 2 comments

Comments

@santoshmondal
Copy link

For the following program, getting different output with version @4.4.5 and @4.12.0. The version of mongoose-autopopulate@0.8.0

let autopopulate = require('mongoose-autopopulate');
let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let ObjectId = Schema.Types.ObjectId;

const CONFIG = {
    "DB_URL" : "mongodb://localhost/test1",
    "DB_CONNECT_OPTION" : {"useMongoClient" : true},
    "PERSON_SCHEMA" : "PERSON",
    "AREA_SCHEMA" : "AREA",
    "BAND_SCHEMA" : "BAND",
};

class AutoPopulateTest {
    static init(){
        try{
            mongoose.connect(CONFIG.DB_URL, CONFIG.DB_CONNECT_OPTION);

            AutoPopulateTest.dbSchemaInit();
        }catch(err){
            throw err;
        }
    }

    static dbSchemaInit(){
        try{
            let areaSchema = new Schema({
                "code": String,
                "name": String
            });
            mongoose.model(CONFIG.AREA_SCHEMA, areaSchema, CONFIG.AREA_SCHEMA);

            let personSchema = new Schema({
                "name": String,
                "nickName": String,
                "areaId": {"type":ObjectId, "ref":CONFIG.AREA_SCHEMA, "autopopulate":true }
            });
            personSchema.plugin(autopopulate);
            mongoose.model(CONFIG.PERSON_SCHEMA, personSchema, CONFIG.PERSON_SCHEMA);

            let bandSchema = new Schema({
                "name": String,
                "personId": {"type":ObjectId, "ref":CONFIG.PERSON_SCHEMA, "autopopulate":{"select" : "name"} },
                "areaId": {"type":ObjectId, "ref":CONFIG.AREA_SCHEMA, "autopopulate":true }
            });
            bandSchema.plugin(autopopulate);
            mongoose.model(CONFIG.BAND_SCHEMA, bandSchema, CONFIG.BAND_SCHEMA);
        }catch(err){
            throw err;
        }
    }

    static async saveRecords(){
        try {
            let People = mongoose.model(CONFIG.PERSON_SCHEMA);
            let Area = mongoose.model(CONFIG.AREA_SCHEMA);
            let Band = mongoose.model(CONFIG.BAND_SCHEMA);

            let taskList = [];
            let peopleRefId = mongoose.Types.ObjectId();
            let areaRefId = mongoose.Types.ObjectId();

            let personJson = {"_id": peopleRefId, "name": "Narendra Modi", "nickName":"Modi", "areaId": areaRefId};
            let personPojo = new People(personJson);
            taskList.push(personPojo.save());

            let areaJson = {"_id" : areaRefId, "code" : "IND", "name" : "INDIA"};
            let areaPojo = new Area(areaJson);
            taskList.push(areaPojo.save());

            let bandJson = {"name" : "Politics", "personId":peopleRefId, "areaId" : areaRefId};
            let bandPojo = new Band(bandJson);
            taskList.push(bandPojo.save());

            let output = await Promise.all(taskList);
            return output;
        }catch(err){
            return Promise.reject(err);
        }
    }

    static async fetchRecord(){
        try{
            let Band = mongoose.model(CONFIG.BAND_SCHEMA);

            let output = await Band.findOne().sort({"_id":-1}).exec();
            return output.toObject();
        }catch(err){
            return Promise.reject(err);
        }
    }

    static async main(){
        try{
            AutoPopulateTest.init();

            await AutoPopulateTest.saveRecords();

            let queryOutput = await AutoPopulateTest.fetchRecord();
            console.log(queryOutput);
        }catch(err){
            console.log(err);
        }
    }
}

AutoPopulateTest.main();

In band schema, I have specified following for people schema, autopopulate

"personId": {"type":ObjectId, "ref":CONFIG.PERSON_SCHEMA, "autopopulate":{"select" : "name"} },

Output with mongoose@4.4.5

{
    _id: 5b5af8c48779e4782284ce6f,
    name: 'Politics',
    personId: {
        _id: 5b5af8c48779e4782284ce6d,
        name: 'Narendra Modi'
    },
    areaId:{
        _id: 5b5af8c48779e4782284ce6e,
        code: 'IND',
        name: 'INDIA',
        __v: 0
    },
    __v: 0
}

Output with mongoose@4.12.0

{
   _id: 5b5af7fe5ba313224100848f,
    name: 'Politics',
    personId:{
        _id: 5b5af7fe5ba313224100848d,
         name: 'Narendra Modi',
         areaId:{ _id: 5b5af7fe5ba313224100848e,
            code: 'IND',
            name: 'INDIA',
            __v: 0
         }
    },
    areaId:{
        _id: 5b5af7fe5ba313224100848e,
        code: 'IND',
        name: 'INDIA',
        __v: 0
    },
  __v: 0
}

The areaId has not been specified in 'select option argument'. With mongoose@4.12.0 areaId is also part of output json. Can you please let me know the reason for different output. and how can i fix this. Thanks.

@santoshmondal santoshmondal changed the title Autopopulate - Select options argument, issue with latest version of mongoose > 4.11.0 Autopopulate - Select options argument(return all fields), issue with latest version of mongoose > 4.11.0 Jul 27, 2018
@vkarpov15
Copy link
Member

Thanks for reporting, will investigate ASAP 👍

@vkarpov15
Copy link
Member

Upon closer inspection this looks like expected behavior because you have an autopopulate on personSchema.areaId. In 4.11.14 we made a bug fix that automatically selects paths that you call populate() on, so in this case your schema is configured to populate() on areaId whenever you load a Person. Notice that, for example, nickName doesn't show up in the output.

As a workaround, you can use the maxDepth option to make sure the bandSchema autopopulates don't trigger any person autopopulate:

            let bandSchema = new Schema({
                "name": String,
                "personId": {"type":ObjectId, "ref":CONFIG.PERSON_SCHEMA, "autopopulate":{"select" : "name", maxDepth: 1} }, // <-- note the `maxDepth: 1` here
                "areaId": {"type":ObjectId, "ref":CONFIG.AREA_SCHEMA }
            });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants