pymongo 使用测试
生活随笔
收集整理的這篇文章主要介紹了
pymongo 使用测试
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
>>> import pymongo
>>> uri = "mongodb://recall:123456@oceanic.mongohq.com:10062/must"
>>> client = pymongo.MongoClient(uri) #連接到數(shù)據(jù)庫
>>> db = client.must #選擇數(shù)據(jù)庫名
>>> db.collection_names #查看所有聚集名,相當(dāng)與show_tables
<bound method Database.collection_names of Database(MongoClient('oceanic.mongohq.com', 10062), u'must')>
>>> table = db.mytable #創(chuàng)建聚集
>>> count = table.count() #查看聚集中的數(shù)目
>>> count
0
>>> monster = {"name":"Dracule","occupation":"Blood Suker","tags":["vampire","teeth","bat"],"data":"19981010"}
>>> insert_id = table.insert(monster) #插入數(shù)據(jù)
>>> for monster_one in table.find():
... print monster_one
...
...
{u'occupation': u'Blood Suker', u'_id': ObjectId('53510fcad6ca3fb153c5681d'), u'data': u'19981010', u'name': u'Dracule', u'tags': [u'vampire', u'teeth', u'bat']}
>>> print table.find_one({"name":"Dracule"})
{u'occupation': u'Blood Suker', u'_id': ObjectId('53510fcad6ca3fb153c5681d'), u'data': u'19981010', u'name': u'Dracule', u'tags': [u'vampire', u'teeth', u'bat']}
可查看?http://docs.mongohq.com/languages/python.html
import os import datetime import pymongo from pymongo import MongoClient# Grab our connection information from the MONGOHQ_URL environment variable # (mongodb://linus.mongohq.com:10045 -u username -pmy_password) MONGO_URL = os.environ.get('MONGOHQ_URL') #connection = Connection(MONGO_URL) client = MongoClient(MONGO_URL)# Specify the database db = client.mytestdatabase # Print a list of collections print db.collection_names()# Specify the collection, in this case 'monsters' collection = db.monsters# Get a count of the documents in this collection count = collection.count() print "The number of documents you have in this collection is:", count# Create a document for a monster monster = {"name": "Dracula","occupation": "Blood Sucker","tags": ["vampire", "teeth", "bat"],"date": datetime.datetime.utcnow()}# Insert the monster document into the monsters collection monster_id = collection.insert(monster)# Print out our monster documents for monster in collection.find():print monster# Query for a particular monster print collection.find_one({"name": "Dracula"})?
?
轉(zhuǎn)載于:https://www.cnblogs.com/tk091/p/3673984.html
超強干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的pymongo 使用测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。