【问题标题】:Mongo Upsert with values of find $inMongo Upsert 的值为 find $in
【发布时间】:2017-10-18 05:44:41
【问题描述】:

我必须通过查询用户 ID 在数组中来运行 mongo 更新。是否可以插入任何不在数组中的值?

例如:

db.collection.update({
 userId:{
      $in:['1','2', '3']
    }
 }, 
 {$set: {score:30}}, 
 {upsert:true})

如果我运行这个查询,我会得到一个带有 _id 和 score 的新文档。我想做的是为 userId 数组中不存在的每个 userId 创建一个新文档:

[{userId:1, score:30, _id:...}, {userId:2, score: 30, _id: ...}, ...]

这在 mongo 中可行吗?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    不,这是不可能的。 documentation 声明:

    如果 upsert 为真且没有文档符合查询条件,update() 将插入 单个 文档。

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2015-10-07
      • 2021-08-28
      • 2013-11-24
      • 2015-11-30
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 2017-07-07
      相关资源
      最近更新 更多