通过将对象附加到每个记录中的集合中,一次更新多个记录,在Sails JS中

时间:2015-07-28 07:17:02

标签: sails.js node-mongodb-native sails-mongo

我正在尝试使用.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)

0 个答案:

没有答案