具有 find $in 值的 Mongo Upsert [英] Mongo Upsert with values of find $in

查看:34
本文介绍了具有 find $in 值的 Mongo Upsert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to run mongo updates by querying that the user id is in an array. Is it possible to upsert any values not in the array?

例如:

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

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

If I run this query I get one new doc with _id and score. What I'd like to do is have a new doc for each userId not present in the userId array:

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

这在 mongo 中可行吗?

Is this possible in mongo?

推荐答案

不,这不能完成.文档 指出:

No, this cannot be done. The documentation states:

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

If upsert is true and no document matches the query criteria, update() inserts a single document.

这篇关于具有 find $in 值的 Mongo Upsert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆