我正在尝试通过如下查询一次更新/创建多个文档:
Follower.updateAsync(
{ organization: event.organization, user: { $each: userIds.map(userId => userId) } },
{ organization: event.organization, user: { $each: userIds.map(userId => userId) } },
{ new: true, upsert: true, setDefaultsOnInsert: true, multi: true },
);
但是,在此示例中,user
是ObjectId
,根据Mongo文档,使用$each
仅适用于数组。我可以做些什么来基本上遍历一组id并让它一次在这里创建/更新所有内容?
我知道如果我做这样的事情,条目将被创建/更新,但是我需要能够对一系列的userIds做到这一点:
Follower.updateAsync(
{ organization: event.organization, user: userIds[0]._id },
{ organization: event.organization, user: userIds[0]._id },
{ new: true, upsert: true, setDefaultsOnInsert: true, multi: true },
);