2017-04-24 60 views
0

我想用真/假条件广东话从数据库中提取数据

这里来从MongoDB的一些数据是回调函数:

module.exports.getAllTeachersByValidation = function (condition, fields, options, callback){ 
    const query = { 
     account: { 
      valid: condition 
     } 
    }; 
    Teacher.find(query, fields, options, callback); 
}; 

这里是方案:

const teacherSchema = mongoose.Schema({ 
    account: { 
     username: { 
      type: String, 
      required: true, 
      unique: true, 
      lowercase: true 
     }, 
     password: { 
      type: String, 
      required: true 
     }, 
     valid: { 
      type: String, 
      enum: ["true","false"], 
      required: true, 
      lowercase: true 
     } 
    }, 

,代码:

const condition = req.query.condition; 
    const ret_values = "_id " 
      + "ID " 
      + "account.username " 
      + "account.valid " 
      + "name " 
      + "communication.email " 
      + "communication.phone"; 
    if(typeof condition !== 'undefined'){ 
     console.log("Sending teachers by validation: " + condition); 
     Teacher.getAllTeachersByValidation(condition.toLowerCase(), ret_values, {}, (error, teachers) => { 
      if (error) throw error; 
      if(teachers){ 
       return res.json({ 
        success: true, 
        teachers 
       }); 
      }else{ 
       return res.json({ 
        success: false, 
        msg: "Error retrieving teachers", 
        error: "No teacher to be found" 
       }); 
      } 
     }); 
    } 

我试过有效的时候是一个字符串类型,当有效的时候是一个布尔类型,但我总是得到一个空教师的数组作为结果(成功:真)

有人可以解释为什么.find()不返回数组?或者我的问题在哪里?

当我不使用条件字段它返回所有教师(包括当有效是假/真)

回答

1

您的查询是不是在这种情况下,正确的

相反的:

const query = { 
     account: { 
      valid: condition 
     } 
}; 

用途:

const query = { 
     "account.valid": condition 
}; 

在第一种情况下,你只能查询文件卫生组织Ë帐户子文档正是{ “有效”: “真正的”},没有用户名密码

更多的信息在这里:https://docs.mongodb.com/manual/tutorial/query-embedded-documents/