Mongoose is unable to $unset a document property

399 views
Skip to first unread message

Lu4

unread,
Nov 15, 2013, 10:27:14 AM11/15/13
to mongoo...@googlegroups.com
I'm having difficulty with performing $unset operation for a document property that already resides in mongodb, but this property not present in Schema.

Mongoose treats this property like it has been just created it and thinks that it shouldn't be removed from database because it doesn't exist there, but in fact it does...

The problem is related to document.js file 
Document.prototype.$__shouldModify = function (
    pathToMark, path, constructing, parts, schema, val, priorVal) {

  if (this.isNew) return true;
  if (this.isDirectModified(pathToMark)) return false;

  if (undefined === val && !this.isSelected(path)) {
    // when a path is not selected in a query, its initial
    // value will be undefined.
    return true;
  }

  if (undefined === val && path in this.$__.activePaths.states.default) {
    // we're just unsetting the default value which was never saved         <---------------------- This assumption is wrong
    return false;
  }

  if (!deepEqual(val, priorVal || this.get(path))) {
    return true;
  }

  if (!constructing &&
      null != val &&
      path in this.$__.activePaths.states.default &&
      deepEqual(val, schema.getDefault(this, constructing))) {
    // a path with a default was $unset on the server
    // and the user is setting it to the same value again


could you please provide a guide or a fix for this problem.

Thanks!

Joe Wagner

unread,
Nov 17, 2013, 3:37:18 PM11/17/13
to mongoo...@googlegroups.com
From the Mongoose Docs 

"Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection." 

Can you provide a use case for wanting to $unset fields that don't exist in your schema?
Would doing some kind of mongodb data cleanup solve your problem?

Aaron Heckmann

unread,
Nov 20, 2013, 1:07:45 AM11/20/13
to mongoo...@googlegroups.com
Please open a ticket on github.com/learnboost/mongoose/issues/new if you haven't already done so. Please include code to reproduce the issue.

Thanks
Reply all
Reply to author
Forward
0 new messages