Mongo Upsert,其值为find $ in

时间:2017-10-17 15:40:12

标签: mongodb

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

例如:

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

如果我运行此查询,我会获得一个带有_id和得分的新文档。我想要做的是为userId数组中没有的每个userId提供一个新文档:

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

mongo有可能吗?

1 个答案:

答案 0 :(得分:1)

不,这不可能。 documentation州:

  

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