Skip to content

Instantly share code, notes, and snippets.

View jason-w's full-sized avatar

Jason Wang jason-w

  • https://www.truevault.com
  • San Francisco, CA
View GitHub Profile
@jason-w
jason-w / gist:4969476
Created February 17, 2013 00:47
Helper functions that converts a MongoEngine Document to a Python Dict so it can be easily converted to JSON.
class Person (db.Document):
name = db.StringField(required=True)
created_date = db.ComplexDateTimeField(default=datetime.datetime.utcnow(), required=True)
def to_dict(self):
return helper.mongo_to_dict(self,[])
#helper.py
def mongo_to_dict(obj, exclude_fields):
@jason-w
jason-w / gist:4973476
Created February 17, 2013 21:06
Convert document field type in MongoDB.
// list of $type values: http://docs.mongodb.org/manual/reference/operator/type/
db.person.find( { 'integer_id' : { $type : 2 } } ).forEach( function (x) {
x.integer_id = new NumberLong(x.integer_id); // convert field to Long
db.person.save(x);
});