I have mongoDB document which looks like this:
{
"_id": {
"$oid": "5b99247efb6fc01dae438815"
},
"participants": [
"5b758a8341ee61f049ded486",
"5b94fb4ffb6fc01dae40eae3"
]
}
The document Schema in Mongoose is defined as such
var conversationSchema = new mongoose.Schema({
participants: [{ type: mongoose.Schema.ObjectId, ref: 'User'}],
});
I am fetching the data as such
var ccc = Conversation.find({participants : "5b758a8341ee61f049ded486"});
ccc.exec(function(err, conversations){
res.status(200).json(conversations);
});
The problem is that I am getting an empty array response [].
I think the problem is with Schema but I can't figure out how can I make this to work.
EDIT, If I change my Schema to the following it will work:
var conversationSchema = new mongoose.Schema({
participants: [{ type: String}],
});
But I want to work with mongoose.Schema.ObjectId and not Strings as foreign key.
Conversation.find({ participants: ObjectId("5b758a8341ee61f049ded486") })mongoose.Types.ObjectId("5b758a8341ee61f049ded486")but still same empty array response.