3

I want to validate some inner elements of an object against schemas that are members of a larger schema before posting that object on the model correspondent to that larger schema.

Someone already asked this question and this answer has something similar on the second option proposed:

var p = new People({
    name: 'you',
    age: 3
});

p.validate(function(err) {
    if (err)
        console.log(err);
    else
        console.log('pass validate');
});

The problem is that before doing the validation one has to create a new model with var People = mongoose.model('People', peopleSchema);.

Does this mean in this case that a new collection 'People' will be stored in the MongoDB database?

Is the act of calling mongoose.model('People', peopleSchema); altering the database in any way?

If so, is there a way to do this validation without creating a new collection, in this case 'People'?

The schema I want to validate will be stored as a component of another document already in the database and I don't want to pollute the database with unused collections.

buzoherbert
  • 1,537
  • 1
  • 12
  • 34
  • it won't be stored in batabase until you write it into database – monkeyinsight Aug 12 '17 at 08:03
  • Thanks @monkeyinsight . The element won't be stored, but, is calling mongoose.model('People', peopleSchema); creating a new collection in the database? – buzoherbert Aug 12 '17 at 08:06
  • mongoose auto creates _collection_ on startup only if it has indexes. New _document_ is added to mongodb collection, only when you call `model.create()` or `document.save()`. – alexmac Aug 12 '17 at 10:31

0 Answers0