Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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]
}

right now i am doing this:

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
571 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can specify multiple fields to the $push operator

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...