How do you use Mongoose without defining a schema?

In previous versions of Mongoose (for node.js), it was possible to use it without defining a scheme

var collection = mongoose.noSchema(db, "User"); 

But in the current version, the "noSchema" function has been removed. My schemes can change often and really don’t fit into a certain scheme, so is there a new way to use models without a scheme in mongoose?

+66
mongodb mongoose
Mar 20 '11 at 19:22
source share
4 answers

I think this is what you are looking for Mongoose Strict

: strict

A strict option (enabled by default) ensures that values ​​added to our model instance that were not specified in our schema are not stored in db.

Note. Do not set the value to false unless you have a good reason.

  var thingSchema = new Schema({..}, { strict: false }); var Thing = mongoose.model('Thing', thingSchema); var thing = new Thing({ iAmNotInTheSchema: true }); thing.save() // iAmNotInTheSchema is now saved to the db!! 
+104
Sep 12 '12 at 13:21
source share

In fact, the "Mixed" mode ( Schema.Types.Mixed ) looks like in Mongoose ...

it accepts, without a schema , a free-form JS object - so no matter what you choose. It seems you will have to start saving this object manually after that, but that seems like a fair compromise.

Mixed

“Something goes” SchemaType, its flexibility comes in a compromise it is more difficult to maintain. Mixed is available either through Schema.Types.Mixed or by passing an empty object literal. the following are equivalent:

 var Any = new Schema({ any: {} }); var Any = new Schema({ any: Schema.Types.Mixed }); 

Since this is a type without a schema, you can change the value to anything else you like, but Mongoose loses its ability to automatically detect and save these changes. To “tell” Mongoose that the value of the mixed type has changed, call the .markModified(path) method of passing the document the path to the mixed type that you just changed.

 person.anything = { x: [3, 4, { y: "changed" }] }; person.markModified('anything'); person.save(); // anything will now get saved 
+48
Jan 23 '12 at 16:23
source share
Hey Chris, look Mongous . I had the same problem with mongoose, as my circuits very often change right now in the development process. Monguian allowed me to have the simplicity of the Mongoose, although I was free to define and modify my "schemes." I decided to just create standard JavaScript objects and save them in a database like this
 function User(user){ this.name = user.name , this.age = user.age } app.post('save/user', function(req,res,next){ var u = new User(req.body) db('mydb.users').save(u) res.send(200) // that it! You've saved a user }); 

Much easier than Mongoose, although I really think you missed some cool mid-tier stuff, like "pre". This is by no means necessary for me. Hope this helps !!!

+11
Mar 21 2018-11-21T00:
source share

Its no longer possible.

You can use Mongoose with collections with a schema and node driver, or another mongo module for these schemas.

https://groups.google.com/forum/#!msg/mongoose-orm/Bj9KTjI0NAQ/qSojYmoDwDYJ

-6
Mar 21 2018-11-11T00:
source share



All Articles