0

I have a collection containing demographic data by zip code. It has 6 columns for every age group:

  • Total; Estimate; AGE # - #
  • Total; Margin of Error; AGE # - #
  • Male; Estimate; AGE # - #
  • Male; Margin of Error; AGE # - #
  • Female; Estimate; AGE # - #
  • Female; Margin of Error; AGE # - #

There are around 10 age groups, so there are 60 fields. And there are over 33,000 documents (33,000+ zipcodes). So there's a lot of data. Is there a way to remove all the margin of error columns using some sort of SQL-like "like"? Like remove any field "%Margin%".

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Eduardo
  • 45
  • 1
  • 9

2 Answers2

2

No, but you can get list of all fields as described in MongoDB Get names of all keys in collection, conditionally emit fields that match your criteria, and iterate the result to $unset each matched field.

Community
  • 1
  • 1
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
0

So I did doc = db.AgeSex.findOne(); to get all the columns.
And then I did:

for(key in doc){
    if(key.includes("Margin")){
        db.AgeSex.update({},{$unset:{ (""+key):""}});
    }
};



and I get SyntaxError: invalid property id

Eduardo
  • 45
  • 1
  • 9