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
Unique-Indexes seems not working. #102
Comments
Publish your schema and create commands please |
My code: const CompoundUnique = new mongoose.Schema({
type:{type: String},
name:{type: String},
});
CompoundUnique.index({
type:1,
name:1,
},{unique: true});
const CompoundU = mongoose.model('CompoundUnique',CompoundUnique);
let doc1 = await CompoundU.create({name:"bob",type:"human"});
let doc2 = await CompoundU.create({name:"bob",type:"human"}); Console:
Looks correct. |
I think that problem somewhere in mongoose. How i know in the latests versions need to call explicitly index creation. But maybe i'm wrong. Anyway this package just downloads binaries from mongodb distro and starts mongo instances. It cannot affect on index creation. PS. Try to use another engine (wiredtiger), maybe some problems with mongod engine |
I am running into the same issue and tried all engines:
Everything worked fine as long as i only used one instance of mongodbMemoryServer, after creating a new test with it's own instance the issue occurred. The first test is working fine, but the second has the duplicate entries, so it also has something to do with the order of the tests. |
I think this is somehow a timing issue or missing await? i disabled the auto start feature, because i think the auto start does not wait for the binary code
i also disabled the "useCreateIndex" feature of the mongo client to get deprecation warnings
TL;DR:
Here are the debug logs of my unit tests, i tried to mark some findings with "-->"
|
I managed to work around this issue by calling:
in the mocha before function of the test |
I encountered the same problem. Solution from @eiabea with But it's strange, I don't understand why for testing we need to put |
Same problem here. When I run my application works fine but when I run my tests, this fails because mongoose cannot create index :( And another question. I am populating my database to run my test. When I try to
Does anyone experience the same issue? 🤔 |
@jesusvallez and all others: Maybe this information will help, even though it doesn't have to do directly with unique-indexes but indexes in general. Info:
Testconst data ={ /* ... */ };
describe("Test Mongoose model billing", () => {
it("create & save successfully", async done => {
const entry = await new MyModel.(data).save();
// ... rest but I'm not getting further than this anyway.
});
}); Scenario 1 - all default in regards of ensureIndexes TypeError: Cannot read property 'Decimal128' of null
at Object.<anonymous> (node_modules/mongoose/lib/types/decimal128.js:13:44)
at Object.<anonymous> (node_modules/mongoose/lib/utils.js:7:17)
at Object.<anonymous> (node_modules/mongoose/lib/statemachine.js:8:15)
at Object.<anonymous> (node_modules/mongoose/lib/internal.js:7:22) Scenario 2 - useCreateIndex as mongoose configuseCreateIndex: false,
// or
useCreateIndex: true, Result both in output of scenario 1. Scenario 2 - ensureIndexes as suggested by @pciazynskiAdded right before the await MyModel.ensureIndexes(); which results than in : Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: Note: The timeout duration doesn't matter here. Same result with 60000ms. EDIT: Tried different engines as well. |
I can confirm this issue with This should be part of the documentation! |
I just tried to update the docs, but I'm not certain what to document exactly. Is this a specific issue in combination with Mongoose, or is it something that will happen with all MongoDB clients? Can someone please describe the workaround and the reason why it is necessary in one or two concise sentences? |
from what i can tell, this isnt an mongodb or mongodb-memory-server issue, because all examples use mongoose, and for mongoose it is known to call either judging from this, i will close this issue |
I have understood that this is not a mongodb-memory-server issue. But it's something a lot of people will trip over when working with mongodb-memory-server. So IMHO it would be valuable to put a note into the docs that at least points to this discussion thread. Even better would be a short explanation why this is happening and how to fix it. |
Note that even after you call |
Hi,
i have this schema-index:
type and name is a combined index. This works live, but in the test i can add two documents with the same type and the same name.
I am using
binary: {version: '4.0.3' }
on both sides.Are unique indexes are supported, yet?
The text was updated successfully, but these errors were encountered: