简体   繁体   中英

mongoose how to take dates between two

I have project - renting app. I need to decide if car is available in dates between date_finish and date_start (includes that dates). I have model - car and order. Orders have few fields: dateStart and dateFinish , carId How to take only these carIds that are available between start_date and stop_date (including these 2 days).

Car:

    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' }
)

Order:

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' }
)

Status:1 for new orders, 2 for orders durating now, 3 for archive/completed orders

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

That query doesn't work. Who can help me?

EDIT: I have to (firstly) take orders which: -date of start and date of end are greater than passed by user date of end -date of end is lower than passed by user date of start. In other words - I need cards available in days between passed dates.

Try pass dates this way: var orders_n = await Order.find({"status":{'$lte':"2"},$and[{"dateFinish":{ '$gte': new Date()},"dateStart":{ '$lte': new Date()}}]}).select('carID -_id').exec();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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