我有一个包含子文档数组的文档(大小为 6),然后我同时删除 5 个元素,它完美地更新了数组,但是更新后找不到文档,这里是更新它的代码
await gameRoom.findOneAndUpdate({ 'players._id': currentUser._id }, { $pull: { players: { _id: currentUser._id } } }, { 'new': true },
(err, docc) => {
console.log('currentUser id ' + currentUser._id + ' time ' + new Date().toUTCString());
console.log('doc ' + JSON.stringify(docc));
if (err) console.log('error at logging out gameRoom.findOne ', '\n ' + err);
else if (docc == null) console.log('how it can be null');
});
这是结果
currentUser id 5eb285720105534970994c92 time Wed, 06 May 2020 09:38:26 GMT
doc null
how it can be null
currentUser id 5eb2856a0105534970994c89 time Wed, 06 May 2020 09:38:26 GMT
doc null
how it can be null
currentUser id 5eb2856f0105534970994c8f time Wed, 06 May 2020 09:38:26 GMT
doc IsNotNull
currentUser id 5eb2856d0105534970994c8c time Wed, 06 May 2020 09:38:26 GMT
doc null
how it can be null
currentUser id 5eb285750105534970994c96 time Wed, 06 May 2020 09:38:26 GMT
doc IsNotNull
currentUser id 5eb285680105534970994c85 time Wed, 06 May 2020 09:38:30 GMT
doc IsNotNull
所以首先我从数组中拉出 5 个元素,然后我拉出最后一个元素,正如您在结果中看到的那样,所有这些元素都通过更新完美地从数组中删除,但其中一些无法到达他们更新的文档,这怎么可能?