Skip to content

Instantly share code, notes, and snippets.

@jrichardsz
Forked from kernow/gist:840796
Last active May 15, 2019 21:36
Show Gist options
  • Save jrichardsz/8adc88916f24e7782e78e380f9fddb55 to your computer and use it in GitHub Desktop.
Save jrichardsz/8adc88916f24e7782e78e380f9fddb55 to your computer and use it in GitHub Desktop.
mongo mongodb mongoose create find all
var sys = require('sys')
, mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test_mongoose');
// Models
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username : String
, uid : String
, messaged_on : Date
});
mongoose.model('User', UserSchema);
var User = mongoose.model('User');
// create a new user
var user = new User({
uid : '54321'
, username : 'Bob'
, messaged_on : Date.now()
});
user.save();
User.find().all(function(user) {
sys.puts(user);
});

I think this is what are you looking for Mongoose Strict

option: strict

The strict option, (enabled by default), ensures that values added to our model instance that were not specified in our schema do not get saved to the db.

Note: Do not set to false unless you have 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!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment