Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create mongoose schema dynamically?

Tags:

I have an app that works on node.js with MongoDB and mongoose. My app simply sends/deletes/edits form data and for that, I have such mongoose model:

var mongoose = require('mongoose');  module.exports = mongoose.model('appForm', {     User_id : {type: String},     LogTime : {type: String},     feeds : [        {         Name: {type: String},         Text : {type: String},     }     ] }); 

and that works just fine!

Now, I would like to add a function to the form so that the user can add a field(or fields) to form and enter a text in it and post it. Creating that dynamic functionality on the client side is no problem but I understand that my mongoose.model has to be correctly structured. My question is: how do I add that variable values(dynamically created form data name and its text) to mongoose schema?

I see that using strict: false and Schema.Types.Mixed is advised. however, I can't figure out... What I have tried:

var mongoose = require('mongoose'); var Schema = mongoose.Schema; var feedSchema = new Schema({strict:false});  module.exports = mongoose.model('appForm', feedSchema); 

Any tips? Thanks in advance!

like image 682
Mar Avatar asked Jan 27 '15 08:01

Mar


People also ask

Does Mongoose create collection automatically?

exports = mongoose. model('User',{ id: String, username: String, password: String, email: String, firstName: String, lastName: String }); It will automatically creates new "users" collection.

What is dynamic schema in MongoDB?

A dynamic schema supports fluent polymorphism. Embedded Data Model: MongoDB uses an embedded data model. In other words, we can define a document as a key/value pair in another document. Use of Index: We can define an index on any attributes of a MongoDB records that increase the speed of data fetching.

Does Mongoose auto generate ID?

_id field is auto generated by Mongoose and gets attached to the Model, and at the time of saving/inserting the document into MongoDB, MongoDB will use that unique _id field which was generated by Mongoose.

How does Mongoose schema work?

With Mongoose, you would define a Schema object in your application code that maps to a collection in your MongoDB database. The Schema object defines the structure of the documents in your collection. Then, you need to create a Model object out of the schema. The model is used to interact with the collection.


1 Answers

Apply the strict: false option to your existing schema definition by supplying it as a second parameter to the Schema constructor:

var appFormSchema = new Schema({     User_id : {type: String},     LogTime : {type: String},     feeds : [new Schema({         Name: {type: String},         Text : {type: String}     }, {strict: false})     ] }, {strict: false});  module.exports = mongoose.model('appForm', appFormSchema); 

If you want to leave feeds as fully schemaless, that's where you can used Mixed:

var appFormSchema = new Schema({     User_id : {type: String},     LogTime : {type: String},     feeds : [Schema.Types.Mixed] }, {strict: false});  module.exports = mongoose.model('appForm', appFormSchema); 
like image 132
JohnnyHK Avatar answered Oct 16 '22 03:10

JohnnyHK