I have created the following objects within a array in my local mongodb database. Having tried the following set up to retrieve the document by date I only get an empty array returned. Here is my code with schema. I am using the following modules in npm shortid,mongoose,moment,cors,bodyParser and express.
const mySchema = new Schema ({
_id:String,
username: String,
data: [{
_id:false,
description : String,
duration: Number,
date :{type:Date}
}]
});
"The user data object with dates of exercises to be completed cannot get date returned in query"
{
"count": 8,
"data": {
"_id": "bB6zWsJD6",
"username": "Timmy",
"data": [
{
"description": "walking",
"duration": 1,
"date": "2018-11-22T00:00:00.000Z"
},
{
"description": "walking",
"duration": 1,
"date": "2018-11-22T00:00:00.000Z"
},
{
"description": "walking",
"duration": 1,
"date": "2018-11-22T00:00:00.000Z"
},
{
"description": "walking",
"duration": 1,
"date": "2018-12-31T00:00:00.000Z"
},
{
"description": "walking",
"duration": 3,
"date": "2018-11-22T00:00:00.000Z"
},
{
"description": "walking",
"duration": 3,
"date": "2018-11-22T00:00:00.000Z"
},
{
"description": "walking",
"duration": 3,
"date": "2018-11-22T00:00:00.000Z"
},
{
"description": "walking",
"duration": 3,
"date": "2018-11-22T00:00:00.000Z"
}
],
"__v": 0
}
}
"using $gt greater than date in database and $lt less than date in database to query date in document"
app.get('/addme15',(req,res)=>{
let iniate = new Date(req.query.start);
let one = moment(iniate).format("YYYY-MM-DDT00:00:00.000") + "Z";
let date1 = new Date(one);
console.log(date1);
console.log(typeof(date1));
let iniate1 = new Date(req.query.end);
let two = moment(iniate1).format("YYYY-MM-DDT00:00:00.000") + "Z";
let date2 = new Date(two);
console.log(date2)
console.log(typeof(date2))
array.find({ "_id.username.data.date": { $gt:date1, $lt:date2 }}, function (err, docs) {
res.send(docs);
});
})