question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Find on nested discriminators is broken after upgrade from mongoose 5 to mongoose 6

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.5.2

Node.js version

16.15.0

MongoDB server version

4.4

Description

We are currently migrating from mongoose 5 to mongoose 6, and it seems like nested discriminators are broken with mongoose 6. When we find by a nested discriminated field, the query will return all documents instead of matching ones. This has created quite a few bugs for us while testing our code with mongoose 6, where completely unrelated data is being returned from queries. The culprit seems to be the new strictQuery option, and setting it to false patches the issue. However, having to set this option for nested fields seems like a broken behavior to use.

Steps to Reproduce

The following script shows the error. The find call should return only the second document. However, both are returned.

import mongoose from 'mongoose';

const BaseDocumentSourceSchema = new mongoose.Schema({
  source: { type: String },
}, {
  discriminatorKey: 'type'
});

const InternalDocumentSchema = new mongoose.Schema({
  source: { type: String },
});

const ExternalDocumentSchema = new mongoose.Schema({
  source: { type: String },
  url: { type: String },
});

const DocumentSchema = new mongoose.Schema({
  source: BaseDocumentSourceSchema,
});
DocumentSchema.path('source').discriminator('internal', InternalDocumentSchema);
DocumentSchema.path('source').discriminator('external', ExternalDocumentSchema);

const DocumentModel = mongoose.model('Document', DocumentSchema);

(async () => {
  await mongoose.connect(`mongodb://localTestUser:HhPQ5rEUvbDgqrkE3c@localhost:27017/frankhr_test`);

  await mongoose.connection.db.dropDatabase();

  const doc1 = await DocumentModel.create({ source: { type: 'internal' } });
  const doc2 = await DocumentModel.create({ source: { type: 'external', url: 'url' } });

  console.log(doc1, doc2);

  // Expected result is document 2. However, it will return both documents.
  const results = await DocumentModel.find({ 'source.url': 'url' });
  console.log(results);

  // Query correctly returns document 2
  const strictResults = await DocumentModel.find({ 'source.url': 'url' }, undefined, { strictQuery: false });
  console.log(strictResults);

  await mongoose.disconnect();
})();

Expected Behavior

Queries using nested discriminated fields should return the correct data.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Sep 8, 2022

The original issue is a problem. We’re planning on changing strictQuery’s default value back to false in 7.0: https://github.com/Automattic/mongoose/issues/11861 , which will fix the original issue. I’m sorry for the inconvenience, the strictQuery change in 6.0.0 turned out to be a bad design decision in hindsight.

0reactions
fschuchtcommented, Sep 8, 2022

@vkarpov15 I can confirm that your script works. It seems like I didn’t set strictQuery for the nested schemas as I assumed the parent options would also apply to the nested child schemas.

Setting strictQuery for the child schemas works and solves this issue: https://github.com/Automattic/mongoose/issues/12318#issuecomment-1228138014

However, the original issue still exists. When strictQuery is not set or enabled, nested discriminated fields don’t work anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating to Mongoose 6
Mongoose now clones discriminator schemas by default. This means you need to pass { clone: false } to discriminator() if you're using recursive...
Read more >
Mongoose `find` with discriminator behavior changed from 5.x ...
The behavior changed between mongoose 5.x and 6.x for a .find call with discriminators, and i could not see any related changes in...
Read more >
An 80/20 Guide to Mongoose Discriminators
Discriminators enable you to store documents with slightly different schemas in the same collection and query them back in a consistent way. In ......
Read more >
Updating nested object in mongoose - node.js - Stack Overflow
Everything works file and all console gives the desired result. But when I switch to mongoose console and query the document, I do...
Read more >
mongoose-autopopulate
Usage. The mongoose-autopopulate module exposes a single function that you can pass to Mongoose schema's plugin() function. const schema = new mongoose.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found