A
A
Abc Edc2015-05-08 19:40:23
MongoDB
Abc Edc, 2015-05-08 19:40:23

How to do binding in mongoose?

For the user to participate in groups, I did it like this

"members" : [ 
        "54bd0fa565bf53efd76210b7",
        
    ],

In groups and then I go through the array with id and in this way I can return a list of users with their data in the group, but I know that there is a DBRef method or a manual method, I read it but I can’t implement how to understand in mongoose you can example please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kumanin, 2015-05-12
@gleber1

// ...
var UserSchema = new Schema({
  name: String,
  email: {type: String, lowercase: true},
  contracts: [{
    type: Schema.Types.ObjectId,
    ref: 'Contract'
  }]
});

module.exports = mongoose.model('User', UserSchema);

// ...
User.findOne({
    _id: userId
  }, '-salt -hashedPassword')
    .populate('contracts')
    .exec(function(err, user) { // don't ever give out the password or salt
      if (err) return next(err);
      if (!user) return res.json(401);
        // user.contracts.forEach(function(contract, index){ console.log(index, contract); });
        res.json(user);
      });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question