Skip to content
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

Error: cyclic dependency detected #6109

Closed
astrotars opened this issue Feb 8, 2018 · 40 comments
Closed

Error: cyclic dependency detected #6109

astrotars opened this issue Feb 8, 2018 · 40 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core
Milestone

Comments

@astrotars
Copy link

astrotars commented Feb 8, 2018

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)
@h2non
Copy link

h2non commented Feb 9, 2018

What was the original issue here?

@RahulSaini202
Copy link

Try to include the field which needs to be updated instead of whole object.

@Rowansdabomb
Copy link

I too have this issue, same line of code.
C:\Users\Rowan\Onedrive\code\react\gradeable\frontend\node_modules\mongoose\lib\utils.js:417
throw err;

@laino
Copy link

laino commented Feb 21, 2018

@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?

@CuReSoft
Copy link

Same here and absolutely no way to track down.

The Error comes and goes.

image

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)

@mrfoh
Copy link

mrfoh commented Mar 1, 2018

Currently experiencing the same issue. Any idea how to resolve this.

@Automattic Automattic locked as spam and limited conversation to collaborators Apr 23, 2018
@vkarpov15
Copy link
Collaborator

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.

@Automattic Automattic unlocked this conversation May 2, 2018
@gustvao
Copy link

gustvao commented May 2, 2018

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

@AndrewBarba
Copy link

AndrewBarba commented May 2, 2018

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

@gustvao
Copy link

gustvao commented May 2, 2018

hey @AndrewBarba .. try adding the index like I mentioned, not on the field but on the schema and see if it works

@AndrewBarba
Copy link

@gustvao I actually tried that first, no luck :(

@damianthekreeeytor
Copy link

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

@onkar0777
Copy link

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

@AndrewBarba
Copy link

Might be worth noting, I was using the mongodb+srv:// uri structure with a few options:

mongodb+srv://user:password@host.mongodb.net?readPreference=primaryPreferred&retryWrites=true

Once I removed the uri options the issue went away without any other code changes:

mongodb+srv://user:password@host.mongodb.net

@leobudima
Copy link

Can confirm - removing retryWrites made the issue go away, even with the "old" mongodb:// URI.

@thaoula
Copy link

thaoula commented May 21, 2018

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
#6469 (then closed it .. thought it was resolved)

Regards,
Tarek

@yogeshbansal
Copy link

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

@vkarpov15 vkarpov15 reopened this May 21, 2018
@vkarpov15 vkarpov15 added this to the 5.1.3 milestone May 21, 2018
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label May 21, 2018
@vkarpov15
Copy link
Collaborator

Will investigate and check whether turning on retryWrites causes this crash.

@pauliusuza
Copy link

pauliusuza commented May 23, 2018

I can confirm that removing &retryWrites=true from the connection URI fixes the error

@jamesagwa
Copy link

jamesagwa commented May 25, 2018

Confirmed! removing &retryWrites=true trully fixes the error

@mykhaliuk
Copy link

Works on Atlas: MongoDB cluster without ?retryWrites=true

@vkarpov15 vkarpov15 modified the milestones: 5.1.3, 5.1.4 May 28, 2018
@vkarpov15 vkarpov15 modified the milestones: 5.1.5, 5.1.6 Jun 11, 2018
@shian15810
Copy link

shian15810 commented Jun 17, 2018

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.

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jun 18, 2018
@vkarpov15
Copy link
Collaborator

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

@gustvao
Copy link

gustvao commented Jun 18, 2018

nice @vkarpov15

@tabuckner
Copy link

@vkarpov15 💪

@otisidev
Copy link

I fixed mine by removing retryWrites querystring from the connection string .

before
mongodb://sa:@cluster0-shard-00-00-ljpyb.mongodb.net:27017,cluster0-shard-00-01-ljpyb.mongodb.net:27017,cluster0-shard-00-02-ljpyb.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true

after
mongodb://sa:@cluster0-shard-00-00-ljpyb.mongodb.net:27017,cluster0-shard-00-01-ljpyb.mongodb.net:27017,cluster0-shard-00-02-ljpyb.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin

hope this helps.

@vkarpov15 vkarpov15 modified the milestones: 5.1.6, 5.1.7 Jun 19, 2018
@vkarpov15
Copy link
Collaborator

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.

@AndrewBarba
Copy link

@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

@AndrewBarba
Copy link

AndrewBarba commented Jun 19, 2018

@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: 'mongoose@test.com'
    })

    console.log(await Model.findOne())
  } catch(err) {
    console.error(err)
  }
}

run()

@tschellenbach
Copy link

Setting an index field to:

index: { type: 'hashed', unique: true }

Also always triggers this issue

@karthikiyengar
Copy link

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 #6469 - a few more repro steps.

@vkarpov15 vkarpov15 added the underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core label Jun 20, 2018
@otisidev
Copy link

otisidev commented Jun 20, 2018

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.

@vkarpov15
Copy link
Collaborator

@AndrewBarba thanks for the repro, I found another issue like node-mongodb-native#1740, see mongodb/node-mongodb-native#1758 . This fix will end up in mongoose once a new version of the mongodb driver is released.

@vkarpov15 vkarpov15 modified the milestones: 5.1.7, 5.1.8 Jun 26, 2018
@dannysofftie
Copy link

dannysofftie commented Jul 1, 2018

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.

@vkarpov15
Copy link
Collaborator

Should be fixed in 5.1.8

@danieler1981
Copy link

I got some issue with MongoDB Atlas and GridStore, write operation was not working until i removed "retryWrites=true".

@Automattic Automattic locked as resolved and limited conversation to collaborators Jul 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core
Projects
None yet
Development

No branches or pull requests