-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change collection.drop() to collection.deleteMany() #39
Conversation
The `collection.drop()` method completely erases a collection from the database, including its indexes. It's better to only remove the documents but keep the indexes, since the user might use them. The method in mongodb-native that allows just that is [`collection.deleteMany()`](http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#deleteMany)
I ran into test failures because of mixing indexes, so I agree something needs to be done to make sure that indexes continue to work after the fixture data is loaded. |
this is really useful, it should be merged |
What's an example of a situation where you want the indexes from a previous test? The indexes would be referring to documents that are no longer there, and in my experience for tests you want as clean a slate as possible each time. |
You could check this issue #37 . In my practical unit test usage, when then indexes is droped also, its very easily to meet this error btw. Using deleteMany also speed up my unit test a lot.
|
OK, however I think there must be another way of fixing the issue #37 without keeping the indexes around. One problem with not removing the indexes for example is if you are testing user account creation and you have a unique index on user.email - next time you run the tests you will get a duplicate key index error. |
Mongo should be updating the index properly when documents are deleted, so the indexes should remain correct. Code reasonably expects indexes to be present, not to manually create there. Without indexes, some operations will fail like some geo functions that require an index. This package is going to drop indexes, it also needs restore them again. |
My use case is a test suite where a field in the collection is expected to
be unique. This is ensured by MongoDB through indexes, and the tests
asserts the code behavior whenever MongoDB raise an error on insertion of a
non-unique field. Each test starts with a `clearAllAndLoad()` call, so the
Database status is the same for every test. If the indexes are reset, the
test which expects the MongoDB error will fail.
Bernardo Figuerêdo Domingues, Ph.D.
…On Tue, Jun 27, 2017 at 10:53 AM, Mark Stosberg ***@***.***> wrote:
What's an example of a situation where you want the indexes from a
previous test? The indexes would be referring to documents that are no
longer there, and in my experience for tests you want as clean a slate as
possible each time.
Mongo should be updating the index properly when documents are deleted, so
the indexes should remain correct.
Code reasonably expects indexes to be present, not to manually create
there. Without indexes, some operations will fail like some geo functions
that require an index.
This package is going to drop indexes, it also needs restore them again.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#39 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACQZavqFRpZ81kMhQgSNKOJHOUPkSvBbks5sIQlbgaJpZM4L1kef>
.
|
Ah of course, OK will merge this PR |
The
collection.drop()
method completely erases a collection from the database, including its indexes. It's better to only remove the documents but keep the indexes, since the user might use them in their tests. The method in mongodb-native v2.0 that allows just that iscollection.deleteMany()
. This is a correction of PR #31, since there is noremoveMany()
method on the collection API