Node.js Mongoose多个可选';和';条件
我正在尝试筛选具有3个可选“和”条件的集合 这是我的模型:Node.js Mongoose多个可选';和';条件,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我正在尝试筛选具有3个可选“和”条件的集合 这是我的模型: const Company = mongoose.model( 'Company', new Schema({ name: { type: String }, sectors: [{ type: Schema.Types.ObjectId, ref: 'Sector' }], industries: [{ type: Schema.Types.ObjectId, ref: 'Industry' }],
const Company = mongoose.model(
'Company',
new Schema({
name: { type: String },
sectors: [{ type: Schema.Types.ObjectId, ref: 'Sector' }],
industries: [{ type: Schema.Types.ObjectId, ref: 'Industry' }],
countries: [{ type: Schema.Types.ObjectId, ref: 'Country' }],
})
和我的组件:
const getCompanies = (skip, limit, filter) =>
Company.find({
...filter.countries && { countries: filter.countries },
...filter.sectors && { sectors: filter.sectors },
...filter.industries && { industries: filter.industries },
})
.skip(skip)
.limit(limit)
.sort({ date: -1 })
.populate('countries')
.populate('sectors')
.populate('industries');
const getAll = async (req, res) => {
try {
const countries = req.query.country;
const sectors = req.query.sector;
const industries = req.query.industry;
const skip = parseInt(req.query.skip, 10);
const limit = parseInt(req.query.limit, 10);
const filter = {
...countries && { countries },
...sectors && { sectors },
...industries && { industries },
};
const result = await getCompanies(skip, limit, filter);
return res.status(200).json(result);
} catch (e) {
return res.status(500).send({ message: (e.message) });
}
};
当筛选器为空时,这会起作用,但当筛选器中有一个或多个项时,我会得到一个空数组
如果我在GetCompanys中硬编码数据,如下所示:
Company.find({
countries: '5d5e913e20c01070fef5c77e',
sectors: '5d5e913e20c01070fef5c754',
industries: '5d5e913e20c01070fef5c7ad',
})
或:
我得到了我想要的数据
我还尝试将console.log过滤器记录在getCompanies中,以确保数据正确,如果请求了所有字段,我会得到:
{
countries: '5d5e913e20c01070fef5c77e',
sectors: '5d5e913e20c01070fef5c754',
industries: '5d5e913e20c01070fef5c7ad',
}
这只是一个例子:
{ countries: '5d5e913e20c01070fef5c77e' }
所以我觉得很好
我还尝试使用“$and”这样:
Company.find({ $and: [
{ ...filter.countries && { countries: filter.countries } },
{ ...filter.sectors && { sectors: filter.sectors } },
{ ...filter.industries && {industries: filter.industries } },
],
})
或使用“$in”:
Company.find({
...filter.countries && { countries: { $in: filter.countries } },
...filter.sectors && { sectors: { $in: filter.sectors } },
...filter.industries && { industries: { $in: filter.industries } },
})
但也没有运气
以下是一个示例URL:
GET /api/internal/member/get?skip=12&limit=6&country=5d5e913e20c01070fef5c77e§or=&industry=
我还发现了一些问题与我的有些相似的其他线程,但它们是不同的,以帮助我解决我的问题
期待着您的建议。我终于让它发挥作用了。事实证明,我没有在每次更改时清除前端中的数据,这导致跳过/限制字段出现问题 我还按照@whoami的建议更改了发现,如下所示:
Company.find({ $and: [
{
...filter.countries && { countries: { $in: [mongoose.Types.ObjectId(filter.countries)] } },
...filter.sectors && { sectors: { $in: [mongoose.Types.ObjectId(filter.sectors)] } },
...filter.sdgs && { sdgs: { $in: [mongoose.Types.ObjectId(filter.sdgs)] } },
}
],
})
嘿!您可以共享用于访问此端点的示例URL(以及查询参数)吗?我怀疑即使您硬编码如下:
Company.find({countries:'5d5e913e20c01070fef5c77e'})
如果这些值的类型为ObjectId(),您的数据库中是否会有任何数据
那么输入中传递的字符串与任何内容都不匹配,所以将字符串转换为ObjectId()&像这样传递输入const mongoose=require('mongoose');{…filter.countries&&{countries:mongoose.Types.ObjectId(filter.countries)}
,如果它们是字符串数组,则迭代、转换并传入..感谢您的回答。我用一个示例URL编辑了我的文章。@whoami我再次尝试对数据进行硬编码,效果很好。我也尝试了您的解决方案,但仍然得到一个空数组。
Company.find({ $and: [
{
...filter.countries && { countries: { $in: [mongoose.Types.ObjectId(filter.countries)] } },
...filter.sectors && { sectors: { $in: [mongoose.Types.ObjectId(filter.sectors)] } },
...filter.sdgs && { sdgs: { $in: [mongoose.Types.ObjectId(filter.sdgs)] } },
}
],
})