2

I need to load a list of dicts (see below) into a mongoDB. Within mongo, you have to define an int type as NumberInt(). Python doesn't recognize this as a valid type for a dict. I've found pages on custom encoding for pymongo that don't actually do what I need. I'm totally stuck. Someone has to have encountered this before!

Need to insert a list of dicts like this into mongoDB from python.

agg = {
    '_id' : unique_id_str,
    'total' : NumberInt(int(total)),
    'mode' : NumberInt(int(mymode)) 
}
debara
  • 357
  • 1
  • 2
  • 9

1 Answers1

3

You should be able to just insert the dict with an int, I've never needed to use NumberInt to insert documents using pymongo. Also, fwiw, folks at mongodb told me that letting mongo create the _id itself tends to be more efficient but obviously it may work better for you to define in your case.

agg = { 
    '_id' : unique_id_str,
    'total' : int(total),
    'mode' : int(mymode) 
}

should work

user3610360
  • 186
  • 1
  • 1
  • 9
  • What version of mongo? Unfortunately, this absolutely does not work for the version we are using (2.6). The answer may be, "upgrade mongo", which I have no control over. – debara Jul 29 '15 at 16:20
  • 1
    Unfortunately I'm running mongo 3 here. I am fairly sure I have done this in 2.6 but I can't test at the moment. What error are you getting? – user3610360 Jul 30 '15 at 19:19
  • This was fixed when we upgraded to mongo 3! Our dev environment was 2.4, prod was 2.6. I was running in dev, so it may have worked in 2.6, I don't know, but it's all good now. – debara Oct 08 '15 at 21:31