-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
"Cast to ObjectId failed" for custom ID and $elemMatch/$not #3719
Comments
Hmm that's strange, the below script executes without error for me against 4.3.4: var assert = require('assert');
var mongoose = require('mongoose');
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/gh3719');
var child = new mongoose.Schema({
_id: {type: String}
});
var parent = new mongoose.Schema({
children: [child]
});
var Parent = mongoose.model('Parent', parent);
Parent.create({ children: [{ _id: 'customString' }] }, function(error) {
assert.ifError(error);
test();
});
function test() {
Parent.find({
$and: [{
$or: [{
$and: [
{
myField: {
$not: {
$elemMatch: {
_id: 'customString'
}
}
}
}
]
}]
}]
}, function(error, docs) {
assert.ifError(error);
assert.ok(docs.length > 0);
process.exit(0);
});
}
Can you check if the above script works for you and confirm which version of mongoose you're using? |
Issue's gone stale. Re-open if this is still an issue. |
I'm able to reproduce this issue. Popped up after upgrading from 3.28.x to 4.6.0. Node v.6.5.0. Example: 'use strict';
var assert = require('assert');
var mongoose = require('mongoose');
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/gh3719');
var child = new mongoose.Schema({
_id: {type: String}
}, {_id: false});
var parent = new mongoose.Schema({
children: [child]
});
var Parent = mongoose.model('Parent', parent);
Parent.create({children: [{ _id: 'customString' }] }, function(error) {
assert.ifError(error);
test();
});
function test() {
Parent.find({
$and: [{children: {$not: {$elemMatch: {_id: 'foobar'}}}}]
}, function(error, docs) {
assert.ifError(error);
assert.ok(docs.length > 0);
process.exit();
});
} Yields:
The query works if the $and: [{children: {$elemMatch: {_id: 'foobar'}}}] |
Seems like possible fix could be changing line 206 of lib/cast.js from if ($cond === '$not') {
cast(schema, nested);
} else { to if ($cond === '$not') {
cast(schematype.caster.schema, nested);
} else { |
I think #4531 fixes this. |
Here is an example schema
The following query has been abbreviated to exclude the non-relevant info (my query is quite large). When $elemMatch is used without $not, it works fine and can cast the _id. However, when placed within a $not{} tag, it tries to cast as ObjectID and fails.
The text was updated successfully, but these errors were encountered: