How do I add new keys to a Mongoose Schema?
up vote
0
down vote
favorite
I recently started developing an app for my senior project which requires me to use some type of database. For that I decided to go with Mongoose since it is noSQL and slightly easier to pick up.
So, fast forward and I run into a problem where I can't figure out how to edit an already existing Schema and add new keys into it.
For example, I have this Schema which represents a post(think Tweets or Facebook posts) that holds:
- A string that holds the body of the post
- The id of the user that created the post
- The Date of when the post was created
My code for that is:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
);
// Create collection and add schema
mongoose.model('posts', PostsSchema, 'posts');
What I want now is to access that schema in some way and add a new key to it using something similar to maybe
PostsSchema.add(Private: default: false);
Meaning that, now the schema in the database will look something like:
"_id":
"$oid": "1831g98af21n9s5u7s9ccchj5"
,
"Value": "Beautiful day outside, can't wait to go jogging!",
"User":
"$oid": "9a79ab143lbk9lk55wq327oi3226m"
,
"Date":
"$date": "2018-10-29T01:28:44.408Z"
,
"Private": "false"
"__v": 0
So back to my question, is there any way to do this? Or if you have a link to documentation of such methods I would greatly appreciate it. Thank you Greatly!
javascript mongodb mongoose mlab
add a comment |
up vote
0
down vote
favorite
I recently started developing an app for my senior project which requires me to use some type of database. For that I decided to go with Mongoose since it is noSQL and slightly easier to pick up.
So, fast forward and I run into a problem where I can't figure out how to edit an already existing Schema and add new keys into it.
For example, I have this Schema which represents a post(think Tweets or Facebook posts) that holds:
- A string that holds the body of the post
- The id of the user that created the post
- The Date of when the post was created
My code for that is:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
);
// Create collection and add schema
mongoose.model('posts', PostsSchema, 'posts');
What I want now is to access that schema in some way and add a new key to it using something similar to maybe
PostsSchema.add(Private: default: false);
Meaning that, now the schema in the database will look something like:
"_id":
"$oid": "1831g98af21n9s5u7s9ccchj5"
,
"Value": "Beautiful day outside, can't wait to go jogging!",
"User":
"$oid": "9a79ab143lbk9lk55wq327oi3226m"
,
"Date":
"$date": "2018-10-29T01:28:44.408Z"
,
"Private": "false"
"__v": 0
So back to my question, is there any way to do this? Or if you have a link to documentation of such methods I would greatly appreciate it. Thank you Greatly!
javascript mongodb mongoose mlab
Possible duplicate of Add field not in schema with mongoose
– Anthony Winzlet
Nov 9 at 5:01
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I recently started developing an app for my senior project which requires me to use some type of database. For that I decided to go with Mongoose since it is noSQL and slightly easier to pick up.
So, fast forward and I run into a problem where I can't figure out how to edit an already existing Schema and add new keys into it.
For example, I have this Schema which represents a post(think Tweets or Facebook posts) that holds:
- A string that holds the body of the post
- The id of the user that created the post
- The Date of when the post was created
My code for that is:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
);
// Create collection and add schema
mongoose.model('posts', PostsSchema, 'posts');
What I want now is to access that schema in some way and add a new key to it using something similar to maybe
PostsSchema.add(Private: default: false);
Meaning that, now the schema in the database will look something like:
"_id":
"$oid": "1831g98af21n9s5u7s9ccchj5"
,
"Value": "Beautiful day outside, can't wait to go jogging!",
"User":
"$oid": "9a79ab143lbk9lk55wq327oi3226m"
,
"Date":
"$date": "2018-10-29T01:28:44.408Z"
,
"Private": "false"
"__v": 0
So back to my question, is there any way to do this? Or if you have a link to documentation of such methods I would greatly appreciate it. Thank you Greatly!
javascript mongodb mongoose mlab
I recently started developing an app for my senior project which requires me to use some type of database. For that I decided to go with Mongoose since it is noSQL and slightly easier to pick up.
So, fast forward and I run into a problem where I can't figure out how to edit an already existing Schema and add new keys into it.
For example, I have this Schema which represents a post(think Tweets or Facebook posts) that holds:
- A string that holds the body of the post
- The id of the user that created the post
- The Date of when the post was created
My code for that is:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
);
// Create collection and add schema
mongoose.model('posts', PostsSchema, 'posts');
What I want now is to access that schema in some way and add a new key to it using something similar to maybe
PostsSchema.add(Private: default: false);
Meaning that, now the schema in the database will look something like:
"_id":
"$oid": "1831g98af21n9s5u7s9ccchj5"
,
"Value": "Beautiful day outside, can't wait to go jogging!",
"User":
"$oid": "9a79ab143lbk9lk55wq327oi3226m"
,
"Date":
"$date": "2018-10-29T01:28:44.408Z"
,
"Private": "false"
"__v": 0
So back to my question, is there any way to do this? Or if you have a link to documentation of such methods I would greatly appreciate it. Thank you Greatly!
javascript mongodb mongoose mlab
javascript mongodb mongoose mlab
edited Nov 9 at 2:51
asked Nov 9 at 2:13
Jas Singh
74
74
Possible duplicate of Add field not in schema with mongoose
– Anthony Winzlet
Nov 9 at 5:01
add a comment |
Possible duplicate of Add field not in schema with mongoose
– Anthony Winzlet
Nov 9 at 5:01
Possible duplicate of Add field not in schema with mongoose
– Anthony Winzlet
Nov 9 at 5:01
Possible duplicate of Add field not in schema with mongoose
– Anthony Winzlet
Nov 9 at 5:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Just add the field to the schema with a default:
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
,
Private: type: Boolean, default: 'false'
);
Since you have a default any new record will have it as well as any new instance of an old model saved before you added the private field.
If you really need more dynamic approach the usual recommendation is using Mixed Type with all the pluses and minuses that come with it.
I'm aware of what you recommended but I wanted to do it dynamically, if I can call it that. To explain better, imagine an app that allows users to create their own human. By default we might give the human keys like arms, legs, head, etc. Now you might want to allow the user to add different characteristics to the Schema like torso, nose, and so on. This is a very basic example but it is essentially the functionality that I want to access so I can edit an already existing schema.
– Jas Singh
Nov 9 at 4:01
Not something that is by design or recommended as a best practice. More here: github.com/Automattic/mongoose/issues/1867
– Akrion
Nov 9 at 4:05
What would be a good alternative to my initial approach rather modifying the initial Schema?
– Jas Singh
Nov 9 at 4:19
The usual recommendation as I updated the answer is simply using Mixed type. That way you can add anything you want. It is flexible but you have to deal with change tracking etc.
– Akrion
Nov 9 at 4:31
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Just add the field to the schema with a default:
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
,
Private: type: Boolean, default: 'false'
);
Since you have a default any new record will have it as well as any new instance of an old model saved before you added the private field.
If you really need more dynamic approach the usual recommendation is using Mixed Type with all the pluses and minuses that come with it.
I'm aware of what you recommended but I wanted to do it dynamically, if I can call it that. To explain better, imagine an app that allows users to create their own human. By default we might give the human keys like arms, legs, head, etc. Now you might want to allow the user to add different characteristics to the Schema like torso, nose, and so on. This is a very basic example but it is essentially the functionality that I want to access so I can edit an already existing schema.
– Jas Singh
Nov 9 at 4:01
Not something that is by design or recommended as a best practice. More here: github.com/Automattic/mongoose/issues/1867
– Akrion
Nov 9 at 4:05
What would be a good alternative to my initial approach rather modifying the initial Schema?
– Jas Singh
Nov 9 at 4:19
The usual recommendation as I updated the answer is simply using Mixed type. That way you can add anything you want. It is flexible but you have to deal with change tracking etc.
– Akrion
Nov 9 at 4:31
add a comment |
up vote
0
down vote
accepted
Just add the field to the schema with a default:
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
,
Private: type: Boolean, default: 'false'
);
Since you have a default any new record will have it as well as any new instance of an old model saved before you added the private field.
If you really need more dynamic approach the usual recommendation is using Mixed Type with all the pluses and minuses that come with it.
I'm aware of what you recommended but I wanted to do it dynamically, if I can call it that. To explain better, imagine an app that allows users to create their own human. By default we might give the human keys like arms, legs, head, etc. Now you might want to allow the user to add different characteristics to the Schema like torso, nose, and so on. This is a very basic example but it is essentially the functionality that I want to access so I can edit an already existing schema.
– Jas Singh
Nov 9 at 4:01
Not something that is by design or recommended as a best practice. More here: github.com/Automattic/mongoose/issues/1867
– Akrion
Nov 9 at 4:05
What would be a good alternative to my initial approach rather modifying the initial Schema?
– Jas Singh
Nov 9 at 4:19
The usual recommendation as I updated the answer is simply using Mixed type. That way you can add anything you want. It is flexible but you have to deal with change tracking etc.
– Akrion
Nov 9 at 4:31
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Just add the field to the schema with a default:
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
,
Private: type: Boolean, default: 'false'
);
Since you have a default any new record will have it as well as any new instance of an old model saved before you added the private field.
If you really need more dynamic approach the usual recommendation is using Mixed Type with all the pluses and minuses that come with it.
Just add the field to the schema with a default:
const PostsSchema = new Schema(
Value:
type: String,
required: true
,
User:
type: Schema.Types.ObjectId,
ref:'users'
,
Date:
type: Date,
default: Date.now
,
Private: type: Boolean, default: 'false'
);
Since you have a default any new record will have it as well as any new instance of an old model saved before you added the private field.
If you really need more dynamic approach the usual recommendation is using Mixed Type with all the pluses and minuses that come with it.
edited Nov 9 at 4:29
answered Nov 9 at 3:49
Akrion
7,91411222
7,91411222
I'm aware of what you recommended but I wanted to do it dynamically, if I can call it that. To explain better, imagine an app that allows users to create their own human. By default we might give the human keys like arms, legs, head, etc. Now you might want to allow the user to add different characteristics to the Schema like torso, nose, and so on. This is a very basic example but it is essentially the functionality that I want to access so I can edit an already existing schema.
– Jas Singh
Nov 9 at 4:01
Not something that is by design or recommended as a best practice. More here: github.com/Automattic/mongoose/issues/1867
– Akrion
Nov 9 at 4:05
What would be a good alternative to my initial approach rather modifying the initial Schema?
– Jas Singh
Nov 9 at 4:19
The usual recommendation as I updated the answer is simply using Mixed type. That way you can add anything you want. It is flexible but you have to deal with change tracking etc.
– Akrion
Nov 9 at 4:31
add a comment |
I'm aware of what you recommended but I wanted to do it dynamically, if I can call it that. To explain better, imagine an app that allows users to create their own human. By default we might give the human keys like arms, legs, head, etc. Now you might want to allow the user to add different characteristics to the Schema like torso, nose, and so on. This is a very basic example but it is essentially the functionality that I want to access so I can edit an already existing schema.
– Jas Singh
Nov 9 at 4:01
Not something that is by design or recommended as a best practice. More here: github.com/Automattic/mongoose/issues/1867
– Akrion
Nov 9 at 4:05
What would be a good alternative to my initial approach rather modifying the initial Schema?
– Jas Singh
Nov 9 at 4:19
The usual recommendation as I updated the answer is simply using Mixed type. That way you can add anything you want. It is flexible but you have to deal with change tracking etc.
– Akrion
Nov 9 at 4:31
I'm aware of what you recommended but I wanted to do it dynamically, if I can call it that. To explain better, imagine an app that allows users to create their own human. By default we might give the human keys like arms, legs, head, etc. Now you might want to allow the user to add different characteristics to the Schema like torso, nose, and so on. This is a very basic example but it is essentially the functionality that I want to access so I can edit an already existing schema.
– Jas Singh
Nov 9 at 4:01
I'm aware of what you recommended but I wanted to do it dynamically, if I can call it that. To explain better, imagine an app that allows users to create their own human. By default we might give the human keys like arms, legs, head, etc. Now you might want to allow the user to add different characteristics to the Schema like torso, nose, and so on. This is a very basic example but it is essentially the functionality that I want to access so I can edit an already existing schema.
– Jas Singh
Nov 9 at 4:01
Not something that is by design or recommended as a best practice. More here: github.com/Automattic/mongoose/issues/1867
– Akrion
Nov 9 at 4:05
Not something that is by design or recommended as a best practice. More here: github.com/Automattic/mongoose/issues/1867
– Akrion
Nov 9 at 4:05
What would be a good alternative to my initial approach rather modifying the initial Schema?
– Jas Singh
Nov 9 at 4:19
What would be a good alternative to my initial approach rather modifying the initial Schema?
– Jas Singh
Nov 9 at 4:19
The usual recommendation as I updated the answer is simply using Mixed type. That way you can add anything you want. It is flexible but you have to deal with change tracking etc.
– Akrion
Nov 9 at 4:31
The usual recommendation as I updated the answer is simply using Mixed type. That way you can add anything you want. It is flexible but you have to deal with change tracking etc.
– Akrion
Nov 9 at 4:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218884%2fhow-do-i-add-new-keys-to-a-mongoose-schema%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Add field not in schema with mongoose
– Anthony Winzlet
Nov 9 at 5:01