как я могу получить вывод без получения _id - PullRequest
0 голосов
/ 09 ноября 2019

Следующий код;

router.get('/', async function (req, res, next) {
    //get the categories
    const categories = await Category.find();
    res.send(JSON.stringify(categories));
});

приводит к следующему:

[
   {
      "_id": "5dc408ae18e952bcfb20f46d",
      "bread": {
         "name": "Bread"
      },
      "dairy": {
         "name": "Dairy"
      },
      "vegetables": {
         "name": "Vegetables"
      },
      "Seasoning": {
         "name": "Seasoning and Spices"
      },
      "fruits": {
         "name": "Fruits"
      }
   }
]

Однако я не хочу включать _id. То, что я хочу, заключается в следующем:

[
   {
      "bread": {
         "name": "Bread"
      },
      "dairy": {
         "name": "Dairy"
      },
      "vegetables": {
         "name": "Vegetables"
      },
      "Seasoning": {
         "name": "Seasoning and Spices"
      },
      "fruits": {
         "name": "Fruits"
      }
   }
]

1 Ответ

0 голосов
/ 09 ноября 2019
//Just use projection for making it work
router.get('/', async function(req, res, next) {
    //get the categories
    const categories = await Category.find({}, { _id: 0 });
    res.send(JSON.stringify(categories));
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...