24

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.

cathy.sasaki
  • 3,995
  • 6
  • 28
  • 39
  • 1
    I realize your question is tagged Mongoose, but one thing I've found really useful and minimalistic is Guille's [monk](https://github.com/LearnBoost/monk) package. I use it wrapped in Kris Kowal's [Q](https://github.com/kriskowal/q) to make a very nice promise-based method to access MongoDB. – Ram Rajamony May 12 '13 at 17:33
  • 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 May 12 '13 at 17:54

2 Answers2

27

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.)

Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
  • Super. You pinpointed the solution perfectly. It's easy to get lost in docs and miss things. Thanks! For now, I want strict; your suggestions for situations in the future when I may not, I smart. Great answer. – cathy.sasaki May 13 '13 at 01:47
  • When I use entry.save({strict:'throw'}, function(err, entry, numAffected){ console.log(err); console.log('/////////// err callback //////////'); console.log(numAffected); }); – Davet Aug 22 '16 at 10:52
1

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.

Community
  • 1
  • 1
Ram Rajamony
  • 1,717
  • 15
  • 17
  • This is helpful too and is a potential solution. I accepted the above response because it pinpointed the exact method. Otherwise, you're right, the middleware has a lot of possibilities on handling situations neatly. I am going to play around with `required` to night. – cathy.sasaki May 13 '13 at 01:49
  • @cathy.sasaki: np! Of late, I've found the combination of Angular, Monk, and Q to be very useful giving me exactly the CRUD and easy-to-manage UI that I want. – Ram Rajamony May 13 '13 at 14:55
  • Neato! I just looked over Angular by Google. I'd heard people talking about it but was busy with node/backbone, and so on. Yes, I need to put that on my learning schedule and use it. Thanks for the pointers to this frameworks/modules! – cathy.sasaki May 13 '13 at 18:25