1. 程式人生 > >pymongo 常用方法

pymongo 常用方法

reg 如果 code pre 列表 recommend 註意 error: lec

setting設置

STORAGE = {
  # 遠程服務器地址
# ‘MONGODB‘: {‘host‘: ‘47.98.100.240‘, ‘port‘: 27017},
# 本地mongo測試地址
‘MONGODB‘: {‘host‘: ‘192.168.0.190‘, ‘port‘: 27017},

    # 本地redis測試地址
  ‘REDIS_CONTENT‘: {‘host‘: ‘192.168.0.190‘, ‘port‘: 6379, ‘db‘: 0},
}
# 項目使用的數據庫
DATABASE_CONTENT = "dbname"

models設置

import pymongo
import redis
import settings

mongo_client = pymongo.MongoClient(**settings.STORAGE[‘MONGODB‘])
db = mongo_client[settings.DATABASE_CONTENT]

增加新文檔:

models.db.collectionname1.insert_one(con_dict)  # con_dict 為組織好數據格式的字典

對查詢結果集排序:
models.db.collectionname1.find({}).sort("updated_at",pymongo.DESCENDING) # 倒敘排列

更新滿足條件的字段字段find:

models.db.collectionname1.find_one({"rank_id": 1}).get("content_id")


條件查詢id在已知的列表(recommend_list=[1,2,5]):
models.db.collectionname1.find({"id": {"$in": recommend_list}})

正則匹配名稱,不區分大小寫,結果不展示 "_id"字典
models.db.collectionname.find({"name":{"$regex":content_name, "$options":‘i‘}}, {"_id": 0})
查詢字段不等於1的數據
models.db.collectionname.find({"status":{"$ne":1}})
查詢集結果的數目:
models.db.collectionname.count()


更新滿足條件的字段字段update
models.db.collectionname.update({"rank_id":1},{"$pullAll":{"content_id":del_list}})

db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一條記錄
db.test0.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 全更新了
db.test0.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 只加進去了第一條
db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全加進去了
db.test0.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );全更新了
db.test0.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );只更新了第一條

1) $inc


用法:{ $inc : { field : value } }

意思對一個數字字段field增加value,例:

> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 16, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $inc : { "count" : 1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 17, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $inc : { "count" : 2 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 19, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $inc : { "count" : -1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "TESTTEST", "test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }

2) $set

用法:{ $set : { field : value } }

就是相當於sql的set field = value,全部數據類型都支持$set。例:
> db.test0.update( { "_id" : 15 } , { $set : { "test1" : "testv1","test2" : "testv2","test3" : "testv3","test4" : "testv4" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "testv1", "test2" : "testv2", "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }
3) $unset

用法:{ $unset : { field : 1} }

顧名思義,就是刪除字段了。例:
> db.test0.update( { "_id" : 15 } , { $unset : { "test1":1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test2" : "testv2", "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $unset : { "test2": 0 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $unset : { "test3":asdfasf } } );
Fri May 14 16:17:38 JS Error: ReferenceError: asdfasf is not defined (shell):0

> db.test0.update( { "_id" : 15 } , { $unset : { "test3":"test" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test4" : "testv4", "test5" : "OK" }

沒看出field : 1裏面的1是幹什麽用的,反正只要有東西就行。

4) $push
用法:{ $push : { field : value } }

把value追加到field裏面去,field一定要是數組類型才行,如果field不存在,會新增一個數組類型加進去。例:

> db.test0.update( { "_id" : 15 } , { $set : { "test1" : ["aaa","bbb"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb" ], "test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $push : { "test1": "ccc" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc" ], "test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $push : { "test2": "ccc" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc" ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $push : { "test1": ["ddd","eee"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }

5) $pushAll

用法:{ $pushAll : { field : value_array } }

同$push,只是一次可以追加多個值到一個數組字段內。例:

> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $pushAll : { "test1": ["fff","ggg"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc", [ "ddd", "eee" ], "fff", "ggg" ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }

6) $addToSet

用法:{ $addToSet : { field : value } }

增加一個值到數組內,而且只有當這個值不在數組內才增加。例:
> db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
        "aaa",
        "bbb",
        "ccc",
        [
                "ddd",
                "eee"
        ],
        "fff",
        "ggg",
        [
                "111",
                "222"
        ],
        "444",
        "555"
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
        "aaa",
        "bbb",
        "ccc",
        [
                "ddd",
                "eee"
        ],
        "fff",
        "ggg",
        [
                "111",
                "222"
        ],
        "444",
        "555"
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"]  } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
        "aaa",
        "bbb",
        "ccc",
        [
                "ddd",
                "eee"
        ],
        "fff",
        "ggg",
        [
                "111",
                "222"
        ],
        "444",
        "555",
        [
                "444",
                "555"
        ]
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"]  } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
        "aaa",
        "bbb",
        "ccc",
        [
                "ddd",
                "eee"
        ],
        "fff",
        "ggg",
        [
                "111",
                "222"
        ],
        "444",
        "555",
        [
                "444",
                "555"
        ]
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }


7) $pop

刪除數組內的一個值

用法:
刪除最後一個值:{ $pop : { field : 1  } }
刪除第一個值:{ $pop : { field : -1  } }

註意,只能刪除一個值,也就是說只能用1或-1,而不能用2或-2來刪除兩條。mongodb 1.1及以後的版本才可以用,例:
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
        "bbb",
        "ccc",
        [
                "ddd",
                "eee"
        ],
        "fff",
        "ggg",
        [
                "111",
                "222"
        ],
        "444"
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $pop : { "test1": -1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
        "ccc",
        [
                "ddd",
                "eee"
        ],
        "fff",
        "ggg",
        [
                "111",
                "222"
        ],
        "444"
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $pop : { "test1": 1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4",
"test5" : "OK" }

8) $pull

用法:$pull : { field : value } }

從數組field內刪除一個等於value值。例:
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", "ggg", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4",
"test5" : "OK" }

> db.test0.update( { "_id" : 15 } , { $pull : { "test1": "ggg" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5"
 : "OK" }

9) $pullAll

用法:{ $pullAll : { field : value_array } }

同$pull,可以一次刪除數組內的多個值。例:
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [ "ddd", "eee" ], "fff", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5"
 : "OK" }

> db.test0.update( { "_id" : 15 } , { $pullAll : { "test1": [ "ccc" , "fff" ] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ [ "ddd", "eee" ], [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }

pymongo 常用方法