Having some issues performing a findOne query with an array of ObjectIds. Simplified schema as follows:
Model:
var InboxSchema = new Schema({
_users: [
{
type: mongoose.Schema.ObjectId,
ref: 'User',
required: 'Users are required',
unique: true
}
]
}
mongoose.model('Inbox', InboxSchema)
Query:
var users = [];
var userIds = ['5567ead844997f969fe3f00f', '558b8492f1723b090b414765'];
for (var i=0; i<userIds.length; i++) {
users.push(new mongoose.Types.ObjectId(userIds[i]));
}
Inbox.findOne({_users: users}).exec(function (err, inbox) {
console.log(inbox);
}
Inbox always returns null despite it returning something when using Mongo shell, so I know that my query works. Previously, I was querying on just userIds alone, which I later realised was not working as I would have to query using ObjectId. However, even converting the strings to ObjectId doesn't work.