Mongoose 在现有猫鼬收藏中查找

Mongoose 在现有猫鼬收藏中查找,mongoose,Mongoose,我是刚接触猫鼬的。我正在研究平均角度2应用程序。我已经创建了基本设置,需要开始使用MEAN angular 2应用程序 我有model文件夹,其中定义了模型模式。我已经正确地将其导出到其他地方的access 我有一个routes文件夹,在那里我注册api调用以将数据保存到数据库。最后我返回路由器 在我的服务器app.js或index.js上,我引用了bodyparser和express、mongoose和connected to mongoose。注册api路由等 我用邮递员来测试这个 下面是一

我是刚接触猫鼬的。我正在研究平均角度2应用程序。我已经创建了基本设置,需要开始使用MEAN angular 2应用程序

  • 我有
    model
    文件夹,其中定义了模型模式。我已经正确地将其导出到其他地方的access
  • 我有一个
    routes
    文件夹,在那里我注册api调用以将数据保存到数据库。最后我返回路由器
  • 在我的服务器app.js或index.js上,我引用了bodyparser和express、mongoose和connected to mongoose。注册api路由等

  • 我用邮递员来测试这个

  • 下面是一个正在创建集合的路由

    const testModel= require('../models/testModel');
    
    module.exports = (router) => {
    
        router.post('/Update', (req, res) =>{
    
            console.log(req.body);
    
            let tModel= new testModel({
                a: req.body.a,
                 key1:{
                    b: req.body.b,                
                    key2: {
                      c: req.body.c,                     
                      key3: {
                        x: req.body.x                     
                      }
                    }
                 }      
            });
    
            tModel.save((err) =>{
                if(err){
                    res.json({success : false, message: 'Could not update details. Error:'+ err});
                }else{
                    res.json({success: true, message: 'details updated'});
                }
            });
    
        });
    }
    
    问题陈述:

    就在上面的路由之后,我又写了一条路由来从同一个集合中获取数据。当我传递多个参数时,这不起作用。下面是相同的代码段

    const testModel= require('../models/testModel');
    router.get('/getDetails', (req,res) => {
    
                 testModel.find({'a': '1', 'b' : '2', 'c': '3'}).exec(function(err, data) {
                        if(err){
                            res.json({success: false, messsage : 'Could not get the details. Error:' + err})
                        }
                        else{
                            res.send(data);
                        }
    
    但是,如果没有参数或只有一个参数,当与邮递员一起测试时,我会得到结果

    const testModel= require('../models/testModel');
    router.get('/getDetails', (req,res) => {
                 console.log("req body::" + JSON.stringify(req.body));
                 testModel.find().exec(function(err, data) {
                        if(err){
                            res.json({success: false, messsage : 'Could not get the details. Error:' + err})
                        }
                        else{
                            res.send(data);
                        }
    

    请提供帮助。

    您正在运行错误的筛选器来查找文档

    {'a':'1','b':'2','c':'3'}

    问题是在模型对象中,您正在将
    b
    定义为另一个属性的属性,该属性是
    key1
    ,将
    c
    定义为
    key1.key2.c
    的属性,以此类推,与x的属性相同

    {
                a: req.body.a,
                 key1:{
                    b: req.body.b,                
                    key2: {
                      c: req.body.c,                     
                      key3: {
                        x: req.body.x                     
                      }
                    }
                 }      
            }
    
    使用提供的过滤器,mongoDB不会返回任何对象,因为它没有对象,其中b和c是对象的直接属性

    您可以做的是尝试以下过滤器,看看这是否适合您

    {
     "a" : 1,
     "key1.b":2,
     "key1.key2.c": 3 
    }