{members: { $in: [user] } }
i am tring tring to execute this query in my project, postman show no data and in mongodb campass it is showing 1 doc and that it i want.
the req.query is equal to this
Query: { members: { '$in': [ '6633e81c999ca497b24d6dc9' ] } }
members is the array of user in a project model
here is the rest of the detailed code
model
const projectSchema = new mongoose.Schema({
name: {
type: String,
maxLength: [40, 'Project name must be less than 40 characters'],
required: [true, 'Project must have a name']
},
description: String,
createdAt: {
type: Date,
default: Date.now()
},
slug: String,
projectManager: {
type: mongoose.Schema.ObjectId,
ref: 'User'
},
members: [
{
type: mongoose.Schema.ObjectId,
ref: 'User'
}
]
});
controller function
exports.findAll = (Model, popOptions) =>
catchAsync(async (req, res, next) => {
const user = req.user.id;
// let query = Model.find({ members: user });
let query = Model.find({
$or: [{ projectManager: user }, { members: { $in: [user] } }]
});
if (popOptions) query = query.populate(popOptions);
console.log('Query:', query.getQuery());
const features = new APIFeatures(query, req.query)
.filter()
.sort()
.limitFeilds();
const doc = await features.query;
res.status(200).json({
status: 'success',
result: doc.length,
data: {
data: doc
}
});
});
exports.getAllProject = factoryHandler.findAll(Project, [
{ path: 'projectManager' },
{ path: 'members' }
]);
I am trying to get the projects in which the currently login user is enrolled as member or project manager
it give the projects docs in which user is project manager but not giving the project in which the user is a member
i don’t understand the problem i try to use the chatgpt to solve it but nothing work
Kashaf Dawood is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.