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!










share|improve this question























  • Possible duplicate of Add field not in schema with mongoose
    – Anthony Winzlet
    Nov 9 at 5:01














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!










share|improve this question























  • Possible duplicate of Add field not in schema with mongoose
    – Anthony Winzlet
    Nov 9 at 5:01












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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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.






share|improve this answer






















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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.






share|improve this answer






















  • 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














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.






share|improve this answer






















  • 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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?