Home > Mobile >  mongodb find specific column and change timestamp to date
mongodb find specific column and change timestamp to date

Time:11-03

I try to query data from mongodb with spesific column, at first it returns all the value and columns i need but then when i try to change the value of the timestamp to date it shows error like this

Options must be an object, got \"_id formId title username date createdAt\"

Here is my code

 const answers = await Answer.find(
        { userId: req.params.userId },
        { $toDate: "createdAt" },
        "_id formId title username date createdAt"
      );

i wonder where did i do wrong here...

CodePudding user response:

Try using aggregate:

const answers = await Answer.aggregate([
  { $match: { userId: req.params.userId } },
  {
    $project: {
      formId: 1,
      title: 1,
      username: 1,
      date: 1,
      createdAt: { $toDate: '$createdAt' },
    },
  },
]);
  • Related