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