MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

MongoDB Update Statement

without comments

While discussing the pros and cons of MongoDB, my students wanted to know how to update a specific element in a collection. Collections are like tables in relational databases.

You create the users collection by inserting rows like this:

db.users.insert(
[
  { contact_account: "CA_20170321_0001"
  , first_name: "Jonathan"
  , middle_name: "Eoin"
  , last_name: "MacGregor"
  , addresses:
    {
      street_address: ["1111 Broadway","Suite 101"]
    , city: "Oakland"
    , state: "CA"
    , zip: "94607" 
    }
  }
, { contact_account: "CA_20170328_0001"
  , first_name: "Remington"
  , middle_name: "Alain" 
  , last_name: "Dennison"
  , addresses:
    {
      street_address: ["2222 Broadway","Suite 121"]
    , city: "Oakland"
    , state: "CA"
    , zip: "94607" 
    }
  }
])

You can query the results with the db.users.find({}) command, or you can query the formatted results with the following command:

db.users.find({}).pretty()

You can provide a simple update of middle_name element of a given collection element with the findAndModify() function. The following queries the users collection to find the JSON middle_name element where the contact_account value is equal to the “CA_20170330_0001” string.

db.users.findAndModify(
  { query: { contact_account: "CA_20170328_0001" }
  , update: { $set: { middle_name: "Alan" }}
  , upsert: false })

After changing the middle_name value from “Alain” to “Alan”, you can query the single element of the collection with the following:

db.users.find({ contact_account: "CA_20170328_0001" }).pretty()

It should return the following:

{
        "_id" : ObjectId("5bd7f69ba135dda917665de7"),
        "contact_account" : "CA_20170328_0001",
        "first_name" : "Remington",
        "middle_name" : "Alan",
        "last_name" : "Dennison",
        "addresses" : {
                "street_address" : [
                        "2222 Broadway",
                        "Suite 121"
                ],
                "city" : "Oakland",
                "state" : "CA",
                "zip" : "94607"
        }
}

You can replace the addresses string element value a collection of elements with the following findAndModify() function:

db.users.findAndModify(
  { query: { contact_account: "CA_20170328_0001" }
  , update:
    { $set:
      { addresses:
        [
          {
            active_status: true
          , start_date : new Date("2018-10-30")
          , street_address: ["2222 Broadway","Suite 121"]
          , city: "Oakland"
          , state: "CA"
          , zip: "94607" 
          }
        , {
            active_status: false
          , start_date: new Date("2017-10-01")
          , end_date : new Date("2018-10-29")
          , street_address: ["2222 Broadway","Suite 121"]
          , city: "Oakland"
          , state: "CA"
          , zip: "94607" 
          }
        ]
      }
    }
  , upsert: false })

You can re-query the modified result set with find() function with the same query syntax as used previously. This looks for a specific member element in the collection by matching the contact_account name’s value pair. It is the same as the one used earlier in this blog post.

db.users.find({ contact_account: "CA_20170328_0001" }).pretty()

It should return the following:

{
        "_id" : ObjectId("5bd7f69ba135dda917665de7"),
        "contact_account" : "CA_20170328_0001",
        "first_name" : "Remington",
        "middle_name" : "Alan",
        "last_name" : "Dennison",
        "addresses" : [
                {
                        "active_status" : true,
                        "start_date" : ISODate("2018-10-30T00:00:00Z"),
                        "street_address" : [
                                "2222 Broadway",
                                "Suite 121"
                        ],
                        "city" : "Oakland",
                        "state" : "CA",
                        "zip" : "94607"
                },
                {
                        "active_status" : false,
                        "start_date" : ISODate("2017-10-01T00:00:00Z"),
                        "end_date" : ISODate("2018-10-29T00:00:00Z"),
                        "street_address" : [
                                "2222 Broadway",
                                "Suite 121"
                        ],
                        "city" : "Oakland",
                        "state" : "CA",
                        "zip" : "94607"
                }
        ]
}

As always, I hope this helps someone.

Written by maclochlainn

October 30th, 2018 at 12:22 am

Posted in Linux,MongoDB,Unix

Tagged with