Soumya Ranjan Dhar
Soumya Ranjan Dhar

Reputation: 21

Not able to query date using mongoose.find

When I am querying from cmd I am getting the result where dates are less than '2016-01-05', but when I am querying through mongoose it is failing to convert the date into the correct format rather in mongo server it is converting to $lte: new Date(1452384000000) thus not able to query.

I have searched StackOverflow for possible error but no help.I have also tried suing moment but no help either

using command line

db.prices.find({date:{$lt:'2016-01-10'}})

In server, the command looks like the above and I am getting the result.

below is the command for mongoose and the result is null

let timeFrame = (req,res)=>{

    var start = new Date(req.params.startDate);
    var end = new Date(req.params.endDate);    
    if (start < end)
    {
        PriceModel.find({'date':{$lte:end}}).exec((err,result)=>{
            console.log("Result is ",result);
        })

    }
    else if (start.valueOf()==end.valueOf()) {
        console.log("both are equal");

    }
    else{
        console.log("Please enter date in reverse way");    
    }

}

In mongoose server it looks like below
2019-04-28T19:13:52.543+0530 I COMMAND  [conn2] command slicePay.prices command: find { find: "prices", filter: { date:
{ $lte: new Date(1452384000000) } }, projection: {} } planSummary: COLLSCAN keysExamined:0 docsExamined:339507 cursorExh
austed:1 keyUpdates:0 writeConflicts:0 numYields:2652 nreturned:0 reslen:121 locks:{ Global: { acquireCount: { r: 5306 }
 }, MMAPV1Journal: { acquireCount: { r: 2653 } }, Database: { acquireCount: { r: 2653 } }, Collection: { acquireCount: {

values for test
{ "_id" : ObjectId("5cc558827a769d9983624219"), "date" : "2016-01-05 00:00:00", "symbol" : "WLTW", "open" : 123.43, "clo
se" : 125.839996, "low" : 122.309998, "high" : 126.25, "volume" : 2163600 }
{ "_id" : ObjectId("5cc558827a769d998362421a"), "date" : "2016-01-06 00:00:00", "symbol" : "WLTW", "open" : 125.239998,
"close" : 119.980003, "low" : 119.940002, "high" : 125.540001, "volume" : 2386400 }
{ "_id" : ObjectId("5cc558827a769d998362421b"), "date" : "2016-01-08 00:00:00", "symbol" : "WLTW", "open" : 115.480003,
"close" : 116.620003, "low" : 113.5, "high" : 117.440002, "volume" : 2006300 }
{ "_id" : ObjectId("5cc558827a769d998362421c"), "date" : "2016-01-07 00:00:00", "symbol" : "WLTW", "open" : 116.379997,
"close" : 114.949997, "low" : 114.93, "high" : 119.739998, "volume" : 2489500 }
{ "_id" : ObjectId("5cc558827a769d9983624314"), "date" : "2010-01-04", "symbol" : "A", "open" : 31.389999, "close" : 31.
300001, "low" : 31.13, "high" : 31.630001, "volume" : 3815500 }
{ "_id" : ObjectId("5cc558827a769d9983624315"), "date" : "2010-01-04", "symbol" : "AAL", "open" : 4.84, "close" : 4.77,
"low" : 4.66, "high" : 4.94, "volume" : 9837300 }
{ "_id" : ObjectId("5cc558827a769d9983624316"), "date" : "2010-01-04", "symbol" : "AAP", "open" : 40.700001, "close" : 4
0.380001, "low" : 40.360001, "high" : 41.040001, "volume" : 1701700 }
{ "_id" : ObjectId("5cc558827a769d9983624317"), "date" : "2010-01-04", "symbol" : "AAPL", "open" : 213.429998, "close" :
 214.009998, "low" : 212.380001, "high" : 214.499996, "volume" : 123432400 }
{ "_id" : ObjectId("5cc558827a769d9983624318"), "date" : "2010-01-04", "symbol" : "ABC", "open" : 26.290001, "close" : 2
6.629999, "low" : 26.139999, "high" : 26.690001, "volume" : 2455900 }
{ "_id" : ObjectId("5cc558827a769d9983624319"), "date" : "2010-01-04", "symbol" : "ABT", "open" : 54.189953, "close" : 5
4.459951, "low" : 53.919951, "high" : 54.559954, "volume" : 10829000 }
{ "_id" : ObjectId("5cc558827a769d998362431a"), "date" : "2010-01-04", "symbol" : "ACN", "open" : 41.52, "close" : 42.07
, "low" : 41.5, "high" : 42.200001, "volume" : 3650100 }
{ "_id" : ObjectId("5cc558827a769d998362431b"), "date" : "2010-01-04", "symbol" : "ADBE", "open" : 36.650002, "close" :
37.09, "low" : 36.650002, "high" : 37.299999, "volume" : 4710200 }
{ "_id" : ObjectId("5cc558827a769d998362431c"), "date" : "2010-01-04", "symbol" : "ADI", "open" : 31.790001, "close" : 3
1.67, "low" : 31.610001, "high" : 32.189999, "volume" : 2102700 }
{ "_id" : ObjectId("5cc558827a769d998362431d"), "date" : "2010-01-04", "symbol" : "ADM", "open" : 31.48, "close" : 31.46
9999, "low" : 31.33, "high" : 31.84, "volume" : 3472500 }
{ "_id" : ObjectId("5cc558827a769d998362431e"), "date" : "2010-01-04", "symbol" : "ADP", "open" : 43.54, "close" : 42.83
0002, "low" : 42.700002, "high" : 43.54, "volume" : 3930100 }
{ "_id" : ObjectId("5cc558827a769d998362431f"), "date" : "2010-01-04", "symbol" : "ADS", "open" : 65, "close" : 65.88999
9, "low" : 64.959999, "high" : 66, "volume" : 794300 }
{ "_id" : ObjectId("5cc558827a769d9983624320"), "date" : "2010-01-04", "symbol" : "ADSK", "open" : 25.610001, "close" :
25.67, "low" : 25.610001, "high" : 25.83, "volume" : 2228600 }
{ "_id" : ObjectId("5cc558827a769d9983624321"), "date" : "2010-01-04", "symbol" : "AEE", "open" : 28.030001, "close" : 2
7.76, "low" : 27.690001, "high" : 28.27, "volume" : 1299300 }
{ "_id" : ObjectId("5cc558827a769d9983624322"), "date" : "2010-01-04", "symbol" : "AEP", "open" : 35.099998, "close" : 3
4.939999, "low" : 34.799999, "high" : 36, "volume" : 4076600 }
{ "_id" : ObjectId("5cc558827a769d9983624323"), "date" : "2010-01-04", "symbol" : "AES", "open" : 13.38, "close" : 13.67
, "low" : 13.38, "high" : 13.7, "volume" : 4597600 }


Upvotes: 1

Views: 1133

Answers (2)

Sakthivel
Sakthivel

Reputation: 159

Use Moment npm to handle date.

Try this .

let query ={
     date:{
            $gte:moment('2016-01-10').startOf('day').toDate(),
            $lte:moment('2016-01-10').endOf('day').toDate(),
         }
}

Upvotes: 3

Wang Liang
Wang Liang

Reputation: 4434

Try with

 var start = new Date(req.params.startDate);
 var end = new Date(req.params.endDate);    

To

 var start = req.params.startDate;
 var end = req.params.endDate;

Why?:

I think, your mongodb, the column date 's data type is not Date, it can be string.
If, it's type is date, working well as MongoDB/Mongoose querying at a specific date?

Upvotes: 0

Related Questions