Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does `mongoose` handle adding documents that have FIELDS that are __NOT__ part of the schema?

Tags:

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.

like image 798
cathy.sasaki Avatar asked May 12 '13 16:05

cathy.sasaki


People also ask

Why does Mongoose need a schema?

Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

How does Mongoose know which collection?

Mongoose uses the model name, as passed when it was created: mongoose. model("User", UserSchema) , converted to lower case and with an 's' appended. For the model User it uses the collection users by default. You can change this by explicitly specifying the collection name in the schema.

What is Mongoose schema explain with example?

A schema in Mongoose maps to a MongoDB collection and defines the format for all documents on that collection. All properties inside the schema must have an assigned SchemaType . For example, the name of our Person can be defined this way: const PersonSchema = new mongoose. Schema({ name: { type: String}, });


1 Answers

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

like image 173
Matthew Bakaitis Avatar answered Sep 19 '22 13:09

Matthew Bakaitis