1

I have a collection which looks like this

{
   "_id":"5f573a697775e75af4399afe",
   "basics":{
      "location":{
         "_id":"5f576a397775e75af4399b00",
         "address":"2712 Broadway St",
         "postalCode":"CA 94115",
         "city":"San Francisco",
         "countryCode":"US",
         "region":"California",
         "hidden":"false"
      },
      "name":"John Doe",
      "label":"Programmer",
      "picture":"",
      "email":"john@gmail.com",
      "phone":"(912) 555-4321",
      "website":"http://johndoe.com",
      "summary":"A summary of John Doe...",
      "profiles":[
         {
            "_id":"5f573a697775e75af4399aff",
            "network":"Twitter",
            "username":"john",
            "url":"http://twitter.com/john",
            "hidden": false
         }
      ]
   },
   "work":[
      {
         "highlights":[
            "Started the company"
         ],
         "_id":"5f573a697775e75af4399b00",
         "company":"Company",
         "position":"President",
         "website":"http://company.com",
         "startDate":{
            "$date":"2013-01-01T00:00:00.000Z"
         },
         "endDate":{
            "$date":"2014-01-01T00:00:00.000Z"
         },
         "summary":"Description...",
         "hidden": false
      }
   ],
   "volunteer":[
      {
         "highlights":[
            "Awarded 'Volunteer of the Month'"
         ],
         "_id":"5f573a697775e75af4399b01",
         "organization":"Organization",
         "position":"Volunteer",
         "website":"http://organization.com/",
         "startDate":{
            "$date":"2012-01-01T00:00:00.000Z"
         },
         "endDate":{
            "$date":"2013-01-01T00:00:00.000Z"
         },
         "summary":"Description...",
         "hidden": false
      }
   ],
   "education":[
      {
         "courses":[
            "DB1101 - Basic SQL",
            "MTH 511 - Algorithmic Mathematics"
         ],
         "_id":"5f573a697775e75af4399b02",
         "institution":"University",
         "area":"Software Development",
         "studyType":"Bachelor",
         "startDate":{
            "$date":"2011-01-01T00:00:00.000Z"
         },
         "endDate":{
            "$date":"2013-01-01T00:00:00.000Z"
         },
         "gpa":"4.0",
         "hidden": false
      }
   ],
   "awards":[
      {
         "_id":"5f573a697775e75af4399b03",
         "title":"Award",
         "date":{
            "$date":"2014-11-01T00:00:00.000Z"
         },
         "awarder":"Company",
         "summary":"There is no spoon.",
         "hidden": false
      }
   ],
   "publications":[
      {
         "_id":"5f573a697775e75af4399b04",
         "name":"Publication",
         "publisher":"Company",
         "releaseDate":{
            "$date":"2014-10-01T00:00:00.000Z"
         },
         "website":"http://publication.com",
         "summary":"Description...",
         "highlights":[
            "Wrote this paper"
         ],
         "hidden": false
      }
   ],
   "skills":[
      {
         "keywords":[
            "HTML",
            "CSS",
            "Javascript"
         ],
         "_id":"5f573a697775e75af4399b05",
         "name":"Web Development",
         "level":"Master",
         "hidden": false
      }
   ],
   "languages":[
      {
         "_id":"5f573a697775e75af4399b06",
         "language":"English",
         "fluency":"Native speaker",
         "hidden": false
      }
   ],
   "interests":[
      {
         "keywords":[
            "Ferrets",
            "Unicorns"
         ],
         "_id":"5f573a697775e75af4399b07",
         "name":"Wildlife",
         "hidden": false
      }
   ],
   "references":[
      {
         "_id":"5f573a697775e75af4399b08",
         "name":"Jane Doe",
         "reference":"Reference...",
         "hidden": false
      }
   ],
   "__v":0
}

I have an api that responds with a json like this

{
    "id": "5f573a697775e75af4399afe",
    "quickText": "John Doe",
    "hidden": false,
    "location": [
        {
            "_id":"5f576a397775e75af4399b00",
            "quickText": "2712 Broadway St",
            "hidden": false
        }
    ],
    "profiles": [
        {
            "id": "5f573a697775e75af4399aff",
            "quickText": "Twitter",
            "hidden": false
        }
    ],
    "work": [
        {
            "id": "5f573a697775e75af4399b00",
            "quickText": "Company / President",
            "hidden": false
        }
    ],
    "volunteer": [
        {
            "id": "5f573a697775e75af4399b01",
            "quickText": "Organization / Volunteer",
            "hidden": false
        }
    ],
    "education": [
        {
            "id": "5f573a697775e75af4399b02",
            "quickText": "University / Software Development",
            "hidden": false
        }
    ],
    "publications": [
        {
            "id": "5f573a697775e75af4399b04",
            "quickText": "Publication",
            "hidden": false
        }
    ],
    "awards": [
        {
            "id": "5f573a697775e75af4399b03",
            "quickText": "Award",
            "hidden": false
        }
    ],
    "skills": [
        {
            "id": "5f573a697775e75af4399b05",
            "quickText": "Web Development",
            "hidden": false
        }
    ],
    "languages": [
        {
            "id": "5f573a697775e75af4399b06",
            "quickText": "English",
            "hidden": false
        }
    ],
    "interests": [
        {
            "id": "5f573a697775e75af4399b07",
            "quickText": "Wildlife",
            "hidden": false
        }
    ],
    "references": [
        {
            "id": "5f573a697775e75af4399b08",
            "quickText": "Jane Doe",
            "hidden": false
        }
    ]
}

On the frontend user changes values of hidden fields to either true or false and returns the same json object with updated hidden fields.

Now, how can I update only the changed values in NodeJS Mongoose, i.e. some hidden fields are changed from true to false and vice-versa.

I am working with Array Update Operators but couldn't figure it out for my scenario. As you may also see, location and profiles are nested within basics, however others are outside as in schema.

This is my route and login,

router.post('/hidden/:id', function(req, res, next) {
  let changedResume = req.body
  if(changedResume.profiles.length) {
    console.log('there is profile')
    changedResume.profiles.forEach(elem => {
      console.log(elem)
      Resume.findByIdAndUpdate(
        { "_id" : new ObjectId(changedResume.id), "basics.profiles._id": new ObjectId(elem.id)},
        { "$set": { "basics.profiles.$[element].hidden": elem.hidden}},
        { "arrayFilters": [{"element._id": new ObjectId(elem.id)}], "upsert": true}, 
        function(err, docs) {
          if (err){ 
            console.log(err) 
          } 
          else{ 
              console.log("Updated User : ", docs); 
          } 
        }
        )
    })
  }

With this controller I'll have to go through all arrays (above only shows for profiles) one by one and update all which was not even changed by user. Is there any other way I can update only the changed data?

Refereces:

Luzan Baral
  • 3,678
  • 5
  • 37
  • 68

0 Answers0