基于带有日期的动态日期键的猫鼬过滤器 [英] Mongoose Filter based on Dynamic Date Key with value

查看:73
本文介绍了基于带有日期的动态日期键的猫鼬过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个员工出勤应用程序,其中记录了出勤情况并将其存储在数据库中.我试图获得所有日期字段的计数,其值为"Present".数据像这样存储在数据库中:

I have created an employee attendance application where attendances are logged and stored in a database. I have tried to obtain a count of all date-field with the value of "Present". Data are stored in the database like so :

"attendances": { <YYYY-MM-DD>: "value" } pair 
// The values being "Absent" or "Present" whatever the case may be. 

问题是,每当我尝试用"attendances": {"2019-08-28": "Present"}计数所有条目时,我都会得到一个0值.

The problem is, I get a value of 0 whenever I try to count all the entries with "attendances": {"2019-08-28": "Present"}.

有人可以帮我找出做错了什么吗?

Can anyone help me find out what am doing wrong?

//架构

const Schema = mongoose.Schema;
const employeeSchema = new Schema({
  name: String,
  department: String,
  origin: String,
  employDate: String,
  attendances: Object
});
module.exports= Employee = mongoose.model('Employee', employeeSchema);

route.js

router.get('/',(req,res)=>{
  Employee.collection.countDocuments({"attendances.date":"present"},(err,data)=>{
    if(err){
      res.status(500)
      res.send(err)
    }else{
      res.status(200)
      res.json(data)
    }
  })

})

//存储在MongoDB中的数据

//Data stored in MongoDB

  {
        "_id": "5d6565236574b1162c349d8f",
        "name": "Benjamin Hall",
        "department": "IT",
        "origin": "Texas",
        "employDate": "2019-08-27",
        "__v": 0,
        "attendances": {
            "2019-08-28": "Sick"
        }
    },
    {
        "_id": "5d6367ee5b78d30c74be90e6",
        "name": "Joshua Jaccob",
        "department": "Marketing",
        "origin": "new york",
        "employDate": "2019-08-26",
        "__v": 0,
        "attendances": {
            "2019-08-26": "Present",
            "2019-08-27": "Sick"
        }
    },

推荐答案

如果要按嵌入式文档中的属性查找,则必须使用点表示法

If you want to find by property in embedded document you have to use dot notation

这将不起作用,因为您正在要求mongoo查找出勤对象等于给定对象的文档.

this will not work, because you are asking mongoo to find the document which have attendances object equal the same given object.

{ "attendances": {"2019-08-26": "Present"}}

这仅在数据库中的出勤对象仅包含

this will work only if attendances object in your database contains only

{ "attendances": {"2019-08-26": "Present"}}

这意味着您问mongoo存储的对象是否等于给定的对象,它将返回false

that's mean that you asking mongoo if the stored object is equal the given object and it will return false

 { "attendances": {"2019-08-26": "Present" , "2019-08-27": "Sick"}} ==  { "attendances": {"2019-08-26": "Present"}}

为此,您必须使用点符号

to do this you have to use dot notation

 Employee.collection.countDocuments({"attendances.2019-08-26":"Present"},(err,data)=>{
    if(err){
      res.status(500)
      res.send(err)
    }else{
      res.status(200)
      res.json(data)
    }
  })

这篇关于基于带有日期的动态日期键的猫鼬过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆