python调用mongodb

    安装

    测试Python驱动

    import pymongoclient=pymongo.MongoClient("127.0.0.1", 27017)#Getting a Database¶db=client.test_database#Getting a Collectioncollection = db.test_collectionpost={"name":"xwp1", "age":32}#Inserting a Documentpost_id = collection.insert_one(post).inserted_iddb.collection_names()post={"name":"xwp2", "age":33}post_id = collection.insert_one(post).inserted_idcollection.find_one()for cur in collection.find(): print curcur=collection.find()cur.next()cur.next()cur.next()print collection.count()collection.find({"name":"xwp"}).count()collection.find({"name":"xwp1"}).count()

    首先,插入一些数据进行聚合:

    可以使用方法 运行一个聚合explain方法:

    执行 explain=False