Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

MongoDB update document in array

farenda 2017-03-26 0

MongoDB has plenty of cool features to work with documents, but I found it less than obvious how to update document in array. So here it is…

Collection with array of documents

Test collection has documents with simple structure, where we keep authors as documents in an array:

{
  "_id" : ObjectId("58d7fcdf2ef1a79af37f59b0"),
  "title" : "Clean code",
  "authors" : [
     { "name" : "Robert C. Martin" }
  ]
}

Update document in array

Now, we want to patch document in array of authors. To do that we have to specify which document we want to update and which document in the array. If you don’t specify nested document, then you’ll see error 16837:

The positional operator did not find the match needed from the query. Unexpanded update: authors.$.name

Therefore the full update command looks like this:

db.books.update(
  {
    "_id" : ObjectId("58d7fcdf2ef1a79af37f59b0"),
    "authors.name": "Robert C. Martin"
  },
  {
    $set: {"authors.$.name": "Uncle Bob"}
  }
)

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

In the $set part of the update we refer to matching document in the array using $ (dollar sign). It is the position of the first array document that matches.

Now the document in the array has updated content:

db.books.find()
{
  "_id" : ObjectId("58d7fcdf2ef1a79af37f59b0"),
  "title" : "Clean code",
  "authors" : [
    { "name" : "Uncle Bob" }
  ]
}

References:

  • Checkout MongoDB Tutorial!
Share with the World!
Categories MongoDB Tags mongodb
Previous: Java regex remove duplicated words
Next: Java 8 Comparators with Method References

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored