简体   繁体   中英

Mongoose populate not working as expected

I am very much new in using mongoose and had done simple db work on mongodb.

Seeing my usecase I found this method of doing it in mongodb using mongoose. However I am not getting expected result as shown in tutorials.

My Schemas

var EventsSchema = new mongoose.Schema({
    root: {type: mongoose.Schema.Types.ObjectId, ref: 'root'},
    voting: [{type: mongoose.Schema.Types.ObjectId, ref: 'Voting'}]
});

var VotingSchema = new mongoose.Schema({
    events: {type: mongoose.Schema.Types.ObjectId, ref: 'Events'},
    title: {
        type: String,
        required: true
    }
})

var Events = mongoose.model('Events', EventsSchema, 'event');
var Voting = mongoose.model('Voting', VotingSchema, 'voting');

I have these two schemas initially. I want to create a voting event. When voting event is created then in voting schema events id should be stored as suggested and more I want is that voting event reference should be stored in EventSchema .

var events = new Events({});
events.save()
.then((events) => {
    var voting = new Voting({ events: events._id, title: "Test Title" });
    voting.save()
    .then((voting) => {
        Voting.findOne({title: 'Test Title'})
        .populate('events')
        .exec(function(err, voting){
            console.log(voting, err);
            if(err) res.sendStatus(401).send();
            else res.sendStatus(200).send();
        })

    })
    .catch((e) => {
        res.sendStatus(401).send();
    });
})
.catch((e) => {
    res.sendStatus(401).send();
})

What I am getting on console is

{ 
    _id: 5b83eca82a3cfb1dec21ddc9,
    events: { voting: [], _id: 5b83eca82a3cfb1dec21ddc8, __v: 0 },
    title: 'Test Title',
    __v: 0 
}

My MongoDB looks like this

voting

{
    "_id" : ObjectId("5b83eca82a3cfb1dec21ddc9"),
    "events" : ObjectId("5b83eca82a3cfb1dec21ddc8"),
    "title" : "Test Title",
    "__v" : 0
}

events

{
    "_id" : ObjectId("5b83eca82a3cfb1dec21ddc8"),
    "voting" : [],
    "__v" : 0
}

I am not sure how will my mongodb look like. But after attempting the code once it looks like above.

I must be doing something wrong or missing something important. But this kind of code is there in docs too. docs link . Help me in sorting this issue.

Thanks

Array of ObjIds should be defined this way in schema:

var EventsSchema = new mongoose.Schema({
    root: {type: mongoose.Schema.Types.ObjectId, ref: 'root'},
    voting: {type: [mongoose.Schema.Types.ObjectId], ref: 'Voting'}//**array bracers** 
});

Also calling population over a field of type array like root field will return an empty array in the res of the API, see query hit by mongoose.set('debug', true); you will notice that mongoose is searching in event model not root.

though you have to tell mongoose in the population method which model to use in order to get the population working, unless you tell mongoose which model to search in .

populate({path:'vooting',model:'vooting'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM