I have an issue.
I should use the mongoose find() method for some queries. While using the find() method, when I give filter find method works but it doesn't give me value/values.
const messages = await MessageSchema.find({sessionId: sessionId}, {__v: 0, _id: 0, updatedAt: 0}).exec();
The above code is my find method. This code gives me as value, empty array. []
But in this code,
const messages = await MessageSchema.find({}, {__v: 0, _id: 0, updatedAt: 0}).exec();
give me an array and all of the messages are in the array.
But I should use filtering.
This is my message Schema.
const mongoose = require('mongoose');
var schema = new mongoose.Schema({
participant_name: {type: String},
message: {type: String},
sessionId: {type: String},
participantId: {type: Object}
}, {
timestamps: true
});
module.exports = mongoose.model("Message", schema)
Could you help me please?
You have to cast your sessionId to Object id type in filtering.
how?
import { Types } from 'mongoose';
const messages = await MessageSchema.find({sessionId: Types.ObjectId(sessionId)}, {__v: 0, _id: 0, updatedAt: 0}).exec();