1. import pymongo
    2. mongo = pymongo.MongoClient("mongodb://127.0.0.1:27017/").nga
    3. # 写入一条 db.collection.insert_one({})
    4. mongo.ttt.insert_one({"_id": 1234, "users": "[1111,2222,33,4555]"})
    5. # 写入多条 db.collection.insert_many([{},{}...])
    6. mongo.ttt.insert_many([{"_id": 34, "users": "[1111,2222,33,4555]"}, {"_id": 44,"users": "[1111,333]"}])
    7. # 更新一条 db.colleciton.update_one({查询条件},{$修改器:{修改值}})
    8. mongo.ttt.update_one({"_id": 1234}, {"$set": {"users": "[1111,2222,33,4555]"}})
    9. # 更新多条 db.colleciton.update_many({查询条件},{$修改器:{修改值}})
    10. mongo.ttt.update_many({"_id": {"$lt": 100}}, {"$set":{"users": "[1111,2222,up]"}}) # 条件id小于100
    11. # 查询,1查询字段,0不查询字段; db.collection.find_one(查询条件)、 db.collection.find(查询条件)
    12. l1 = mongo.ttt.find_one({"_id": 1234}, {"users": 1})
    13. l1 = mongo.ttt.find({"_id": {“$lt":100}}, {"users": 1}) # 条件id小于100
    14. # 删除 db.colleciton.delete_one(查询条件)、db.colleciton.delete_many(查询条件)
    15. mongo.ttt.delete_one({"_id": 1234})
    1. 查询关键字:
    2. 1$and----并列查询
    3. db.collection.find({'$and':[{},{}...]})
    4. 2$or----或条件查询
    5. db.collection.find({'$or':[{},{}...]})
    6. 3$in----范围查询
    7. db.collection.find({field:{'$in':['',''...]}})
    8. 4$all----子集查询
    9. db.collection.find({field:{'$all':['',''...]}})
    10. 查询条件操作符:
    11. 1$lt----小于
    12. db.collection.find({field:{'$lt':value}})
    13. 2$gt----大于
    14. db.collection.find({field:{'$gt':value}})
    15. 3$eq----等于
    16. db.collection.find({field:{'$eq':value}})
    17. 4$lte----小于等于
    18. db.collection.find({field:{'$lte':value}})
    19. 5$gte----大于等于
    20. db.collection.find({field:{'$gte':value}})
    21. 6$ne----不等于
    22. db.collection.find({field:{'$ne':value}})
    23. 数据排序+跳跃+范围:
    24. 1sort(filed,pymongo.ASCENDING/pymongo.DESCENDING)----对查询结果升序/降序排序
    25. db.collection.find({}).sort()
    26. db.COLLECTION_NAME.find().sort({KEY:1,key2:-1}); ---- 1升序 -1降序
    27. db.COLLECTION_NAME.find().sort([(key, -1)]).limit(20)
    28. 2skip(num)----对查询结果进行跳跃取值
    29. db.collection.find({}).skip()
    30. 3limit(num)----对查询结果进行范围截取
    31. db.collection.find({}).limit()
    32. 4)优先级:sort>skip>limit,与使用时的顺序无关
    33. db.collection.find({}).sort().skip().limit()
    34. db.collection.find({}).limit().sort().skip()
    35. db.collection.find({}).skip().sort().limit()
    36. $修改器及$特殊用法:
    37. 1$set----修改某个字段的值
    38. db.colleciton.update_one({'name':'c','age':20},{'$set':{'hobby':['swim,dump','sing']}})
    39. 2$unset---删除字段
    40. db.colleciton.update_one({'$and':[{'name':'c'},{'age':20}]},{"$unset":{'hobby':[1,2]}})
    41. 3$inc----引用增加(先引用 后增加)
    42. db.colleciton.update_many({'name':{'$in':['d','e','f']}},{'$inc':{'age':2}})
    43. 4)针对数组操作:
    44. $push----在Array的最后一个位置中增加一个数据
    45. db.colleciton.update_one({'name':'b'},{'$push':{'hobby':['swim','sing']}})
    46. $pushAll----在Array的最后一个位置中增加多个数据
    47. mycol.update({'name':'c'},{'$pushAll':{'hobby':['sing','scrapy']}})#实际测试无法使用,报错:Unknown modifier: $pushAll
    48. $pull ----删除Array中的指定单个元素
    49. .update_one({'hobby':'run'},{'$pull':{'hobby':'eat'}})
    50. $pullAll ----删除Array中的指定多个元素
    51. mycol.update_many({'name':'b'},{'$pullAll':{'hobby':['swim','play']}})#实际测试每次只删除一个元素
    52. $pop----删除Array中的第一个或者最后一个元素 正数是倒序删除 负数是正序删除
    53. db.colleciton.update_many({'hobby':'run'},{'$pop':{'hobby':1}})
    54. $----存储当前(Array)符合条件的元素下标索引 ,只能存储最外层的 索引位置
    55. db.colleciton.update_many({'hobby':'run'},{'$set:{'hobby.$':'swim'}})