ここでのデータはさまざまなコレクションに含まれているため、1つの更新ステートメントで両方のカウンターを同時にインクリメントすることはできません。
一貫性のあるビューを得るには、更新ステートメントを「連鎖」し、それぞれの返信結果を使用してレスポンスを構築する必要があります。
ニーズに応じて、あなたはこれでPromise
を使用することができ、次のいずれか
Test.incrementTest("56fe279d363ce91765d9e39e").then(function(test) {
console.log(JSON.stringify(test,undefined,2));
}).catch(function(err) {
throw err;
})
場合や、async
ライブラリからasync.waterfall
を使用することができます:あなたは、あなたのモデルに呼び出すことができ
testSchema.statics.incrementTest = function(id) {
var self = this;
return new Promise(function(resolve,reject) {
self.findByIdAndUpdate(
id,
{
"$inc": {
"points": 5,
"numVotes": 1
}
},
{ "new": true }
).then(function(test) {
var userModel = test.schema.path("userCreated").options.ref;
mongoose.model(userModel).findByIdAndUpdate(
test.userCreated,
{ "$inc": { "points": 5 } },
{ "new": true }
).then(function(user) {
test.userCreated = user;
resolve(test);
})
}).catch(reject)
})
};
をそれはあなたによく合います:
testSchema.statics.incrementTest = function(id,callback) {
var self = this;
async.waterfall(
[
function(callback) {
self.findByIdAndUpdate(
id,
{
"$inc": {
"points": 5,
"numVotes": 1
}
},
{ "new": true },
callback
)
},
function(err,test) {
if (err) callback(err);
var userModel = test.schema.path("userCreated").options.ref;
mongoose.model(userModel).findByIdAndUpdate(
test.userCreated,
{ "$inc": { "points": 5 } },
{ "new": true },
function(err,user) {
if (typeof(user) !== "undefined")
test.userCreated = user;
callback(err,test);
}
);
}
],
callback
);
};
Test.incrementTest("56fe279d363ce91765d9e39e",function(err,test) {
if (err) throw err;
console.log(JSON.stringify(test,undefined,2));
})
の両方が両方のコレクションのために両方のオブジェクトでインクリメントデータを示しているバックあなたに結果を与えるべきである:
{ points: 5,
numVotes: 1,
__v: 0,
userCreated: { points: 5, __v: 0, _id: 56ff1aa6dba6d13e798fc894 },
createdAt: Sat Apr 02 2016 12:04:38 GMT+1100 (AEDT),
updatedAt: Sat Apr 02 2016 12:04:38 GMT+1100 (AEDT),
_id: 56fe279d363ce91765d9e39e }
種類が実際として格納されていないref'マングース '同様の用法を持っていドキュメントには、オブジェクトIDだけが格納されます。したがって、パス 'userCreated.points'はtestSchemaに存在しないため、更新できません。ユーザー文書を更新するために別の更新クエリを実行する必要があります。 –