javascript - 使用猫鼬填充过滤数据
问题描述
我有两个数据结构“数据库”和“组件”
const DatabaseSchema = mongoose.Schema({
components: [{ type: Schema.Types.ObjectId, ref: 'Components', required: false }],
});
const ComponentsSchema = mongoose.Schema({
name: { type: String, required: true, trim: true, unique: true, lowercase: true },
updatedAt: Date,
});
我想按组件名称过滤数据库中的所有项目
我正在使用的搜索规则
Database.find({
components: { $elemMatch: { name: /antr/i } }
}).populate({
path: 'components',
select: 'name -_id'
}).select(['descript','components']).exec( (err,data) => {
console.log(err);
res.json(data);
});
但是总是返回一个空元素
解决方案
请试试这个:
正如我已经建议你可以使用这个:
Database.find({})
.populate({ path: 'components', match: { name: /antr/i }, select: 'name -_id' })
.exec((err, data) => { console.log(err); res.json(data); });
由于您看到空数组是因为过滤器查询在match
数据库文档的组件数组中的组件集合 wrt ObjectIds 中找不到合适的文档,这是正常的。也许你可以在代码中过滤掉那些,因为你不是这样看的,你可以使用 mongoDB 的$lookup
来自聚合框架,它相当于.populate()
来自 mongoose。
Database.aggregate(
[{
$lookup: {
from: "components",
"let": { "ids": "$components" },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$ids'] } } }],
as: "dbComponentsArray"
}
}, { $unwind: '$dbComponentsArray' }, { $match: { 'dbComponentsArray.name': /antr/i } },
{ $group: { _id: '$_id', dbComponentsArray: { $push: '$dbComponentsArray' }, data: { $first: '$$ROOT' } } }, { $addFields: { 'data.dbComponentsArray': '$dbComponentsArray' } },
{ $replaceRoot: { 'newRoot': '$data' } }])
集合中的示例数据:
成分 :
/* 1 */
{
"_id" : ObjectId("5d481cd098ba991c0857959f"),
"name" : "antracito",
"updatedAt" : ISODate("2019-08-05T12:10:56.777Z"),
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("5d481cd098ba991c0857958f"),
"name" : "anacito",
"updatedAt" : ISODate("2019-08-05T12:10:56.777Z"),
"__v" : 0
}
/* 3 */
{
"_id" : ObjectId("5d481cd098ba991c0857951f"),
"name" : "antracito",
"updatedAt" : ISODate("2019-08-05T12:10:56.777Z"),
"__v" : 0
}
/* 4 */
{
"_id" : ObjectId("5d481cd098ba991c0857952f"),
"name" : "anacito",
"updatedAt" : ISODate("2019-08-05T12:10:56.777Z"),
"__v" : 0
}
数据库:
/* 1 */
{
"_id" : ObjectId("5d4979d52a17d10a6c8de81b"),
"components" : [
ObjectId("5d481cd098ba991c0857951f"),
ObjectId("5d481cd098ba991c0857952f"),
ObjectId("5d481cd098ba991c0857953f"),
ObjectId("5d481cd098ba991c0857959f")
]
}
输出 :
/* 1 */
{
"_id" : ObjectId("5d4979d52a17d10a6c8de81b"),
"components" : [
ObjectId("5d481cd098ba991c0857951f"),
ObjectId("5d481cd098ba991c0857952f"),
ObjectId("5d481cd098ba991c0857953f"),
ObjectId("5d481cd098ba991c0857959f")
],
"dbComponentsArray" : [
{
"_id" : ObjectId("5d481cd098ba991c0857959f"),
"name" : "antracito",
"updatedAt" : ISODate("2019-08-05T12:10:56.777Z"),
"__v" : 0
},
{
"_id" : ObjectId("5d481cd098ba991c0857951f"),
"name" : "antracito",
"updatedAt" : ISODate("2019-08-05T12:10:56.777Z"),
"__v" : 0
}
]
}
推荐阅读
- python - 将灰度 png 转换为 RGB png 图像
- wordpress - Wordpress:“您关注的链接已过期”问题
- javascript - 如何使中间件在页面加载之前完成对服务器的异步请求并提交到 Vuex 存储?
- angular - kendoGrid 弹出自定义验证,多个字段
- javascript - 我应该在测试中使用 toHaveBeenCalledTimes 来计算调用了多少次调度吗?
- javascript - 如果用户交互,则暂停自主功能?
- python - vscode 中的 Python 版本设置为 2.7,而 3.7 应该正在运行。如何修复工作区和用户设置以使其正常工作?
- hana - SAP HANA 历史表何时(不)使用,优点/缺点
- html - 在输入字段和 input-group-addon 之间添加标签
- java - 使用 maven 的基于 Spring 引导配置文件的 WAR