0

I wanted to use MongoVue to create a quick export of all the ids for portion of a given collection.

I have a document that has an _id field which is a composite key.

For example.

{
  "_id" : {
    "GroupID" : 3,
    "ThingyID" : 320486
  },
  "HowManyOwned" : 42,
  "IsAwesome" : true
}

I want to create an export of all the ThingyIDs for Group 3.

Of course if I make my query something like this.

db.GroupThingy.find({ "_id.GroupID" : 3 }, { "_id.ThingyID" : 1 })

I'll get back all the composite keys. I wanted to use MongoVue to just quickly create this export. If I use that query I get back an export of.

Document[2 Keys]
Document[2 Keys]
Document[2 Keys]

What I was hoping to get was

either

3,12345
3,3838
3,3777
3,1111

Or even better would be just

12345
3838
3777
1111

I could write a program for this but there has to be a quick way to accomplish this that I just don't know about.

Aggregation Framework doesn't help me get a csv export... but something like this will only support 20k documents

db.GroupThingy.group( 
{
    key: { 
            "_id.ThingyID": 1, 
            "_id.GroupID":1 
        }, 
    cond: { "_id.GroupID": 3 }, 
    reduce: function(curr, result){
            result.ThingyID2 = curr._id.ThingyID
        }, 
    initial: { "ThingyID2": 0 }
});
John Sobolewski
  • 4,512
  • 1
  • 20
  • 26

1 Answers1

1

Well I finally found out how to get the export using mongoexport thanks to this question: how to export collection to csv in mongodb

However I kept getting the error:

ERROR: too many positional options... and this post helped me find the answer: What does "too many positional options" mean when doing a mongoexport?

So my final solution to run the mongoexport was:

mongoexport 
     --host myHostName 
     --db theDB 
     --collection GroupThingy 
     --fields "_id.ThingyID" 
     --csv 
     --query "{'_id.GroupID':3}"

(options on there own lines for readability)

Community
  • 1
  • 1
John Sobolewski
  • 4,512
  • 1
  • 20
  • 26