Skip to content

Commit

Permalink
docs; clarify #3597
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 19, 2015
1 parent b4d915f commit 1f67c74
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions docs/faq.jade
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ block content
doc.array[3] = 'changed';
doc.markModified('array');
doc.save();
hr#assign_schemas_to_paths
:markdown
**Q**. Why doesn't mongoose allow me to directly assign schemas to paths?
:js
var userSchema = new Schema({ name: String });
new Schema({ user: userSchema })
:markdown
**A**. Schemas have a one-to-one mapping with documents. Documents have `save` and `remove` methods along with their own `pre` and `post` hooks which would lead to code like the following:
:js
doc.user.save(); // ?
doc.user.remove();// ?
doc.save()
:markdown
We've felt that this api would be more confusing than helpful. The counter argument is that arrays of sub-documents already have this functionality, but at best this too leads to confusion (calling `save` on a sub-document is a no-op and exists only to support `pre` save hooks). In the future this is likely to be revisited.
hr#enable_debugging
:markdown
**Q**. How can I enable debugging?
Expand Down Expand Up @@ -66,14 +52,19 @@ block content
**Q**. Should I create/destroy a new connection for each database operation?

**A**. No. Open your connection when your application starts up and leave it open until the application shuts down.
hr#overwritemodelerror
hr#overwrite-model-error
:markdown
**Q**. Why do I get "OverwriteModelError: Cannot overwrite .. model once compiled" when I use nodemon / a testing framework?
It always works when the code loads the first time, but then throws this error from the second load onwards.
**Q**. Why do I get "OverwriteModelError: Cannot overwrite .. model once
compiled" when I use nodemon / a testing framework?

**A**. Use `connection.model(..)` instead of `mongoose.model(..)` to create your models.
See [this issue](https://github.com/Automattic/mongoose/issues/1251#issuecomment-11697477) and below:
"it works b/c a new connection is created for each of your tests and the model is cached locally within the connection for each. the other way fails b/c models were getting compiled at the mongoose module level for each test, hence the conflicts."
**A**. `mongoose.model('ModelName', schema)` requires 'ModelName' to be
unique, so you can access the model by using `mongoose.model('ModelName')`.
If you put `mongoose.model('ModelName', schema);` in a
[mocha `beforeEach()` hook](https://mochajs.org/#hooks), this code will
attempt to create a new model named 'ModelName' before **every** test,
and so you will get an error. Make sure you only create a new model with
a given name **once**. If you need to create multiple models with the
same name, create a new connection and bind the model to the connection.
:js
var mongoose = require('mongoose');
var connection = mongoose.createConnection(..);
Expand All @@ -90,4 +81,3 @@ block content

If you'd like to contribute to this page, please [visit it](https://github.com/Automattic/mongoose/tree/master/docs/faq.jade) on github and use the [Edit](https://github.com/blog/844-forking-with-the-edit-button) button to send a pull request.
br

0 comments on commit 1f67c74

Please sign in to comment.