24
votes

I'm playing around with quick start guide for mongoose.

http://mongoosejs.com/docs/index.html

I assumed that it would throw an error when I saved a document with a field NOT defined in the schema. Instead, it created a new document in the collection but without the field. (Note: I realize mongodb itself is "schema-less" so each document in a collection can be completely different from each other.)

two questions

  1. How does mongoose handle adding documents that have fields that are NOT part of the schema? It seems like it just ignore them, and if none of the fields map, will create an empty document just with an ObjectId.
  2. And how do you get mongoose to warn you if a specific field of a document hasn't been added even though the document successfully saved?

(The question is - I believe - simple enough, so I didn't add code, but I definitely will if someone requests.)

Thanks.

2
I realize your question is tagged Mongoose, but one thing I've found really useful and minimalistic is Guille's monk package. I use it wrapped in Kris Kowal's Q to make a very nice promise-based method to access MongoDB.Ram Rajamony
Hey, I definitely love suggestions. Great suggestions too. I just gave both packages a glance and they both are clearly worth playing with and getting to know. I spent a fair amount of time making sure mongoose was a good place to build on, and then came to conclusion, Yes! It's really fantastic. And Guille is one of the authors of both mongoose and monk.cathy.sasaki

2 Answers

27
votes

Q: How does mongoose handle adding documents that have fields that are NOT part of the schema?

The strict option, (enabled by default), ensures that values passed to our model constructor that were not specified in our schema do not get saved to the db. - mongoose docs

Q: How do you get mongoose to warn you if a specific field of a document hasn't been added even though the document successfully saved?

The strict option may also be set to "throw" which will cause errors to be produced instead of dropping the bad data. - mongoose docs

...but if you absolutely require saving keys that aren't in the schema, then you have to handle this yourself. Two approaches I can think of are:

1. To save keys that aren't in the schema, you could set strict to false on a specific model instance or on a specific update. Then, you'd need to write some validation that (a) the values in the document conformed to your standards and (b) the document saved in the database matched the document you sent over.

2. You could see if the Mixed schema type could serve your needs instead of disabling the validations that come with strict. (Scroll down to 'usage notes' on that link, as the link to the 'Mixed' documentation seems broken for the moment.)

1
votes

Mongoose lets you add "validator" and "pre" middleware that perform useful functions. For instance, you could specify the required attribute in your schema to indicate that a specific property must be set. You could also specify a validator that you can craft to throw an error if the associated property doesn't meet your specifications. You can also set up a Mongoose "pre" validator that examines the document and throws an Error if it finds fields that are outside of your schema. By having your middleware call next() (or not), you can control whether you proceed to the document save (or not).

This question/response on stackoverflow can help with figuring out whether or not an object has a property.