1

I am trying to get pymongo to insert new documents which have id's in string format rather than ObjectId's. The app I am building integrates meteor and python and meteor inserts string id's so having to work with both string and Objectids adds complexity.

Example:

Meteor-inserted doc:

{
    "_id" : "22FHWpvqrAeyfvh7B"
}

Pymongo-inserted doc:

{
    "_id" : ObjectId("5880387d1fd21c2dc66e9b7d")
}
matt
  • 63
  • 1
  • 4

1 Answers1

1

You could just switch your Meteor app to insert ObjectIds instead of strings. Just use the idGeneration option property and set the value to MONGO.

Here is an example.

var todos = new Mongo.Collection('todos', {
  idGeneration: 'MONGO'
});

It is described in the Meteor docs here.

Or, if you want Meteir to keep strings and can't figure out how to configure Pymongo to store as strings, then you can do the approach described here to convert between ObjectIds and strings in Pymongo..

Community
  • 1
  • 1
jordanwillis
  • 10,449
  • 1
  • 37
  • 42
  • thanks Jordan! this helps. the approach you linked is working great! first, generate an ObjectId, then extract the string value, then insert into pymongo with an explicit "_id" field" str(ObjectId()) – matt May 17 '17 at 18:04