首页 > 代码库 > pymongo

pymongo

import sys
import pymongo

from pymongo import Connection
from pymongo.errors import ConnectionFailure

def main():

  try:
    conn = Connection(host="localhost", port=27017)
 
  except ConnectionFailure, e:
    sys.stderr.write("connection failure: %s" % e)
    sys.exit(1)

  db = conn["books"]

  book_doc = {
    "ISDN" : "121",
    "Name" : "Art of War"

  }

  db.books.insert(book_doc, safe=True)

  print "insert doc: %s" % book_doc


if __name__ == "__main__":
  main()


> db.books.find()
{ "_id" : ObjectId("5466b112a616540bf5a1507b"), "Name" : "Art of War", "ISDN" : "121" }


pymongo