Running the latest version of Node.js on macOS. Also running the latest version of Mongoose.
Seeing Error: cyclic dependency detected
with literally no way to debug. Everything points back to Mongoose. Any suggestions? Started happening today.
/Users/nickparsons/Code/winds-2.0/api/src/node_modules/mongoose/lib/utils.js:417
throw err;
^
Error: cyclic dependency detected
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:296:33)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:618:17)
at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
What was the original issue here?
Try to include the field which needs to be updated instead of whole object.
I too have this issue, same line of code.
C:\Users\Rowan\Onedrive\code\react\gradeable\frontendnode_modules\mongoose\lib\utils.js:417
throw err;
@nparsons08 Ping. Can you tell us whether it did turn out to be an issue on your end, you found a workaround (besides downgrading to an earlier mongoose version), or you resolved it in another way?
Same here and absolutely no way to track down.
The Error comes and goes.
Error: cyclic dependency detected
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:296:33)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:618:17)
at serializeObject (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at BSON.serialize (/var/www/node_modules/mongoose/node_modules/bson/lib/bson/bson.js:58:27)
at Query.toBin (/var/www/node_modules/mongoose/node_modules/mongodb-core/lib/connection/commands.js:141:25)
at serializeCommands (/var/www/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:1050:43)
at Pool.write (/var/www/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:1230:3)
at executeWrite (/var/www/node_modules/mongoose/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:80:10)
at WireProtocol.insert (/var/www/node_modules/mongoose/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:91:3)
at Server.insert (/var/www/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:747:35)
at executeWriteOperation (/var/www/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/mongos.js:899:22)
at Mongos.insert (/var/www/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/mongos.js:955:3)
at Mongos.insert (/var/www/node_modules/mongoose/node_modules/mongodb/lib/topologies/topology_base.js:315:25)
at /var/www/node_modules/mongoose/node_modules/mongodb/lib/db.js:1093:21
at handleCallback (/var/www/node_modules/mongoose/node_modules/mongodb/lib/utils.js:128:55)
Currently experiencing the same issue. Any idea how to resolve this.
Locking this because "Currently experiencing the same issue" is too vague to be actionable. Please open up a new issue and follow the issue template.
guys, I guess I found the cause - apparently it is related to mongodb 3.6 (I recently upgraded from 3.2 to 3.6 and mongoose got upgraded to 5.x as well)...
so I was not being able to run my app anymore, I was getting these cyclic dependency errors... the only work-around that worked for me was to add autoIndex: false
into mongoose.connect
- but doing so my indexes were not being created anymore even after using ensureIndex()
later on the application - it would eventually result in the same cyclic dependency errors..
long story short I managed to fix it by removing all attributes INDEX from inside my models and appending them to the model afterwards, like this:
before
client: { type: ObjectId, ref: 'tsCli', required: true, index: true },
after
client: { type: ObjectId, ref: 'tsCli', required: true },
[...]
Schema.index({ client: 1 });
AND, more importantly, removing attribute INDEX from compound indexes, i.e.:
before
Schema.index({ client: 1, code: 1 }, { unique: true, index: true, partialFilterExpression: { code: { $type: String } } });
after
Schema.index({ client: 1, code: 1 }, { unique: true, partialFilterExpression: { code: { $type: String } } });
hope this helps others
cheers
I'm specifically encountering this issue when attempting to add index on an array:
...
tags: {
type: [String],
index: true
}
...
Simply adding that prop/index to my schema is causing this issue
hey @AndrewBarba .. try adding the index like I mentioned, not on the field but on the schema and see if it works
@gustvao I actually tried that first, no luck :(
@AndrewBarba I had this same issue. Using MongoClient I had to use :
Collection.createIndexes([
{
key: { email: 1 },
name: 'user_email',
unique: true
}
])
Hope this helps.
Happens to me when trying to add a text index:
/Users/ashish/dictionary/js_server/node_modules/mongoose/lib/utils.js:429
throw err;
^
Error: cyclic dependency detected
at serializeObject (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:331:34)
at serializeInto (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/ashish/dictionary/js_server/node_modules/bson/lib/bson/parser/serializer.js:934:17)
Emitted 'error' event at:
at done (/Users/ashish/dictionary/js_server/node_modules/mongoose/lib/model.js:1051:13)
at /Users/ashish/dictionary/js_server/node_modules/mongoose/lib/model.js:1093:16
at /Users/ashish/dictionary/js_server/node_modules/mongoose/lib/utils.js:424:16
at err (/Users/ashish/dictionary/js_server/node_modules/mongodb/lib/utils.js:415:14)
at session.endSession (/Users/ashish/dictionary/js_server/node_modules/mongodb/lib/utils.js:399:27)
at ClientSession.endSession (/Users/ashish/dictionary/js_server/node_modules/mongodb-core/lib/sessions.js:72:41)
at executeCallback (/Users/ashish/dictionary/js_server/node_modules/mongodb/lib/utils.js:397:17)
at err (/Users/ashish/dictionary/js_server/node_modules/mongodb/lib/utils.js:415:14)
at executeCallback (/Users/ashish/dictionary/js_server/node_modules/mongodb/lib/utils.js:404:25)
at err (/Users/ashish/dictionary/js_server/node_modules/mongodb/lib/utils.js:415:14)
at executeCallback (/Users/ashish/dictionary/js_server/node_modules/mongodb/lib/utils.js:404:25)
at handleCallback (/Users/ashish/dictionary/js_server/node_modules/mongodb/lib/utils.js:128:55)
at /Users/ashish/dictionary/js_server/node_modules/mongodb/lib/db.js:1068:25
at executeWrite (/Users/ashish/dictionary/js_server/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:82:5)
at WireProtocol.insert (/Users/ashish/dictionary/js_server/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:91:3)
at Server.insert (/Users/ashish/dictionary/js_server/node_modules/mongodb-core/lib/topologies/server.js:752:35)
Creating a text index on my field gives this error. Code:
WordSchema = new Schema({
title: {
type: String,
required: true,
},
meanings: [MeaningSchema],
keys: [String],
tags: [String],
language: String,
createdAt: Date,
updatedAt: Date
});
WordSchema.index({title: 'text'}); ---> Error
WordSchema.index({title: 1}); ---> Works fine
node - 9.11.1
mongoose - 5.1.1 (same with 5.0.8)
MongoDB - 3.6.2
Might be worth noting, I was using the mongodb+srv://
uri structure with a few options:
mongodb+srv://user:[email protected]?readPreference=primaryPreferred&retryWrites=true
Once I removed the uri options the issue went away without any other code changes:
mongodb+srv://user:[email protected]
Can confirm - removing retryWrites
made the issue go away, even with the "old" mongodb://
URI.
Hi @vkarpov15,
What is the impact of removing retryWrites for Atlas, is this something that mongoose is doing internally?
I open this issue about retryWrites
https://github.com/Automattic/mongoose/issues/6469 (then closed it .. thought it was resolved)
Regards,
Tarek
Hi
I am also getting the this error after updated to latest mongoose version. I am not creating any index in any of my schemas. Now my application is so big, I am not able to find out, what code is causing this issue.On mongoose ^4.13.1 is working fine.
/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/lib/utils.js:429
throw err;
^
Error: cyclic dependency detected
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:331:34)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:724:17)
at serializeObject (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:345:18)
at serializeInto (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/parser/serializer.js:934:17)
at BSON.serialize (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/bson/lib/bson/bson.js:63:28)
at Query.toBin (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/mongodb-core/lib/connection/commands.js:144:25)
at serializeCommands (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:1053:43)
at Pool.write (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:1269:3)
at executeWrite (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:80:10)
at WireProtocol.insert (/Users/yogesh_bansal/workzone/Projects/apiserver/node_modules/mongoose/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:91:3)
[nodemon] app crashed - waiting for file changes before starting...
Will investigate and check whether turning on retryWrites
causes this crash.
I can confirm that removing &retryWrites=true
from the connection URI fixes the error
Confirmed! removing &retryWrites=true
trully fixes the error
Works on Atlas: MongoDB cluster without ?retryWrites=true
Below script works fine for me both locally and against Atlas. Please modify the below script to reproduce this issue:
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect('mongodb+srv://user:[email protected]/test?retryWrites=true');
const M = mongoose.model('Test', new Schema({ name: String }));
await M.create({ name: 'test' });
console.log(await M.findOne());
}
@vkarpov15
Please modify the below script to reproduce this issue:
```const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect('mongodb+srv://user:[email protected]/test?retryWrites=true', {dbName:'db-name'});
const M = mongoose.model('Test', new Schema({ name: {type: String, unique: true }}));
await M.create({ name: 'test' });
console.log(await M.findOne());
}
```
With this code from @mykhaliuk :
const mongoose = require('mongoose');
mongoose.set('debug', true);
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect('mongodb+srv://user:[email protected]/test?retryWrites=true', {dbName:'db-name'});
const M = mongoose.model('Test', new Schema({ name: {type: String, unique: true }}));
await M.create({ name: 'test' });
console.log(await M.findOne());
}
I got the following error: Error: cyclic dependency detected
The only way I found to maintain the retryWrites
is using the function createIndexes
from mongodb API:
const mongoose = require('mongoose');
mongoose.set('debug', true);
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect('mongodb+srv://user:[email protected]/test?retryWrites=true');
const M = mongoose.model('NewTest', new Schema({ name: {type: String} }));
M.collection.createIndexes([{key: {name: 1}, name: "name", unique: true}])
await M.create({ name: 'test' });
console.log(await M.findOne());
}
So we are removing unique
, index
from Mongoose model definitions and creating all indexes using createIndexes (https://docs.mongodb.com/manual/reference/command/createIndexes/)
I ran into this issue as well updating from Mongoose 4.11.7 to 5.1.4. I tried removing ?retryWrites=true
from my connection URI, no luck. Finally I figured out I was using index:true
on a custom _id
property like this:
_id: {
type: String,
required: true,
index: true
}
Removing index:true
fixed the issue for me. Also found this issue which mentions the solution : https://github.com/Automattic/mongoose/issues/2303. Just posting incase someone else runs into the same issue.
For me, I ran into this issue when having retryWrites=true
in my connection string and Schema.index({ someKey: 1 })
in my model.
In mongo shell, executing db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
returned { "featureCompatibilityVersion" : { "version" : "3.6" } }
.
Then I downgraded the featureCompatibilityVersion
to 3.4
of mongodb by executing db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
in mongo shell and ran my application again with retryWrites=true
and Schema.index({ someKey: 1 })
as mentioned above.
After ensuring that the index had been successfully created, I upgraded the featureCompatibilityVersion
to 3.6
of mongodb by executing db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
, and then ran my application again. The cyclic dependency error didn't appear anymore.
Found the issue. It has nothing to do with Atlas, or retryWrites
, or featureCompatibilityVersion
. It has everything to do with trying to create an index on _id
. Minimal repro script:
const mongoose = require('mongoose');
mongoose.set('debug', true);
Error.stackTraceLimit = Infinity;
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', {dbName: 'test'});
const M = mongoose.model('Test', new Schema({ _id: { type: String, index: true } }));
await M.create({ _id: 'abc' + Math.random() });
console.log(await M.findOne());
}
Will open up a PR in the mongodb driver for this
nice @vkarpov15
@vkarpov15 馃挭
I fixed mine by removing retryWrites querystring from the connection string .
before
mongodb://sa:
after
mongodb://sa:
hope this helps.
Try removing any indexes you have on _id
@otisidev . So if you have something like new Schema({ _id: {type: String, index: true} })
or schema.index({ _id: 1 })
please remove that.
@vkarpov15 I think there may be two issues here. I don't have any indexes on _id
anywhere in my project and was still seeing this error until I removed ?retryWrites
from the URI. I will make a project that shows that. Regardless, this really seems like an issue with the mongodb driver and not Mongoose
@vkarpov15 Here is a script that produces this issue without any index on _id
:
Mongoose: v5.1.5
Node: v8.11.3
const mongoose = require('mongoose')
const { Schema } = mongoose
async function run() {
try {
await mongoose.connect('mongodb://localhost:27017/test?retryWrites=true', {
dbName: 'mongoose-issue-6109'
})
const Model = mongoose.model('Model', new Schema({
email: {
type: String,
index: {
unique: true
}
}
}))
await Model.create({
email: '[email protected]'
})
console.log(await Model.findOne())
} catch(err) {
console.error(err)
}
}
run()
Setting an index field to:
index: { type: 'hashed', unique: true }
Also always triggers this issue
Yeah, this seems to be a legit issue - I've scanned my codebase for bad indexes and it still seems to reoccur. Downgraded to 5.1.0
and it seems to work fine.
Links to https://github.com/Automattic/mongoose/issues/6469 - a few more repro steps.
I've seen the bug.
_email_: {
type: String,
required: true,
_unique: true_
},
.......
credentialSchema.index({ _email_: 1, domain: 1, ipAddress: 1, password: -1 });
i had to remove unique from the model definition.
@AndrewBarba thanks for the repro, I found another issue like node-mongodb-native#1740, see https://github.com/mongodb/node-mongodb-native/pull/1758 . This fix will end up in mongoose once a new version of the mongodb driver is released.
I encountered the same issue when using atlas, but everything was fine locally. A quick fix around it, as someone suggested is removing the parameter ?retryWrites=true
, and now things are fine.
Should be fixed in 5.1.8
I got some issue with MongoDB Atlas and GridStore, write operation was not working until i removed "retryWrites=true".
Most helpful comment
Can confirm - removing
retryWrites
made the issue go away, even with the "old"mongodb://
URI.