wood burning stoves 2.0*
The moose likes MongoDB and the fly likes Pattern for updating multiple rows in an array attribute. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » MongoDB
Bookmark "Pattern for updating multiple rows in an array attribute." Watch "Pattern for updating multiple rows in an array attribute." New topic
Author

Pattern for updating multiple rows in an array attribute.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rick Copeland
author
Greenhorn

Joined: Apr 09, 2013
Posts: 17
    
    5
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Apr 09, 2013
Posts: 17
    
    5
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
 
 
subject: Pattern for updating multiple rows in an array attribute.