MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Estrai Decimal da Decimal128 con Mongoose - MongoDB

Metodo 1: .

usa toString() . Converte l'oggetto in stringa.

find((docs) => {
   let result = docs.map((doc) => {
       if(doc.open){
          doc.open = doc.open.toString();
       }

       if(doc.close){
          doc.close = doc.close.toString();
       }

       return doc;  
   });

    //send modified output
    res.json(result);
})

uscita come segue:-

/*
[
  {
    "open":  "86.13",
    "close": "85.64"
  },
]
*/

Metodo 2: Mongodb 4.0 sopra,

db.myCollection.aggregate([
  {$match:{
   //...
   //...
   }},


  { $addFields : {
        open: {"$toString" : "$open"},
        close : {"$toString" : "$close"},
    }},
]);