MongoDB的简单使用


使用python向集合test中插入1000条文档,文档包含_id和name
_id的值为0,1,2,3,...999
name的值为py0,py2,py4,py6,py8..
 查询显示出_id为100整数的倍数的文档, 如100,200,300
 1 from pymongo import MongoClient
 2 client = MongoClient()
 3 collection = client['neusoft']['test']
 4 l = [{'_id':i,"name":'py{}'.format(i*2)} for i in range(1000)]
 5 collection.insert_many(l)
 6 c = collection.find()
 7 for i in c:
 8     if i['_id'] % 100 == 0 and i['_id'] != 0:
 9         print(i)
10 client.close()

终端可使用:

db.test.find({$where:function(){
return this._id % 100 == 0 && this._id != 0
}})



posted @ 2019-10-28 19:14  一心取信  阅读(208)  评论(0)    收藏  举报