首页 > 解决方案 > 猫鼬如何在两个之间约会

问题描述

我有项目 - 租赁应用程序。我需要确定在 date_finish 和 date_start 之间的日期(包括该日期)是否有汽车可用。我有模型 - 汽车和订单。订单的字段很少:dateStart以及dateFinishcarId 如何仅获取 start_date 和 stop_date 之间(包括这 2 天)之间可用的这些 carId。

车:

    mark:{
        type: String,
        required: true,},
    model:{
        type: String,
        required: true,},
    price:{
        type: Number,
        required: true,},
    available: {
        type: Boolean,
        required: true,},
    pic_1:{
        type:String,
        required:true,
    },
    pic_2:{
        type:String,
        required:true,
    },


},
{ collection: 'cars' }
)

命令:

const orderSchema = new mongoose.Schema(
    {
        userID: { type: String, required: true },
        carID: { type: String, required: true },
        status: {type:String,required:true},
        dateStart: {type:Date,required:true},
        dateFinish:{type:Date,required:true},
        total: {type:Number,required:true},

    },
    { collection: 'orders' }
)

状态:1 个新订单,2 个当前订单,3 个存档/已完成订单

猫鼬查询: var orders_n = await Order.find({"status":{'$lte':"2"},$and[{"dateFinish":{ '$lte': start},"dateStart":{}}]"dateFinish":{ '$lte': start}}).select('carID -_id').exec();

该查询不起作用。谁能帮我?

编辑:我必须(首先)接受以下订单: - 开始日期和结束日期大于用户结束日期所传递的 - 结束日期低于用户开始日期所传递的。换句话说 - 我需要在过去日期之间的几天内可用的卡片。

标签: javascriptnode.jsmongodbmongoose

解决方案


以这种方式尝试通过日期:var orders_n = await Order.find({"status":{'$lte':"2"},$and[{"dateFinish":{ '$gte': new Date()},"dateStart":{ '$lte': new Date()}}]}).select('carID -_id').exec();


推荐阅读