T
T
Test Test2015-10-24 19:14:26
MongoDB
Test Test, 2015-10-24 19:14:26

How to properly organize relationships on Mongoose?

Good evening. The question arose about how best to organize relationships between Mongoose models.
For example, we have two models: User and UserRecovery.

// Schema модели User
var schema = mongoose.Schema({
     // ....
    _recovery: {type: mongoose.Schema.ObjectId, ref: 'UserRecovery'}
});

// Schema модели UserRecovery
var schema = mongoose.Schema({
     // ....
    _user {type: mongoose.Schema.ObjectId, ref: 'User'}
});

When saving the UserRecovery model, I would like to access the User model:
var userRecovery = new UserRecovery({_user: objectId('...')});
userRecovery.save(function(err, createdUserRecovery) {
     console.log(createdUserRecovery._user.username);
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
amatory10, 2015-10-24
@amatory10

This article I think should help: https://alexanderzeitler.com/articles/mongoose-ref...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question