• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pattern for updating multiple rows in an array attribute.

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 17
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic