Reputation: 181
How to fetch the data of particular date in mongoose?
const orderSchema = new Schema({
foods: [
{
food: { type: Object, required: true },
quantity: { type: Number, required: true }
}
],
shopId:{
type: Schema.Types.ObjectId,
required: true,
}
},{ timestamps: { createdAt: 'created_at' } });
I have tried the following but it does not work
const dat=new Date(2020,07,23);
const ndat=new Date(2020,07,24);
const sid=req.shop._id;
Order.find({shopId:sid,created_at: {$gte: dat, $lt: ndat}})
.then(data=>{
res.send(data);
})
I need to fetch the order details of particular date.
Upvotes: 1
Views: 246
Reputation: 14490
MongoDB stores timestamps, not dates. Ensure the date you are passing is converted to timestamps that cover your order, and ensure that your order has the creation timestamp that you think it has.
Use mongo shell to find out what is actually stored in your database, and get the queries working in mongo shell first before trying to do them in your application.
Upvotes: 0
Reputation: 286
It seems that you have a syntax error:
const dat=new Date(2020,07,23);
const ndat=new Date(2020,07,24);
const sid=req.shop._id;
Order.find({shopId:sid,created_at: {"$gte": dat, "$lt": ndat}})
.then(data=>{
res.send(data);
})
OR this also works
const sid=req.shop._id;
Order.find({shopId:sid,created_at: {"$gte": new Date(2020,07,23), "$lt": new Date(2020,07,24)}})
.then(data=>{
res.send(data);
})
Upvotes: 1