can i model.get, specifying the type to return, with the field defined in the schema?

1 view
Skip to first unread message

Derick Bailey

unread,
Mar 10, 2015, 1:52:12 PM3/10/15
to mongoo...@googlegroups.com
Hi!

I have a sub-sub-sub document that is changing... the old / original version of this field is defined as an object with 3 fields in it:

  stepFamily: {
    name: {type: String, required: true, default: ""},
    description: {type: String, required: true, default: ""},
    slug: {type: String, required: true, default: ""},
  },

the new version of the field needs to change this to a simple string

  stepFamily: {type: String, required: true, default: ""},

after changing my schema definition, i'm trying to write some code that will transform the original data in to the new string... and i'm running in to a problem. I can't get my original object out of the field.

every time I try to .get the field, specifying Object as the type to retrieve:

myObj.get("stepFamily", Object); 

it returns the value as '[object Object]' - the .toString() representation of an object literal... it's as if the .get method is ignoring the type parameter, because the field is defined in the schema as a type.

has anyone run in to this problem? know of a workaround for this, so that I can get my original object out of the field?

Thanks,

    - Derick

Ryan Wheale

unread,
Mar 10, 2015, 4:11:02 PM3/10/15
to mongoo...@googlegroups.com
This is kind of "standard" for doing in-database schema updates like this.  

1) Update your schema to have both fields:

stepFamily: Object
strStepFamily: String

2) Copy and format the data from stepFamily to strStepFamily
3) Update your schema file again:

stepFamily: String
strStepFamily: String

4) Copy all the information back
5) Delete strStepFamily

I forgot to mention 2a: validate that everything made it.
Also forgot step 0: backup your database.

~Ryan

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages