I have a mongoDB, node server, mongoose model using Schema. I want to eventually do searches on these Guides models by "timeLostInSeconds", "description", etc.
For example I want the json response based on the model below to show the id, description, and timeLostInSeconds.
What i have done is used .select('-errorCode') for example which gets rid of that when I test my route with postman but i need to drill down to the guideList array of objects so that eventually on the client side i can have a search bar and search and display these things. I was also thinking maybe the model is written wrong for how it is in the db?
I've provided my code and what the response is now with a normal .find with a .limit of 1
//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",
"timeLostInSeconds" : {
"engineering" : 600,
"qcInspector" : 361,
"assembler" : 90
}
},
{
"category" : "Build technique",
"description" : "Units DC connector positive anderson pin is improperly crimped",
"timeLostInSeconds" : {
"engineering" : 600,
"qcInspector" : 361,
"assembler" : 120
}
},
{
"category" : "Build technique",
"description" : "Units DC connector negative anderson pin is improperly crimped",
"timeLostInSeconds" : {
"engineering" : 600,
"qcInspector" : 361,
"assembler" : 120
}
}
]
}
]
}
question from:
https://stackoverflow.com/questions/65854669/drilling-down-mongoose-model-select-aggregate-find-maybe-a-combination 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…