在 mongodb 中的一次更新调用中推送到两个单独的数组 [英] Push to two separate arrays in one update call in mongodb

查看:12
本文介绍了在 mongodb 中的一次更新调用中推送到两个单独的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新以通过一次更新调用更新文档中的两个单独数组.有没有办法做到这一点?

I am trying to update to update two separate arrays in a document with one update call. Is there a way to do this?

例如,如果我有一个类似的文档:

For example if I have a document like:

{
  _id:1,
  array1:[1],
  array2:[4]
}

现在我正在这样做:

db.collection.update({_id:1},{$push:{array1:"2"}})
db.collection.update({_id:1},{$push:{array2:"5"}})

有没有办法将这种情况减少到一次调用?我试过只传递一个数组来推送,我在更新对象中尝试了多个推送语句,但这些语句不起作用.感谢您对此的帮助!

Is there a way to reduce this to just one call? I have tried just passing an array to push, i have tried multiple push statements in update object but those don't work. Thanks for your help with this!

推荐答案

您可以指定多个字段到 $push 运算符

You can specify multiple fields to the $push operator

db.collection.update(
   { _id :1 }, 
   { $push : { array1 : "1",   array2 : "5" }}
)

这篇关于在 mongodb 中的一次更新调用中推送到两个单独的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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