0

Scenario: I have a collection 'People' with following documents

       {
         "_id" : ObjectId("512bc95fe835e68f199c8686"),
         "name": "David",
         "age" : 78
       },
       { "_id" : ObjectId("512bc962e835e68f199c8687"),
         "name" : "Dave",
         "age" : 35
       }

When I query using following code from Node.js

db.articles.aggregate(
    { $match : { author : "Dave" } }
);

The output will be like:

       { "_id" : ObjectId("512bc962e835e68f199c8687"),
         "name" : "Dave",
         "age" : 35
       }

Issues: The above is just a sample of the actual scenario, I want the 'age' filed value to be embedded in double quotes i.e for above quoted example it should be like "age": "35".

That is full resultant document should be like following:

{ "_id" : ObjectId("512bc962e835e68f199c8687"),
     "name" : "Dave",
     "age" : "35"
   }

Consider I have huge number of documents how efficiently I can achieve the same to get the desired output?

Question: Can someone help out with bright and efficient way to achieve the same?

Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
  • 1
    It is not enclosed in quotes because it's data type is a number. If you want quotes around it, change the data type to a string. Why do you need it in quotes? – NilsH May 14 '13 at 08:28
  • Yaa, Its known(Ref. Question Title).. To answer your question, its really difficult for me.. To brief; So many dependencies are there.. And the ultimate thing is the field type has to be `number` and I should enclose the result value within `""`. Thanks for your time... – Amol M Kulkarni May 14 '13 at 08:32
  • But why? What is the problem in the first place? Anyway, maybe this can help: http://stackoverflow.com/questions/4973095/mongodb-how-to-change-the-type-of-a-field – NilsH May 14 '13 at 08:35
  • 1
    Change the data or add to your NodeJs code the conversion from number to a string as each document is retrieved. – WiredPrairie May 14 '13 at 10:47
  • @WiredPrairie: Thanks I did the same.. but I felt there may be more efficient way to do the same.. – Amol M Kulkarni May 14 '13 at 11:43
  • Getting the database to do data conversions isn't going to be more efficient than: a) converting the data to always be a `string` (which is what I think you mean when you say, 'quoted') b) fixing it in NodeJS. I'd personally do busy work like that on the receiving end rather than on the DB server. – WiredPrairie May 14 '13 at 12:20

0 Answers0