Unsetting values

1,457 views
Skip to first unread message

Jon Keating

unread,
Aug 1, 2011, 8:29:17 AM8/1/11
to Mongoose Node.JS ORM
Hello,

I would like to use $unset or something that gets the same result via
Mongoose. What are my options?

For details, take a look at this open question on Stack Overflow:
http://stackoverflow.com/questions/4486926/delete-a-key-from-a-mongodb-document-using-mongoose

Regards,
Jon

Aaron Heckmann

unread,
Aug 1, 2011, 10:29:14 AM8/1/11
to mongoo...@googlegroups.com
there isn't a way to do this at the moment.

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Laurie Harper

unread,
Aug 1, 2011, 12:13:58 PM8/1/11
to mongoo...@googlegroups.com
Presumably, though, you could drop to the native driver API directly, right? If not, would something like this work as a hack until Mongoose adds support:

var id = user._id;
user.remove()
user._id = id
delete user.key
user.save()

(Note to OP: not very efficient; may not be a good idea if you use DbRef's in your Mongo DB; may interact badly with any internal caches, etc, that Mongoose maintains... So, use with caution unless/until blessed by a Mongoose dev ;-)

L.

Aaron Heckmann

unread,
Aug 1, 2011, 12:33:06 PM8/1/11
to mongoo...@googlegroups.com
yeah you could always drop down to the driver:

User.collection.update(query, { $unset: ... }, options, cb)

Aaron Heckmann

unread,
Aug 1, 2011, 12:34:15 PM8/1/11
to mongoo...@googlegroups.com
On Mon, Aug 1, 2011 at 12:13 PM, Laurie Harper <lau...@holoweb.net> wrote:
var id = user._id;
user.remove()
user._id = id
delete user.key
user.save()

This won't actually work either.

--
Aaron


Laurie Harper

unread,
Aug 1, 2011, 12:37:26 PM8/1/11
to mongoo...@googlegroups.com
Good to know; out of interest, why not?

Aaron Heckmann

unread,
Aug 1, 2011, 12:43:41 PM8/1/11
to mongoo...@googlegroups.com
I thought someone would ask :)

The delete operation skirts mongooses methods and thus doesn't get marked as modified. When `save` is called, nothing gets updated b/c mongoose can't "see" the change.

Lets take it even further, suppose we "tell" mongoose that we changed it too:

delete user.key;
user.markModified('key');
user.save(...)

This will only succeed in setting the key to undefined instead of deleting it. So yeah, we should add some support for this sometime.

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Aaron Heckmann

unread,
Aug 1, 2011, 12:45:46 PM8/1/11
to mongoo...@googlegroups.com
BTW, all of the models schema properties are wrapped with getters and setters so we can detect the changes transparently in most cases, with the drawback of it not working for things like delete.

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Laurie Harper

unread,
Aug 1, 2011, 12:53:05 PM8/1/11
to mongoo...@googlegroups.com
Thanks for the explanation, makes perfect sense. Definitely something that will need API support at some point then :)

L.

David D.

unread,
Sep 13, 2011, 6:48:26 PM9/13/11
to Mongoose Node.JS ORM
Is there a ticket open for this? Just want to make sure this doesn't
get forgotten.

It'd be nice to be able to completely remove a field if we want to. So
far I haven't figured out how to do it. So all the fields I'm looking
to undo get '' or null. Ideally we could just get rid of the entire
key with something like $unset.

Aaron Heckmann

unread,
Sep 21, 2011, 7:02:34 AM9/21/11
to mongoo...@googlegroups.com
I don't think there's a ticket. Would you mind opening one?

David D.

unread,
Sep 22, 2011, 10:40:08 AM9/22/11
to Mongoose Node.JS ORM

David D.

unread,
Sep 22, 2011, 10:53:35 AM9/22/11
to Mongoose Node.JS ORM
Turns out there was a ticket already open...

https://github.com/LearnBoost/mongoose/issues/519

Aaron Heckmann

unread,
Sep 22, 2011, 4:40:38 PM9/22/11
to mongoo...@googlegroups.com
thanks David
Reply all
Reply to author
Forward
0 new messages