简体   繁体   English

使用 mongoose 在 Mongo DB 中搜索和过滤数据

[英]Search and filter data in Mongo DB using mongoose

 var questionSchema = new Schema({ category: { name: { type: String, lowercase: true, required: [true, 'Category is a required field'] }, question:[ { q: { type: String, lowercase: true }, options: { option1:{ type: String, lowercase: true }, option2:{ type: String, lowercase: true ] }, option3:{ type: String, lowercase: true }, option4:{ type: String, lowercase: true }, }, ans: { type: String, lowercase: true }, level:{ type: String, lowercase: true } } ] }, }, { strict: true, runSettersOnQuery: true, timestamps: { createdAt: 'created', updatedAt: 'updated' } });

This is my question schema.这是我的问题架构。 I am trying to get all the questions of a particular category.我正在尝试获取特定类别的所有问题。 Till now I have tried this ---直到现在我已经尝试过这个---

 function (req,res,next) { console.log(req.params.qcategory); Question.find({ category:{ name: req.params.qcategory, } },'', function (err,data) { if (err) { err.status = 406; return next(err); } console.log(data); return res.status(201).json({ message: ' success.',data:data }) }) };

In req.params.qcategory it contains the name of the category like 'programming' for instance.在 req.params.qcategory 中,它包含类别的名称,例如“编程”。 But it returned me an empty array.但它返回给我一个空数组。 **PS. **附注。 There are questions of programming category in my DB.我的数据库中有编程类别的问题。 Then What am i doing wrong ???那我做错了什么???

Also is there a way to first check what category a question is and then add to the question array inside that particular category.还有一种方法可以先检查问题是什么类别,然后添加到该特定类别内的问题数组中。 I don't want to add questions with categories again and again instead i want to check if that category already exists in the database and push the question inside the array of that question category.我不想一次又一次地添加带有类别的问题,而是想检查数据库中是否已经存在该类别并将问题推送到该问题类别的数组中。 For instance if a question is of 'programming' category and in the database there is already a question which is of again a 'programming' category then push the question inside the array of programming category.例如,如果一个问题属于“编程”类别,并且在数据库中已经存在一个再次属于“编程”类别的问题,则将该问题推送到编程类别数组中。

If my questions are not clear then please comment I will respond quickly.如果我的问题不清楚,请发表评论,我会尽快回复。

Try this, Not tested试试这个,未测试

function (req,res,next) {
console.log(req.params.qcategory);
Question.find({
    "category.name": req.params.qcategory,}
 , function (err,data) {
    if (err) {
        err.status = 406;
        return next(err);
    }
    console.log(data);
    return res.status(201).json({
        message: ' success.',data:data
    })
})
};

Replace the {category:{name:req.params.qcategory}} with {"category.name":req.params.qcategory} .{category:{name:req.params.qcategory}}替换为{"category.name":req.params.qcategory}

read more on mongodb's Query Embedded Documents https://docs.mongodb.com/.../method/db.collection.find/阅读更多关于 mongodb 的查询嵌入文档https://docs.mongodb.com/.../method/db.collection.find/

Try this.试试这个。

function (req,res,next) {
    console.log(req.params.qcategory);
    const name = req.params.qcategory;
    Question.find({name})
        .then((data)=>return res.status(201).json({message:"success",data:data})
        .catch(err=> {
            res.status(406)
            return next(err);
        })
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM