akm
akm

Reputation: 169

How to retrieve date from mongodb

I am trying to retrieve date as per input from user.The date is inserted in the format yyyy/mm/dd(Stored as string) in mongodb. I want to know how to retrieve the record for the date the user has specified.

I have tried breaking down the date into year,month and day and then tried searching but still cannot find a solution. In the below find() code if I am searching it is returning both the records but I need only the record with date "2019/11/12".

user.db

{
.
.
start_Date: "2019/11/12"
}

{
.
.
start_Date: "2019/12/12"
}

app.js

dbo
  .collection("testing")
  .find({ start_Date: { $regex: req.params.day, $options: "i" } })
  .toArray(function(err, n_date) {
    if (err) {
      reject(err);
    } else {
      console.log(n_date);
      resolve(n_date);
    }
  });

Not getting any error message.

Upvotes: 0

Views: 106

Answers (1)

mohammad javad ahmadi
mohammad javad ahmadi

Reputation: 2081

you should get data from client side equal to sting of date save it

  var dateCommingFromClient = req.params.completeDate 
  // completeDate is like this "2019/11/12" not day only
  var query = { start_Date: dateCommingFromClient };
  dbo.collection("testing").find(query).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });

Upvotes: 1

Related Questions