I have the following mongoose schema:
export interface MusicInput {
title: string
artist: string
album: string
genre: string
userId?: string // maybe make this mongoose.type.ObjectId
}
export interface MusicDocument extends mongoose.Document, MusicInput {
createdAt: Date
updatedAt: Date
}
const musicSchema = new mongoose.Schema(
{
title: {
type: String,
require: true
},
artist: {
type: String,
require: true
},
album: {
type: String,
require: true
},
genre: {
type: String,
require: true
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
require: false
}
},
{ timestamps: true }
)
const MusicModel = mongoose.model<MusicDocument>('Music', musicSchema)
export default MusicModel
What I want to do is search for a music based on the music._id. But I have tried everything possible: MusicModel.findOne({_id: <some_valid_id>})
, or MusicModel.findById(<some_valid_id>)
But it always returning null. Here is the code I am currently using to find a music by their id:
export const findMusic = async (
query: FilterQuery<MusicDocument>,
options: QueryOptions = { lean: true }
) => {
return MusicModel.findOne(query, {}, options)
}
What is going on? I have been surfing the internet but still can not find the solution.
For Reference
Mongoose Version => 8.5.2