example07.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import pymongo
  2. # BSON - Binary JSON - dict
  3. def main():
  4. # client = pymongo.MongoClient('mongodb://120.77.222.217:27017')
  5. client = pymongo.MongoClient(host='120.77.222.217', port=27017)
  6. db = client.zhihu
  7. pages_cache = db.webpages
  8. """
  9. pages_cache.insert_many([
  10. {'_id': 1, 'url': 'http://www.baidu.com', 'content': 'shit'},
  11. {'_id': 2, 'url': 'http://www.qq.com', 'content': 'another shit'},
  12. {'_id': 3, 'url': 'http://www.qfedu.com', 'content': 'biggest shit'}
  13. ])
  14. print(pages_cache.update({'_id': 5}, {'$set': {'content': 'hello, world!'}}, upsert=True))
  15. # page_id = pages_cache.insert_one({'url': 'http://www.baidu.com', 'content': '<html></html>'})
  16. # print(page_id.inserted_id)
  17. # print(pages_cache.remove({'url': 'http://www.baidu.com'}))
  18. print(pages_cache.find().count())
  19. for doc in pages_cache.find().sort('_id'):
  20. print(doc)
  21. """
  22. pages_cache.insert_one({
  23. 'url': 'http://www.baidu.com',
  24. 'content': 'bull shit!',
  25. 'owner': {
  26. 'name': 'Lee Yanhong',
  27. 'age': 50,
  28. 'idcard': '110220196804091203'
  29. }
  30. })
  31. if __name__ == '__main__':
  32. main()