Tuesday 28 January 2020

Changing output of Mongoose database query

Hi, Is it possible to change the output of a mongoose query during the generation, or is it only possible to change the return value in the app? Sorry if I can't articulate this better, but here is an example of what I have.const router = require('express').Router(); let Dataset = require('../models/practice.model'); router.route('/').get((req, res) => { const query = { inProduction: true }; const projection = { questions: 1 }; Dataset.find(query, projection) .then(json => res.json(json)) .catch(err => res.status(400).json(err)); }); module.exports = router; Which returns something like[ { "_id": "5de6647978e18605844b28e3", "questions": [ { "q": [ "something" ], "a": [ "something else" ] }, ... I would like have the output in the following format, so that the object's _id value replaces the questions property key.[ { "5de6647978e18605844b28e3": [ { "q": [ "something" ], "a": [ "something else" ] }, ... I can return this at the query level with Mongoose, or do I need to create a new object based on the output of the query?Thanks.

Submitted January 28, 2020 at 07:56AM by hey__its__me__

No comments:

Post a Comment