如何使用 Mongoose 方案 return 按日期在 MongoDB 数据库中查询?

How to return a query in a MongoDB database by Date, using Mongoose schemes?

我正在尝试在我的 API 中创建一个端点,使用 mongodb e mongoose return 我数据库中最年轻和最年长的人。

我的数据库是这样的:

{
"message": "Employees list",
"result": [
    {
        "_id": "62562137bd9acaa47a13e2d2",
        "name": "Anakin Skywalker",
        "email": "skywalker@ssys.com.br",
        "department": "Architecture",
        "salary": 4000,
        "birth_date": "1983-01-01T03:00:00.000Z",
        "__v": 0
    },
    {
        "_id": "625873509d27198519c460cb",
        "name": "pedro",
        "email": "pedror@ssys.com.br",
        "department": "Architecture",
        "salary": 700000,
        "birth_date": "1988-01-01T02:00:00.000Z",
        "__v": 0
    },
    {
        "_id": "6258b2bb6f4786b217624c2d",
        "name": "Henrique Skywalker",
        "email": "henrique@ssys.com.br",
        "department": "INfra",
        "salary": 9000,
        "birth_date": "1991-01-01T02:00:00.000Z",
        "__v": 0
    },
    {
        "_id": "6258b3066f4786b217624c31",
        "name": "perycles Skywalker",
        "email": "perycles@ssys.com.br",
        "department": "INfra",
        "salary": 9000000,
        "birth_date": "1990-01-01T02:00:00.000Z",
        "__v": 0
    },
    {
        "_id": "6258c38dca6a1013e80dbd2f",
        "name": "pdsdss Skywalker",
        "email": "perxsd@ssys.com.br",
        "department": "INfra",
        "salary": 9000000,
        "birth_date": "1990-01-01T02:00:00.000Z",
        "__v": 0
    },
    {
        "_id": "625cbdae195853285e21fead",
        "name": "leia Skywalker",
        "email": "lqpwoe@ssys.com.br",
        "department": "Data",
        "salary": 4500,
        "birth_date": "1978-01-01T03:00:00.000Z",
        "__v": 0
    }
]

我需要如下所示的查询响应:

{
  "younger": {
    "id": "1",
    "name": "Anakin Skywalker",
    "email": "skywalker@ssys.com.br",
    "department": "Architecture",
    "salary": "4000.00",
    "birth_date": "01-01-1983"},
  "older": {
    "id": "2",
    "name": "Obi-Wan Kenobi",
    "email": "kenobi@ssys.com.br",
    "department": "Back-End",
    "salary": "3000.00",
    "birth_date": "01-01-1977"},
  "average": "40.00"
}

我通过 mongoose 与 mongoDb 建立的联系如下例所示:

const mongoose = require("mongoose");
require('dotenv').config();

const dataBase = () => mongoose.connect(
  process.env.URI,
  { useNewUrlParser: true, useUnifiedTopology: true },
  () => {
    console.log('Connected to MongoDB');
  }
);

module.exports = dataBase;

我的猫鼬模式如下:

const mongoose = require("mongoose");
const User = new mongoose.Schema({
    name: {
      type: String,
      required: true,
    },
    email: {
      type: String,
      required: true,
    },
    password: {
      type: String,
      required: true,
    },
    department: {
      type: String,
      required: true,
    },
    salary: {
      type: Number,
      required: true,
    },
    birth_date: {
      type: Date,
      required: true,
    },
  });

module.exports = mongoose.model("User", User);

我的模型层正在导入这样的连接文件:

const model = require("../models/app.model");

尽管如此,我还是无法自定义数据表单并对其进行排序。感谢您提供的任何帮助

db.getCollection('').find({}).sort({birth_date:-1})

这会将您的 collection 按 birth_date

降序排列

查找最大值或最小值然后过滤数组。

db.collection.aggregate([
  {
    $match: { message: "Employees list" }
  },
  {
    $project: {
      younger: {
        $first: {
          $filter: {
            input: "$result",
            as: "r",
            cond: { $eq: [ "$$r.birth_date", { $max: "$result.birth_date" } ] }
          }
        }
      },
      older: {
        $first: {
          $filter: {
            input: "$result",
            as: "r",
            cond: { $eq: [ "$$r.birth_date", { $min: "$result.birth_date" } ] }
          }
        }
      },
      avg: {
        $subtract: [
          { $year: "$$NOW" },
          {
            $year: {
              $toDate: {
                $avg: {
                  $map: {
                    input: "$result",
                    as: "r",
                    in: { $toLong: { $toDate: "$$r.birth_date" } }
                  }
                }
              }
            }
          }
        ]
      }
    }
  }
])

mongoplayground