JS24
JS24

Reputation: 458

mongodb find specific column and change timestamp to date

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...

Upvotes: 0

Views: 25

Answers (1)

lpizzinidev
lpizzinidev

Reputation: 13289

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' },
    },
  },
]);

Upvotes: 1

Related Questions