1

I have a document as follows:

var data={"john:"friend",
          "fruit":"banana",
           "tv":[{"livingroom":"led",
                   "bedroom":"lcd"
                   "fruit":"banana"}]}

and I am trying to return an array of all its unique keys as follows:

["john","fruit,livingroom,bedroom]

so I have the following code:

var mykeys=[];
database.find({},function(result){
    result.forEach(function(each){
         for (key in each){
             mykeys.push(key)
         };
    }});

But this returns a whole bunch of objects I don't need like:

[$__, isNew, errors, _doc, $__original_save, save, _pres, _posts....]

Is there anyway I can get rid of these keys which aren't in the document? I am aware of this mapreduce answer here MongoDB get the names of all the keys in a MongoDB collection but I do not know how to translate it into mongoose. AFAIK mongoose doesn't support runCommand.

Community
  • 1
  • 1
qts
  • 984
  • 2
  • 14
  • 25
  • 4
    Possible duplicate of [MongoDB Get names of all keys in collection](http://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection) – Bertrand Martel Jul 31 '16 at 13:38
  • @Bertrand Martel:I did look at that the solution but it seems too complicated for what I want. I don't need to find unique keys in a collection, just all keys in a document without the vituals. Also I'm not too sure how to implement mapreduce in mongoose. – qts Jul 31 '16 at 13:45
  • Do you mean you want to extract keys from one document (any document) from your collection ? – Bertrand Martel Jul 31 '16 at 13:51
  • @Bertrand Martel: Actually you're right I am kind of looking for the same thing! But I still don't know how to translate the answer into mongoose. – qts Jul 31 '16 at 14:26

3 Answers3

8

You have to use the _doc property as this contains your actual document. In addition, you can just use Object.keys to get a list of properties.

var mykeys;
database.findOne({}, function(result) {
    mykeys = Object.keys(result._doc);
});
str
  • 42,689
  • 17
  • 109
  • 127
  • This answer works, but I just realised that I have subarrays in my document, which seems to be messing this up :-(. So I have upvoted your answer anyway and edited by question. – qts Jul 31 '16 at 14:26
1

Not sure if you may still need it, but i manged to get it done in Nestjs and Mongoose using this code:

    async leadModel(): Promise<any> {
    const keys = this.leadsModel.findOne();
    return Object.keys(keys.schema.obj)
}

It's still not the best version of it and also throws me a warning that the property doesnt exist on my type, but it works perfectly. Returns only keys of the collection. If you remove Object.keys() it will also return nested keys.

Piotr S.
  • 21
  • 4
0

Adding to @str answer, don't forget to add error as first callback argument

  let tableKeys 
  TRow.findOne({}, (err, result) {
    tableKeys = Object.keys(result._doc);
  });

You might also want to delete '_id' and '__v' from your keys object

Ilya Izrailyan
  • 1
  • 1
  • 1
  • 1