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

Querying array of arrays with $elemMatch and $not throws a CastError #13880

Closed
2 tasks done
Davian99 opened this issue Sep 19, 2023 · 1 comment · Fixed by #13893
Closed
2 tasks done

Querying array of arrays with $elemMatch and $not throws a CastError #13880

Davian99 opened this issue Sep 19, 2023 · 1 comment · Fixed by #13893
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Davian99
Copy link

Prerequisites

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

Mongoose version

7.5.2

Node.js version

18.2

MongoDB server version

6.0.5

Typescript version (if applicable)

No response

Description

When querying an array of arrays of strings field using a combination of $elemMatch and $not, mongoose's query casters throws an error.

The query is:
{ arr: { $elemMatch: {$not: {$elemMatch: {$eq: '1'}}} } }

Our intention is to obtain all documents that have one nested array that does not contain certain element (in this case, the string '1').

CastError: Cast to string failed for value "{ '$elemMatch': { '$eq': '1' } }" (type Object) at path "arr" for model "nested_arrays"
  at SchemaString.cast (/home/davids/Documents/MongooseType/node_modules/.pnpm/mongoose@7.5.2/node_modules/mongoose/lib/schema/string.js:606:11)

Steps to Reproduce

This short code shows that the query itself works using mongo itself (using the collection property of the model) and that mongoose throws the cast error:

const mongoose = require('mongoose');

const schema = new mongoose.Schema({
  arr: [[String]],
});

const model = mongoose.model('nested_arrays', schema);

mongoose.connect('mongodb://127.0.0.1:27017', { useNewUrlParser: true, useUnifiedTopology: true })
  .then(async () => {
    const obj = new model({ arr: [[1,2,3], [2,3,4]] })
    await obj.save();

    const query = { arr: { $elemMatch: {$not: {$elemMatch: {$eq: '1'}}} } };
    const mongo_res = await model.collection.find(query).toArray();
    console.log(mongo_res);

    const mongoose_res = await model.find(query);
    console.log(mongoose_res);
  });

The full output is really long, but the first lines are:

[
  {
    _id: new ObjectId("6509bfce3c3c67f9f8e099d6"),
    arr: [ [Array], [Array] ],
    __v: 0
  }
]
/home/davids/Documents/MongooseType/node_modules/.pnpm/mongoose@7.5.2/node_modules/mongoose/lib/schema/string.js:606
    throw new CastError('string', value, this.path, null, this);
          ^

CastError: Cast to string failed for value "{ '$elemMatch': { '$eq': '1' } }" (type Object) at path "arr" for model "nested_arrays"

Expected Behavior

Using the mongoose find should return the same results than the model.collection when using the same query.

@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const schema = new mongoose.Schema({
  arr: [[String]],
});

const test = mongoose.model('nested_arrays', schema);

async function run() {
  await mongoose.connect('mongodb://127.0.0.1:27017');
  await mongoose.connection.dropDatabase();
  const obj = new test({ arr: [[1,2,3], [2,3,4]] })
    await obj.save();

    const query = { arr: { $elemMatch: {$not: {$elemMatch: {$eq: '1'}}} } };
    const mongo_res = await test.collection.find(query).toArray();
    console.log(mongo_res);

    const mongoose_res = await test.find(query);
    console.log(mongoose_res);
}

run();

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 19, 2023
@vkarpov15 vkarpov15 added this to the 7.5.4 milestone Sep 20, 2023
vkarpov15 added a commit that referenced this issue Sep 25, 2023
vkarpov15 added a commit that referenced this issue Sep 25, 2023
fix: handle casting `$elemMatch` underneath `$not` underneath another `$elemMatch`
@vkarpov15 vkarpov15 modified the milestones: 7.5.4, 7.5.3 Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
3 participants