‘$set’: replace the value of a field with the specified value 更新新的valuepython
假設咱們有一個 products collection, 其中的字段以下正則表達式
{ _id: 100, sku: "abc123", quantity: 250, instock: true, reorder: false, details: { model: "14Q2", make: "xyz" }, tags: [ "apparel", "clothing" ], ratings: [ { by: "ijk", rating: 4 } ] }
db.products.update( { _id: 100 }, { $set: { quantity: 500, details: { model: "14Q3", make: "xyz" }, tags: [ "coats", "outerwear", "clothing" ] } } )
須要使用.
進行修飾的mongodb
db.products.update( { _id: 100 }, { $set: { "details.make": "zzz" } } )
db.products.update( { _id: 100 }, { $set: { "tags.1": "rain gear", "ratings.0.rating": 2 } } )
'$and': 邏輯與符號express
{ $and : [ {<expression1>}, {<expression2>}, ... {<expressionN>}]}
$or
: 邏輯或符號數組
{ $or : [ {<expression1>}, {<expression2>}, ... {<expressionN>}]}
多重判斷的寫法網絡
db.inventory.find( { $and : [chaxun { $or : [ { price : 0.99 }, { price : 1.99 } ] }, { $or : [ { sale : true }, { qty : { $lt : 20 } } ] } ] } )
CRUD 命令app
Create:
Retrieve:
Update: Supplier.update({'id': mer_supplier.id}, {'$set': {'bound_status': mer_supplier.bound_status}})
Delete:ide
直接給出字符, 就是精確匹配,
\d
匹配數字
\w
匹配一個字母或數字
\s
匹配空格
00\d
匹配 007
.
能夠匹配任意字符
*
表示任意個字符(0個或0個以上)
+
表示至少一個字符
?
表示0個或1個字符
{n}
表示n個字符
{n, m}
表示n-m個字符
|
表示或關係
[]
表示範圍 [0-9]
數字0-9, [0-9a-z]
數字0到9以及字母a到字母z均可以匹配url
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832260566c26442c671fa489ebc6fe85badda25cd000
{"$regex":r"[0-9a-z]{24}"}
spa
"_id": "text": "entities": { "user_mentions": [ { "screen_name": : "", ... } ], "urls": [], "hashtags": [] }, "user「: { "friends_count" : 544, "screen_name": "", "follwers_count" : 100, ... },
$group :
Example: Who Tweeted Most?
Answer:
* Group tweets by user
* Count each user's tweets
* Sort into descending order
* Select user at top
def most_tweets(): result = db.tweets.aggregate([ { "$group" : {"_id" : "$user.screen_name", "count" : { "$sum" : 1 } } } , # 合併全部_id 名字爲 user.screen_name的字段,並在每次遇到一樣字段的時候加一 { "sort" : { "count" : -1 } } ] ) return result
\(project*: shape documents you receive as input into what you need for the next stage *\)match: filter the document, 選出想要的document
* Example Question: Who has the highest followers to friends ratio?
def highest_ratio(): result = db.tweets.aggregate([ { "$match" : { "user.friends_count" : {"$gt" : 0}, "user.followers_count" : {"$gt" : 0 } } }, { "$project" : { "ratio" : { "$divide" : ["$user.followers_count", "$user.friends_count"]}, "screen_name" : "$user.screen_name"} }, { "$sort" : { "ratio" : -1 } }, { "$limit" : 1 } ]) return result
\(skip*: 直接跳過某些document, *\)limit: 和skip做用相反, 直接劃定向下繼續傳入的文檔數量
$unwind: 把數組的元素抽離出來, 方便根據數組元素的來進行聚合等進一步操做
Example Question: Who included the most user mentions?
def user_mentions(): result = db.tweets.aggregate([ { "$unwind" : "$entities.user_mentions" }, { "$group" : { "_id" : "$user.screen_name", "count" : { "$sum" : 1 } } }, { "$sort" : { "count" : -1 } }, { "$limit" : 1 } ] ) return result
全部的query 語句都是在server side執行, 只有最後的結果會經過網絡傳輸返回
BSON: binary encode of JSON
Python Application Pymongo -> MongoDB 使用Pymongo來進行數據的存取和操做