MongoDB vs Mongoose

Jan 14, 2016 02:01

  • MongoDB has two editing functions: insert & update. Both accept two parameters, a query/filter and the data. They also have an alias for both functions in one: save. Insert is self-explanatory, but update is not. It will update specific fields of records identified by the query/filter if the $set operator is used (eg - { $set: { field: value, .. } }) BUT will wipe out the entire document(s) and replace entirely if a JSON object is provided (eg - { field: value, .. }). And then, save behaves like a hybrid of both but condenses both parameters into a single document argument BUT it always wipes out data (which obviously is expected for an insert since yeah . . ).
  • Mongoose has unfortunately similarly named functions with different functionality and methodology. There is a save method for a document which will behave intuitively based on whether it's a new instance of a model or pre-existing record that's been returned by a call to find. The update method is a class method that takes two parameters like its Mongo counterpart BUT it defaults to only updating specific fields unless explicitly told to overwrite the entire document. (Yes, this is the exact opposite default behavior of its counterpart.) There is also a useful class method, Model.create, that simply inserts a record.
  • Previous post Next post
    Up