Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

javascript - Drilling Down Mongoose Model, select?, aggregate?, find? maybe a combination?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...