0

How to get only certain values ​​from an array?

module.exports.getAll = async function (req, res) {
    try {
        const cards = await Card.find({})
        res.status(200).json(cards)
    } catch (e) {
        errorHandler(res, e)
    }
}

I am currently displaying an array in this form:

[
    {
        "ncard": "123123",
        "name": "sdf",
        "lim": "0",
        "ost": "0",
        "message": " ",
        "status": "1",
        "_id": "5f030111f5df83755eb65721",
        "date": "2020-07-06T10:46:41.001Z",
        "__v": 0
    },
...
]

But I want to receive in this form:

   [
        {
            "ncard": "123123",
            "name": "sdf",
            "_id": "5f030111f5df83755eb65721",
            "date": "2020-07-06T10:46:41.001Z",
            "__v": 0
        },
    ...
    ]

How can I do that?

  • Does this answer your question? [mongo .find return specific field only for all user](https://stackoverflow.com/questions/33982034/mongo-find-return-specific-field-only-for-all-user) – ggorlen Jul 21 '20 at 04:36

2 Answers2

1

try this line

  module.exports.getAll = async function (req, res) {
        try {
            const cards = await Card.find({}).project({ ncard: 1, name: 1, date: 1})
            res.status(200).json(cards)
        } catch (e) {
            errorHandler(res, e)
        }
    }
vishnusaran
  • 139
  • 1
  • 9
-1

you can try this.
await Card.find({}).project({ ncard: 1, name: 1, date: 1})