My collection looks like this: (Don’t ask me why it’s an array inside of a collection)
const postsSchema = mongoose.Schema({
posts: {type: Array},
})
I want to find a document in it and I tried this:
Posts.aggregate([
{
$match:{
'posts': { _id: new ObjectId(req.query.postId), }
}
}
])
And this:
Posts.find({
posts: {
$elemMatch: { "_id" : new ObjectId(req.query.postId) }
}
})
And this:
Posts.findOne({
'posts._id': new ObjectId(req.query.postId)
})
2
Answers
If you use async function you can put await else you can do .then method to get the result and avoid the await keyword
to help you I can show you this code I made in typescript.
use the await to retrieve the give and put your function in async
the model (user job is the array
the get and update