"explain" cannot be used on an aggregate call with writeConcern

We are using the explain option when writing aggregations, to debug slow queries etc.

However, when we run the queries against one of our environments (stage), we get this error:

"explain" cannot be used on an aggregate call with writeConcern

Where can I check in atlas what this means / fix it?

Thanks for your help.

Hi @Alex_Bjorlig ,

How do you run this explain?

If you use mongo shell you should do:

db.collection.explain().aggregate ( ... );


Hi @Pavel_Duchovny .

We run the explain through the Node.js driver.

Works without issues in dev and production environment, but in our stage environment we get the described error?

What could the explanation for this be?

Hi @Alex_Bjorlig ,

Can you share the code and the properties of the connection string? Maybe due to your writeConcern in stage it can’t be performed this way…


Ahh that might be the case actually!

For refrence here is my dev connection string:

and stage:

So I should probably just remove the retryWrites and w=majority.

Do you have any good beginner reference to documentation/videos to understand the above properties?

@Alex_Bjorlig ,

Yes those are related to the write consistency and retry mechanisim:

We actually recommend those settings as part of the connection string but maybe you can adjust the write concern specifically for the aggregatie with explain :

There is a writeConcern option as part of the aggregate maybe use {w : 1} for it :


Thanks - I will read the provided links to better understand the options. Maybe the error message for aggregation explain could be improved to indicate the above considerations?

Well the error does say its writeConcern related :

“explain” cannot be used on an aggregate call with writeConcern

Yeah ok - that’s correct :relaxed: