如何解决钻取 Mongoose 模型,选择?,聚合?,查找?也许是一个组合?
我有一个使用 Schema 的 mongoDB、节点服务器、猫鼬模型。我想最终通过“timeLostInSeconds”、“描述”等对这些指南模型进行搜索。
例如我希望基于下面模型的 json 响应显示 id、描述和 timeLostInSeconds。
我所做的是使用 .select('-errorCode') 例如,当我用邮递员测试我的路线时,它摆脱了它,但我需要深入到 guideList 对象数组,以便最终在客户端我可以有一个搜索栏并搜索和显示这些东西。我也在想,也许模型在数据库中的写法有误?
我已经提供了我的代码以及现在使用 .limit 为 1 的普通 .find 的响应
//model
const Guides = require('../models/guides.js')
exports.list = (req,res) => {
let limit = req.query.limit ? parseInt(req.query.limit) : 1;
Guides.find()
.limit(limit)
.exec((err,guides) => {
if (err) {
return res.status(400).json({
error: 'Guides not found'
});
}
res.json(guides);
});
};
//controller
const mongoose = require('mongoose');
const guidesSchema = new mongoose.Schema(
{
errorCode: {
type: String
},guides: {
type: Array
},},{timestamps: true}
);
module.exports = mongoose.model('Guides',guidesSchema)
//model document in json from mongoDB:
{
"_id" : ObjectId("5f7b305bba7a53f9c9caf021"),"errorCode" : "487/489-1","guides" : [
{
"name" : "Production","guideList" : [
{
"category" : "Loose connection","description" : "Units DC connector positive anderson pin not inserted properly","timeLostInSeconds" : {
"engineering" : 600,"qcInspector" : 361,"assembler" : 90
}
},{
"category" : "Loose connection","description" : "Units DC connector negative anderson pin not inserted properly",{
"category" : "Build technique","description" : "Units DC connector positive anderson pin is improperly crimped","assembler" : 120
}
},"description" : "Units DC connector negative anderson pin is improperly crimped","assembler" : 120
}
}
]
}
]
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。