0

I am new to the mongoose and Express and I am stuck with querying in Mongoose.

I need to get the users by filtering their NIC (key) and createdAt fields with greater than and less than keywords and for createdAt I need to compare it to a variable. The code is as follows.

const setData = asyncHandler(async (req, res) => {

const prevDate = new Date(Date.now() - 1000 * 86400).toISOString();

const nUser = new user({
    Name: req.body.name,
    Age: req.body.age,
    NIC: req.body.NIC,
    Sex: req.body.sex,
    Telephone: req.body.tel,
    Address: req.body.addr,
    email: req.body.email,
    Inquiry: req.body.inquiry,
    Branch:req.body.branch,
    askLoan:req.body.askLoan
})
// await nUser.save();
// console.log (nUser)

const userStored = await user.find({ NIC: nUser.NIC},**{createdAt: {$lte: prevDate}})**
console.log(userStored)

1 Answers1

0

I changed the line for userStored like this:

const userStored = await user
  .find({
    NIC: nUser.NIC
  })
  .where({
    createdAt: {
      '$gte': prevDate
    }
  })

And it worked. Turns out using a comma to add queries was not optimal and also the single commas for the $lte or $gte.

This successfully returned results to the console.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31