Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.88k stars 3.83k forks source link

update from version 6.11.6 to 7.4.0 there is a problem with the query #14770

Closed DerickBastos closed 1 week ago

DerickBastos commented 1 month ago

Prerequisites

Mongoose version

6.11.6 to 7.4.0

Node.js version

16.17.0

MongoDB server version

7.0.12

Typescript version (if applicable)

No response

Description

After upgrading to Mongoose version 7.4.0, I noticed that the query is no longer returning the same results as it did in version 6.11.6. I found that the query was incomplete, with the $in clause being removed

Steps to Reproduce

version mongoose 6.11.6:

const data = await this.model.find({
  $and: [
    {
      $or: [
        { channels: { $exists: false } },
        { channels: { $size: 0 } },
        { channels: { $elemMatch: { $in: ["5dc1839b88b96c0017de66a0"] } } },
      ],
    },
    {
      [`wabaResult.5dc1839b88b96c0017de66a0.status`]: { $eq: "approved" },
    },
  ],
}).getFilter();

/** on version 6.11.6 elemMatch is correct
{
  $and: [
    {
      $or: [
        { channels: { $exists: false } },
        { channels: { $size: 0 } },
        { channels: { $elemMatch: { $in: ["5dc1839b88b96c0017de66a0"] } } },
      ],
    },
    {
      "wabaResult.5dc1839b88b96c0017de66a0.status": { $eq: "approved" },
    },
  ],
}*/

version mongoose 7.4.0:

const data = await this.model.find({
  $and: [
    {
      $or: [
        { channels: { $exists: false } },
        { channels: { $size: 0 } },
        { channels: { $elemMatch: { $in: ["5dc1839b88b96c0017de66a0"] } } },
      ],
    },
    {
      [`wabaResult.5dc1839b88b96c0017de66a0.status`]: { $eq: "approved" },
    },
  ],
}).getFilter();

/** on version 7.4.0 elemMatch is incorrect
{
  $and: [
    {
      $or: [
        { channels: { $exists: false } },
        { channels: { $size: 0 } },
        { channels: { $elemMatch: { } } },
      ],
    },
    {
      "wabaResult.5dc1839b88b96c0017de66a0.status": { $eq: "approved" },
    },
  ],
}*/

Expected Behavior

The expected behavior is that Mongoose does not remove parts of my query, as this is affecting the expected results.

IslandRhythms commented 1 month ago

Tested on 6.11.6, 7.4.0, and 8.5.2 and am unable to reproduce.

const mongoose = require('mongoose');

const channelSchema = new mongoose.Schema({
    name: String
});

const testSchema = new mongoose.Schema({
    channels: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Channel'}]
});

const Test = mongoose.model('Test', testSchema);
const Channel = mongoose.model('Channel', channelSchema);

async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();

    const channelOne = await Channel.create({
        name: 'TestOne'
    });

    const channelTwo = await Channel.create({ name: 'TestTwo' });

    await Test.create({
        channels: [channelOne._id, channelTwo._id]
    });

    const res = await Test.find({
        $and: [{
            $or: [
                { channels: { $exists: false } },
                { channels: { $size: 0 } },
                { channels: { $elemMatch: { $in: [channelOne._id] } } }
            ]
        }]
    }).getFilter();

    console.log('what is res', res, res.$and[0].$or, res.$and[0].$or[2].channels.$elemMatch);
    console.log('done');
    process.exit(0)
}

run();
vkarpov15 commented 1 month ago

What does your schema look like @DerickBastos ?

github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] commented 1 week ago

This issue was closed because it has been inactive for 19 days and has been marked as stale.