I have the schema
const UserSchema = new Schema({
_id: {
type: String,
require: true
},
paused: {
type: Boolean,
default: false
},
name: {
type: String,
default: ''
},
type: {
type: String,
enum: ['private', 'group']
},
})
const PasswordSchema = new Schema({
password: {
type: String,
require: true
},
used: {
type: Boolean,
default: false,
},
usedBy: UserSchema
})
Like this, works fine
await Password.findOne({"usedBy._id": 123}) // 123 is converted to string
But aggregate is not converting the _id
await Password.aggregate().match({ "usedBy._id": 123 }) // 123 keeps number, so the query returns null
This is normal? I didn't want to have to convert the _id manually in all queries
According to the issue: https://github.com/Automattic/mongoose/issues/10032
This is expected:
Mongoose doesn't cast aggregation pipelines. That's because aggregation pipelines can, and usually do, change the shape of the data in MongoDB. If you need casting, you can use find(), or you can use the Query#cast() method to cast the filter:
await Password.aggregate().match(new Query({ "usedBy._id": 123 }).cast(Password).getFilter())