mongo update query to ignore few fields in update operation

mongodb, mongodb-query, mongoskin, node.js

Solution

Your request for update actually does the following: it replaces everything in the document with the values provided by the request (except for the "_id" of course, which is immutable). What you want is what is called a "partial update" in mongosphere. Please have a look into the $set operator. So what you would do is something like:

db.yourcollection.update({"_id":"53555ef203dabf282b750a81"},
  {$set:
    {
      "categoryId":"53555ef203dabf282b750a81",
      "updatedByUserId":"5675561b25521601a5c9530",
      "dateUpdated":ISODate("2014-05-19T19:49:28.203Z")
    }
  }
)

As far as I know there is now way of sending a complete document to a mongo[s|d] and tell it to only skip certain fields.

Problem

I am using Nodejs based mongoskin driver for mongo database operation. I want to update my document however don't want to update few fields. Following are more details. Request for add: ``` { "name": "Theme Name", "description": "Theme Description", "createdByUserId": "53651221b25521601a5c9530", } ``` Request for update: ``` { "_id":"53555ef203dabf282b750a81" "name": "Theme Name", "categoryId": "53555ef203dabf282b750a81", "description": "Theme Description", "createdByUserId": "53651221b25521601a5c9530", "updatedByUserId": "5675561b25521601a5c9530", "dateCreated": ISODate("2014-05-19T19:47:26.603Z"), "dateUpdated": ISODate("2014-05-19T19:49:28.203Z"), } ``` I want to ignore following field send by client. 1. createdByUserId 2. dateCreated For time being I am taking following approach in update operation: 1. Read collection for given _id 2. Read these above two fields from database and update the request and then perform database update operation Looking help for clean approach.

Original source