-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
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
added
the
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
label
Sep 19, 2023
vkarpov15
added a commit
that referenced
this issue
Sep 25, 2023
IslandRhythms
added a commit
that referenced
this issue
Sep 25, 2023
…ttic/mongoose into IslandRhythms/gh-13880.js
vkarpov15
added a commit
that referenced
this issue
Sep 25, 2023
fix: handle casting `$elemMatch` underneath `$not` underneath another `$elemMatch`
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
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').
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:The full output is really long, but the first lines are:
Expected Behavior
Using the
mongoose
find
should return the same results than themodel.collection
when using the same query.The text was updated successfully, but these errors were encountered: