Skip to content

Instantly share code, notes, and snippets.

@am-MongoDB
Created April 10, 2018 09:25
Show Gist options
  • Save am-MongoDB/e4f196e85f1946ed66480bce6616a94e to your computer and use it in GitHub Desktop.
Save am-MongoDB/e4f196e85f1946ed66480bce6616a94e to your computer and use it in GitHub Desktop.
The code to add a single user using the MongoDB query language
from pymongo import MongoClient
def addUser(database, user):
return database.customers.insert_one(user).inserted_id
client = MongoClient(port=27017)
db=client.store
# This is the object that the application would be working with anyway and
# so you wouldn't actually need to create it just to add it to the DB
customer = {
"name" : {
"first" : "Eartha",
"second" : "Thompson"
},
"address" : [
{
"location" : "home",
"number" : 23,
"street" : "Twin Pines",
"city" : "New York",
"state" : "New York",
"postalCode" : "O83 1F1"
},
{
"location" : "work",
"number" : 1,
"street" : "Holy Cross",
"city" : "New York",
"state" : "New York",
"postalCode" : "513 8U5"
}
],
"phone" : [
{
"location" : "mobile",
"number" : "+48-675-560-3029"
},
{
"location" : "work",
"number" : "+48-887-222-1234"
}
],
"email" : "eandrzejak0@yellowpages.com",
"annualSpend" : 916305.32,
"dob" : "1985-02-28 07:32:58",
"interests" : [
{
"interest" : "XML Schema Design"
},
{
"interest" : "Glazing"
}
]
}
customerId = addUser(db, customer)
print "CustomerId: ", customerId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment