I have the following Schema:
const userSchema = new Schema({
local: {
email: {type: String, unique: true, lowercase: true, trim: true, required: true},
password: {type: String, required: true},
},
Items: [{
name: {type: String, trim: true},
createdAt: {type: Date, default: Date.now, select: false}
}]
});
How do I query 'Items' (which contains array of objects) based on a specific _id that I will receive from the url parameter?
I have many different variations, including one shown below, but they all seem to return the Items array containing all objects, instead of just a single object matching the id?
User.findById(req.user.id, {}, {"Items._id": req.params.id}, (err, result) => { ... }
User.findOne({_id:req.user.id, "Items._id": req.params.id},{}, {"Items.$": 1}, (err, result) => { ... }db.users.findOne({_id:req.user.id, "Items._id": req.params.id},{"Items.$": 1}). It will return one item value. I'll try to find the right mongoose query.User.findOne({_id:req.user.id, "Items._id": req.params.id},{"Items.$": 1}, (err, result) => { ... }