Tried to get records from mongodb between including two days but in my code only i am getting between two days.
Example:
01-06-2020 to 08-06-2020 = getting records from 02-06-2020 to 08-06-2020(01-06-2020 missing)
But i want 01-06-2020 to 08-06-2020 = need to get records from 01-06-2020 to 08-06-2020.
How to get it?
Mongodb Data:
{
_id:ObjectId("5edd1df67b272e2d4cf36f70"),
pname:"Test 1",
category:"Choco 1",
todaydate:2020-06-01T18:30:00.000+00:00
},
{
_id:ObjectId("5gdd1df67b272e2d4cf36f72"),
pname:"Test 2",
category:"Choco 3",
todaydate: 2020-06-02T18:30:00.000+00:00
},
{
_id:ObjectId("5kdd1df67b272e2d4cf36f74"),
pname:"Test 5",
category:"Choco 6",
todaydate: 2020-05-01T18:30:00.000+00:00
},
{
_id:ObjectId("5ewd1df67b272e2d4cf36f75"),
pname:"Test 6",
category:"Choco 8",
todaydate: 2020-06-03T18:30:00.000+00:00
},
{
_id:ObjectId("5sdd1df67b272e2d4cf36f76"),
pname:"Test 3",
category:"Choco 9",
todaydate: 2020-06-04T18:30:00.000+00:00
},
{
_id:ObjectId("5tdd1df67b272e2d4cf36f78"),
pname:"Test 11",
category:"Choco 10",
todaydate: 2020-06-05T18:30:00.000+00:00
}
data.model.js:
const mongoose = require('mongoose');
var userSchemaDate = new mongoose.Schema({
pname: {
type: String
},
category: {
type: String
},
todaydate: {
type: Date
}
}, {
versionKey: false,
collection: 'data'
});
module.exports = mongoose.model('Data', userSchemaDate);
data.controller.js:
module.exports.getReportTableData = (req, res, next) => {
var collection = req.query.collection;
let tableReportdata = dbc.model(collection);
let date1 = "01-06-2020"; dd/mm/yyyy
let date2 = "07-06-2020"; dd/mm/yyyy
tableReportdata.find({
$and: [{
todaydate: {
$gt: date1
}
},
{
todaydate: {
$lt: date2
}
}
]
}, function(err, docs) {
if (err) {
console.log(err);
return;
} else {
console.log("Successful loaded report data");
res.json({ data: docs, msg: 'Report data loaded.' });
}
});
}
The answer on your other question should return the correct result. I'll also emphasise that it's better to store the date as date object.
Let's try another approach by using $dateFromString
on the input values as well.
tableReportdata.find({
$expr: {
$and: [
{
$gte: [
{
$dateFromString: {
dateString: "$todaydate",
format: "%d-%m-%Y",
timezone: "UTC"
}
},
{
$dateFromString: {
dateString: "01-06-2020",
format: "%d-%m-%Y",
timezone: "UTC"
}
}
]
},
{
$lte: [
{
$dateFromString: {
dateString: "$todaydate",
format: "%d-%m-%Y",
timezone: "UTC"
}
},
{
$dateFromString: {
dateString: "07-06-2020",
format: "%d-%m-%Y",
timezone: "UTC"
}
}
]
}
]
}
}, function(err, docs) {
if (err) {
console.log(err);
return;
} else {
console.log("Successful loaded report data");
res.json({ data: docs, msg: 'Report data loaded.' });
}
});
Shorter version with a helper function
const dateUTCexpr = (dateString) => ({
$dateFromString: {
dateString,
format: "%d-%m-%Y",
timezone: "UTC"
}
})
tableReportdata.find({
$expr: {
$and: [
{
$gte: [dateUTCexpr("$todaydate"), dateUTCexpr("01-06-2020")]
},
{
$lte: [dateUTCexpr("$todaydate"), dateUTCexpr("07-06-2020")]
}
]
}
}, function(err, docs) {
if (err) {
console.log(err);
return;
} else {
console.log("Successful loaded report data");
res.json({ data: docs, msg: 'Report data loaded.' });
}
});
If you have todaydate
defined as String
in your schema, also make sure that it's properly converted in your database, you can use the following code
const dateUTCexpr = (dateString) => ({
$dateFromString: {
dateString,
format: "%d-%m-%Y",
timezone: "UTC"
}
})
tableReportdata.find({
todaydate: {
$gte: dateUTCexpr("01-06-2020"),
$lte: dateUTCexpr("07-06-2020")
}
}, function(err, docs) {
if (err) {
console.log(err);
return;
} else {
console.log("Successful loaded report data");
res.json({ data: docs, msg: 'Report data loaded.' });
}
});
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.