我正在尝试使用.native()
来更新多条记录。这是我的代码:
// Controller
Pet.native(function(err, collection) {
// For demo purposes only. Correct owner is returned previously from Owner.findOne()
var owner = {
id: '55b6989ca03b83e846198b18',
foo: 'Foo',
bar: 'Bar'
};
collection.update({
_id: {
"$in": ['55b69860a03b83e846198b13', '55b69860a03b83e846198b10'] // get all pets in array
}
}, {
"$push": {
owners: owner // append owner to each selected pets
}
}, {
multi: true
}, function(err, result) {
console.log("Owner ADDED");
});
});
// User.js Model
module.exports = {
schema: true,
attributes: {
petsOwned: {
collection: 'pet',
via: 'owners'
},
}
}
// Pet.js Model
module.exports = {
schema: true,
attributes: {
owners: {
collection: 'user',
via: 'petsOwned'
},
}
}
添加后,如果我填充owners
中的Pet
,则它始终为空。
Pet.find().populate('owners').exec(console.log)