Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pattern for updating multiple rows in an array attribute.

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rick. What is the best approach to deal with updating a document which has an attribute that is of type array, and you want to update all the elements in the array, or just some of them that meet a certain criteria? I know Mongo hasn't supported those types of updates yet.

So what would be a good workaround rather than brute force of first querying then looping through the results and updating them one at a time?

Any good examples of how you might do this with JavaScript code passed to Mongo?

Thanks

Mark
 
Rick Copeland
author
Greenhorn
Posts: 17
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can remove several elements from an array, or you can update a single element, but there is no way to update multiple elements in a single pass unless you replace the array all at once. To do the replacement, consider that you might want to increment all the integers in the 'b' property of the following document:



In this case, you might do the following in Javascript:



Hope that helps!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I was hoping that there might be a good trick out there. I have seen examples of increment. But as I always find in my use cases, they never seem to be that simple. It is always about changing different types of fields, and never increments.

Also, it would be great to know if they will ever allow you to use one of the field values from one attribute in the update of another attribute.

Thanks

Mark
 
Rick Copeland
author
Greenhorn
Posts: 17
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler wrote:Also, it would be great to know if they will ever allow you to use one of the field values from one attribute in the update of another attribute.


Unfortunately, it's not currently possible to update one field based on another field's values in MongoDB (like adding two fields together, for instance). You can do queries similar to this using the aggregation framework, but it's not really the same.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rick Copeland wrote:
Mark Spritzler wrote:Also, it would be great to know if they will ever allow you to use one of the field values from one attribute in the update of another attribute.


Unfortunately, it's not currently possible to update one field based on another field's values in MongoDB (like adding two fields together, for instance). You can do queries similar to this using the aggregation framework, but it's not really the same.


Thanks Rick for the replies.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic