How Do I Update Fields Inside An Array?

23 views
Skip to first unread message

Floppy Disk

unread,
Sep 23, 2016, 6:29:00 PM9/23/16
to mongodb-user
I have a document with an array for developers and I've incorrectly inserted one of their names. How would I correct just "Judy Graham"?

{
 
"_id" : ObjectId("57e5a1fe67b6bb963d17818d"),
 
"app" : "Solarbreeze",
 
"active" : true,
 
"lastUpdated" : ISODate("2016-09-23T21:43:26.941Z"),
 
"ver" : 9,
 
"developers" : [
 
{
 
"name" : "Judith Graham",
 
"customers" : [
 
{
 
"customer" : "Katz",
 
"ver" : 8,
 
"loc" : "USA",
 
"active" : true,
 
"lastUpdated" : ISODate("2016-09-23T21:43:26.941Z")
 
},
 
{
 
"customer" : "Blogspan",
 
"ver" : 5,
 
"loc" : "USA",
 
"active" : false,
 
"lastUpdated" : ISODate("2016-01-28T11:28:26.941Z")
 
}
 
]
 
},
 
{
 
"name" : "Bruce Mason",
 
"customers" : [
 
{
 
"customer" : "Tagopia",
 
"ver" : 3,
 
"loc" : "USA",
 
"active" : false,
 
"lastUpdated" : ISODate("2014-14-12T19:40:26.941Z")
 
},
 
{
 
"customer" : "Feedfire",
 
"ver" : 3,
 
"loc" : "China",
 
"active" : false,
 
"lastUpdated" : ISODate("2015-12-11T21:00:26.941Z")
 
},
 
{
 
"customer" : "Wikido",
 
"ver" : 9,
 
"loc" : "Japan",
 
"active" : true,
 
"lastUpdated" : ISODate("2014-03-23T13:13:26.941Z")
 
}

Chris Cunningham

unread,
Oct 4, 2016, 7:46:41 PM10/4/16
to mongodb-user

Hi Carlos,

There are a couple of ways you may want to update just “Judith Graham” of the developers array:

  • To change the “Judith Graham” to “Judy Graham”, use the $ (update) positional operator. The query would look like this:

    db.collection.update({ "_id": ObjectId("57e5a1fe67b6bb963d17818d"),"developers.name": "Judith Graham" },
        { $set: { "developers.$.name" : "Judy Graham" } })
    
  • To change the customer name “Katz” to “Dogz” for the developer “Judith Graham” use $set elements in an array. The query would look like this:

    db.collection.update({_id:ObjectId("57e5a1fe67b6bb963d17818d")},
      {$set:{"developers.0.customers.0.customer":"Dogz"}})
    
  • To change the customer name “Blogspan” to “C-Span” for the developer “Judith Graham” you would use $set and would select the next element in the array. The query would look like this:

    db.collection.update({_id:ObjectId("57e5a1fe67b6bb963d17818d")},
      {$set:{"developers.0.customers.1.customer":"C-Span"}})
    
  • To add a new customer for the developer “Judith Graham” without overwriting any existing customers, combine $addToSet and the $ (update) positional operator.
    The query would look like this:

    db.collection.update({ "_id": ObjectId("57e5a1fe67b6bb963d17818d"), "developers.name": "Judith Graham" },
    { $addToSet: 
      {"developers.$.customers" : 
        {"customer" : 
            "ACME, Inc.", 
            "ver" : 1, 
            "loc" : "American Southwest", 
            "active" : true, 
            "lastUpdated" : ISODate("1968-09-23T21:43:26.941Z") 
          }
      } 
    }
    )
    

Please note that MongoDB supports a flexible schema, if this kind of update is part of a frequently used operation in your use case you may want to reconsider your data model/schema. Please review the Data Model Design documentation for more information.

Thanks,

Chris


Reply all
Reply to author
Forward
0 new messages