简体   繁体   English

Mongoose 根据条件和特定日期之间获取文档的查询

[英]Mongoose query for fetching documents based on conditions and between certain dates

I have a page which lists all the students data in front end.我有一个页面,其中列出了前端的所有学生数据。 Currently I am fetching all the documents irrespective of their showStudentProfile status.目前我正在获取所有文档,而不管它们的 showStudentProfile 状态如何。 Now, I need to fetch all the students from mongoDB using Mongoose query based on below conditions.现在,我需要根据以下条件使用 Mongoose 查询从 mongoDB 获取所有学生。

Conditions:状况:

  1. If showStudentProfile === hide && owner === userId ( userId is the LoggedIn User), then fetch students list.如果showStudentProfile === hide && owner === userIduserId是登录用户),则获取学生列表。 ie, he should be only able to fetch his own hidden documents.也就是说,他应该只能获取他自己的隐藏文件。

2.If showStudentProfile === custom , then fetch only those documents if current date is between the dateRange field. 2.如果showStudentProfile === custom ,则如果当前日期在dateRange字段之间,则仅获取那些文档。

  1. If showStudentProfile === yes , then fetch the documents.如果showStudentProfile === yes ,则获取文档。

Students schema is like below:学生模式如下:

[{
  "_id": ObjectId("5f44af5a232afe415dc2bc01"),
    "name" : "abc",
    "sub" : ["math", "generalScience"],
    "showStudentProfile" : "yes",
    "dateRange" : [],
    "owner": ObjectId("5f44af5a232afe415dc2bc03"),
},{
  "_id": ObjectId("5f44af5a232afe415dc2bc04"),
  "name" : "def",
  "sub" : ["physics", "chemistry"],
  "showStudentProfile" : "hide",
  "dateRange" : [],
  "owner": ObjectId("5f44af5a232afe415dc2bc03"),
},{
  "_id": ObjectId("5f44af5a232afe415dc2bc05"),
  "name" : "ghi",
  "sub" : ["math", "science"],
  "showStudentProfile" : "custom",
  "dateRange" : [ 
    "2020-07-28T11:14:50.652Z", 
    "2020-08-28T11:14:50.652Z"
],
  "owner": ObjectId("5f44af5a232afe415dc2bc03"),
},{
  "_id": ObjectId("5f44af5a232afe415dc2bc07"),
  "name" : "xyz",
  "sub" : ["physics", "chemistry"],
  "showStudentProfile" : "hide",
  "dateRange" : [],
  "owner": ObjectId("5f44af5a232afe415dc2bc08"),
}]

My Attempt:我的尝试:

StudentModel.find({showStudentProfile:"hide", owner:"userId"})

But this will fetch only those documents whose showStudentProfile is hide and owner is userId .但这只会获取那些showStudentProfile为 hide 且owneruserId的文档。 But I also need to fetch the documents whose showStudentProfile is custom and if custom and considering the currentDate, I need to fetch the documents which falls between the dateRange field.但我还需要获取showStudentProfilecustom的文档,如果custom并考虑到 currentDate,我需要获取落在dateRange字段之间的文档。

Expected result if the userId = ObjectId("5f44af5a232afe415dc2bc03") and considering today's date:如果userId = ObjectId("5f44af5a232afe415dc2bc03")并考虑今天的日期,预期结果:

[{
  "_id": ObjectId("5f44af5a232afe415dc2bc01"),
    "name" : "abc",
    "sub" : ["math", "generalScience"],
    "showStudentProfile" : "yes",
    "dateRange" : [],
    "owner": ObjectId("5f44af5a232afe415dc2bc03"),
},{
  "_id": ObjectId("5f44af5a232afe415dc2bc04"),
  "name" : "def",
  "sub" : ["physics", "chemistry"],
  "showStudentProfile" : "hide",
  "dateRange" : [],
  "owner": ObjectId("5f44af5a232afe415dc2bc03"),
}];

You have three conditions, so you can get it from $match and $or combination db.collection.aggregate([你有三个条件,所以你可以从$match$or组合 db.collection.aggregate([

db.collection.aggregate([
  {
    $match: {
      $or: [
        {
          owner: ObjectId("5f44af5a232afe415dc2bc03"),
          showStudentProfile: "hide"
        },
        {
          showStudentProfile: "custom",
          $expr: {
            $and: [
              {
                $lt: [
                  {
                    $toDate: {
                      $arrayElemAt: [
                        "$dateRange",
                        0
                      ]
                    }
                  },
                  new Date()
                ]
              },
              {
                $gt: [
                  {
                    $toDate: {
                      $arrayElemAt: [
                        "$dateRange",
                        1
                      ]
                    }
                  },
                  new Date()
                ]
              }
            ]
          }
        },
        {
          showStudentProfile: "yes"
        }
      ]
    }
  }
])

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

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