Following is an example model:
UserModel == {
name: String,
friends: [ObjectId],
}
friends
corresponds to a list of id
of objects of some other model for example AboutModel
.
AboutModel == {
name: String,
}
User.findOne({name: 'Alpha'}, function(error, user){
About.find({}, function(error, abouts){ // consider abouts are all unique in this case
var doStuff = function(index){
if (!(about.id in user.friends)){
user.friends.push(about.id);
about.save();
}
if (index + 1 < abouts.length){
doStuff(index + 1)
}
}
doStuff(0) // recursively...
})
})
In this case, the condition 'about.id in user.friends` seems to be always false. How? Is this to do with the type of ObjectId or the way it is saved?
Note: ObjectId
is short for Schema.ObjectId
; I don't know if that's an issue in itself.