0

My collections contain documents with numerous lists of maps. E.g

{
  name : 'Ned',
  mapList : 
  [
   {
     type : 'DVD',
     sales : 100
    },
    {
      type : 'CD',
      sales : 200
    }
   ]
 }         

Is there a way to list the keys of the documents in the mapList attribute?

I've come across this problem before with the document itself and used map reduce as a solution following the answer listed here : MongoDB Get names of all keys in collection

Is it possible to use map reduce here or is there another way? Note* I can't change the my document structure

Edit - My current approach and output

mr = db.runCommand({
"mapreduce" : "entertainmentStats",
"map" : function() {
 for (var key in this.mapList) { emit(key, null); }
},
"reduce" : function(key, stuff) { return null; }, 
"out": "entertainmentStats" + "_keys"
})

The approach is identitcal to that of the answer listed in ly linked question except that I'm tring to perform the map reduce operation on the mapList attribute of the documents. My output keys are always null.

I've attempted the same approach with a single map element in a collection and it worked fine but it doesn't work for the array of maps

Community
  • 1
  • 1
Travis
  • 705
  • 6
  • 30
  • They are so similar, comparing the link you provide. What's the problem you met? – Wizard Oct 23 '14 at 14:12
  • What is the exact output you're looking for? – Explosion Pills Oct 23 '14 at 14:12
  • Let's just call this a duplicate until you can give a reasonable explanation as yo why it is not. You took the time to find the link. Take the time to explain why it does not solve your problem. Or just try it. – Neil Lunn Oct 23 '14 at 14:15
  • @Wizard Perhaps trhe solution is quite obvious and my novice map/reduce skills are the problem. Basically I tried multiple variations on the answer provided in the question but always ended up with null values at the end of the function. – Travis Oct 23 '14 at 14:16
  • @ExplosionPills The output I'm looking for in this example would be keys in the maps :['type','sales'] – Travis Oct 23 '14 at 14:17
  • Perhaps you can post what you've tried and someone can point out to help. – Wizard Oct 23 '14 at 14:21
  • @Wizard I've added my current approach to the question. – Travis Oct 23 '14 at 14:28
  • 1
    Replace the "map" function with this : function myMap() { for (var key in this.mapList) { var e = this.mapList[key]; if (e) { for (var x in e) { { emit(x, null); } } } } } – Wizard Oct 23 '14 at 14:38
  • Nice! that works. So it was my novice map/reduce skills that was the issue. If you put that down as answer I'll accept. – Travis Oct 23 '14 at 14:42
  • @Travis, It doesn't matter. :) Actually the link you provided has a good answer for this kind of question, at the bottom of that page. You can have a look at it if you would like to. – Wizard Oct 23 '14 at 14:48

0 Answers0