0

I want to add the property type as billing to the result of a mongodb operation. I treid many answers from Is it possible to add dynamically named properties to JavaScript object?, like this one: https://stackoverflow.com/a/46512466/6655884 here:

    let r1 = await Billing.find({
      launchDate: {
        "$gte": new Date(req.query.minDate),
        "$lte": new Date(req.query.maxDate)
      },
      deleted: false,
      status: "pago"
    });

    r1.map((obj)=>({...obj,['type']:"billing"}));

But when I print r1, there's no type property.

UPDATE:

What about

for (var i=0; i<r1.length; i++) {
    r1[i]['type'] = "billing";
}

?

It doesn't work either

Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • Presuming *r1* is an array like `[{launchDate: {...}, deleted: false, status:'pago}]`, then the code "works" and does add a *type* property. But it's added to the array *returned* by `r1.map(...)`, the *r1* array itself isn't modified. – RobG Mar 27 '20 at 03:59
  • You can use an aggregation query to add a new field to the query result. The [aggregation stages](https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/) `$project` and `addFields` can be used for this. You can add this stage after your initial `$match` stage with the query filter. – prasad_ Mar 27 '20 at 04:01
  • @RobG I added and I don't know if it works. When I print now I get `[Object object ...]`. I also tried a new simpler way, can you look at my update? It also won't work. I see no changes in `r1` – Guerlando OCs Mar 27 '20 at 04:25
  • What does the print output from the variable `r1` look like immediately _after_ the `find`? – prasad_ Mar 27 '20 at 04:33
  • [JavaScript's map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) function works on an array. So use the [cursor's toArray](https://mongodb.github.io/node-mongodb-native/3.5/api/Cursor.html#toArray) on the `find` _and_ then apply the `map`. – prasad_ Mar 27 '20 at 04:42
  • In regard to "what about…" you can do `r1.forEach(item => item.type = 'billing')`. There's not much point in `['type']` for the property name, it's the same as `'type'`. – RobG Mar 27 '20 at 04:56

1 Answers1

1

That could possible be happening because of mongoose .find() might be resulting in raw MongoDB docs rather than javascript objects, try to use .lean() to convert result into Js object in order to manipulate it :

 let r1 = await Billing.find({
      launchDate: {
        "$gte": new Date(req.query.minDate),
        "$lte": new Date(req.query.maxDate)
      },
      deleted: false,
      status: "pago"
    }).lean();

    r1.map((obj)=>({...obj,['type']:"billing"}));

Or alternatively you can do it in query using aggregation-pipeline :

 let r1 = await Billing.aggregate([
  /** Filter docs with criteria */
  {
    $match: { }
  },
  /** Add new field to all returned docs */
  {
    $addFields: {
      type: "Billing"
    }
  }
]);
whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46