Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use findOne() when you want the few fields in one object and other fields in the other object?

I am querying the database using findOne which will return just one document. Now, I want a few of the fields of that document in one object and the other fields in the other object, both of which are wrapped in a single object. For example I have a table called Bus with the following fields –

_id,
busNo,
city,
agency,
date,
ticketPrice,
helper,
driver,
totalCollection

My find one query returns one document but I want it in the below format –

existingAggr -  {
  "result": [
    {
      "_id": "630dcd0c652489bca1b319f7",
      "busNo": "123",
      "city": "32",
      "agency": "58",
      "date": "2022-08-29T00:00:00.000Z",
    }
  ],
  "aggregates": {
    "ticketPrice": 8,
    "totalCollection": 402,
    "helper": 0,
    "driver": 23,
  }
}

I want this either with single access of database, or we can do some javascript calculations to further bring my result in this manner, but I cant seem to reach the solution. Currently, I am using the below code –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const res = await Bus.findOne(
    { busNo, date },
    { 
     _id :1,
     busNo:1, 
     city:1,
     agency:1,
     date:1,
     ticketPrice:1,
     helper:1,
     driver:1,
     totalCollection:1
   }
  );

This would return all the fields in one.

>Solution :

Given a result, you can straightfowardly create new objects from your result.

const res = await BusDayWise.findOne(
    { ...filter },
    { session: mongoSession }
  );

const result = [
    {
      "_id": res._id,
      "busNo": res.busNo,
      "city": res.city,
      "agency": res/agency,
      "date": res.date,
    }
],
const aggregates = 
{
    "ticketPrice": res.ticketPrice,
    "totalCollection": res.totalCollection,
    "helper": res.helper,
    "driver": res.driver,
}

More advanced answer

You can have a function that retrieve only certain key from a dictionnary

function subDict(dict, keys){
   const newDict = {};
   keys.forEach(key => newDict[key] = dict[key]);
   return newDict 
}

test = {"a": 1, "b": 2, "c": 3}
keys = ["a", "c"];
newTest = subDict(test, keys); // {"a": 1; "c": 3}

So in your case

const result = subDict(res, ["_id", "busNo", "city","agency", "date"]);
const aggregates = subDict(res, ["ticketPrice", "totalCollection", "helper", "driver"]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading