How to format the result of a mongodb find query?
Asked Answered
S

1

7

i have a collection that is storing data in this format :

{
    _id: ObjectId("51b9be6dbbdeef1e5f008cca"),
    name: 'sfdsfsdfsdfsdfsd'
    details: {
        varA: {
            created: "2013-06-13T12:43:25.853Z",
            validity: "2013-07-13T12:43:25.853Z",
            modified: "2013-06-13T12:43:25.853Z"
        },
        varB: {
            created: "2013-06-13T12:43:25.853Z",
            validity: "2013-07-13T12:43:25.853Z",
            modified: "2013-06-13T12:43:25.853Z"
        }
    }
}

I would like to be able to expose only the varA data in this format (without the nested depth...) :

{ 
    _id: ObjectId("51b9be6dbbdeef1e5f008cca"),
    name: 'sfdsfsdfsdfsdfsd',
    created: "2013-06-13T12:43:25.853Z",
    validity: "2013-07-13T12:43:25.853Z",
    modified: "2013-06-13T12:43:25.853Z"
}

Unfortunately, my query (wher i'm using projection) :

db.coll.find({}, {'details.varB': 0})

return something like this :

{
    _id: ObjectId("51b9be6dbbdeef1e5f008cca"),
    name: 'sfdsfsdfsdfsdfsd',
    details: {
        varA: {
            created: "2013-06-13T12:43:25.853Z",
            validity: "2013-07-13T12:43:25.853Z",
            modified: "2013-06-13T12:43:25.853Z"
        }
}

How can I improve the find query to return the expected format ?

Thanks a lot in advance for those who will help me ;-)

P.S. here i'm using the mongo shell to retrieve the data but i need to get this query working with node.js with node-mongodb-native.

Skimpy answered 13/6, 2013 at 13:55 Comment(1)
I'd suggest you consider doing that busy work on the client (in NodeJS) rather than having the DB server do extra work to project the data in a different format.Overgrow
M
6

I have done this with aggregate function only, as explained in my blog post here

For your case, this works

db.temp.aggregate (
   {
      $project : 
      {
         name:"$name",
         created:"$details.varA.created",
         validity:"$details.varA.validity",
         modified:"$details.varA.modified"
      }
   }
);

or

db.temp.aggregate ({$project:{name:"$name",created:"$details.varA.created",validity:"$details.varA.validity",modified:"$details.varA.modified"}});

This is the sample run

> db.temp.insert ({name:'sfdsfsdfsdfsdfsd', details: { varA : { created: "2013-06-13T12:43:25.853Z", validity: "2013-07-13T12:43:25.853Z", modified: "2013-06-13T12:43:25.853Z"}, varB : { created: "2013-06-13T12:43:25.853Z", validity: "2013-07-13T12:43:25.853Z", modified: "2013-06-13T12:43:25.853Z" } } })
> db.temp.aggregate ({$project:{name:"$name",created:"$details.varA.created",validity:"$details.varA.validity",modified:"$details.varA.modified"}});
{
        "result" : [
                {
                        "_id" : ObjectId("51b9d7151723a9c4d6bc9936"),
                        "name" : "sfdsfsdfsdfsdfsd",
                        "created" : "2013-06-13T12:43:25.853Z",
                        "validity" : "2013-07-13T12:43:25.853Z",
                        "modified" : "2013-06-13T12:43:25.853Z"
                }
        ],
        "ok" : 1
}
Margarito answered 13/6, 2013 at 14:31 Comment(2)
thanks @thefourtheye. This is what i was looking for. Is there a way to aggregate all other fields of this query? I have 20 more fields in the collection that i want to display in my result, should I list them all in the $project operator ?Skimpy
Yes. If you want to flatten or rename the title in the query result, you have to mention all the field names.Margarito

© 2022 - 2024 — McMap. All rights reserved.