I would like to count all rooms for the particular user with particular roles.
Here is my schema:
const RoomSchema = new Schema({
...
moderator: String,
// Subdocument with two fields *email* and *role*
participants: {
type: [{
_id: false,
email: {
type: String,
index: {
unique: true,
},
},
role: String,
}],
default: [],
},
...
})
Here is the query:
let userEmail = "xxx"
const result = await RoomModel
.count({
$or: [
{ moderator: userEmail },
{
participants: {
email: userEmail,
// role: "reader", works fine
// role: { $in: ["reader"] }, ERROR
}
},
]
})
.exec()
Here is the error:
CastError: Cast to string failed for value "{ '$in': [ 'reader' ] }" (type Object) at path "role" for model "Room"
Thank you in advance.