db.getCollection('voiceBusiData').count()
db.getCollection('voiceBusiData').count({"content":"劉德華"})
複製代碼
查詢語句:編程
db.getCollection('voiceBusiData').aggregate([{$group:{_id:{"content":"$content","openId":"$openId"},sum:{$sum:"$times"}}}])
複製代碼
查詢結果:數組
/* 1 */
{
"_id" : {
"content" : "朋友",
"openId" : "56662cad"
},
"sum" : NumberLong(103)
}
/* 2 */
{
"_id" : {
"content" : "dj",
"openId" : "56662cad"
},
"sum" : NumberLong(107)
}
複製代碼
查詢語句:bash
db.getCollection('voiceBusiData').group({
"key":{"openId":true},
"initial":{"count":0},
"$reduce":function(cur,pre){
pre.count = pre.count + cur.times
}
})
複製代碼
查詢結果:分佈式
/* 1 */
[
{
"openId" : "589682",
"count" : 97.0
},
{
"openId" : "594770",
"count" : 1235.0
},
{
"openId" : "ALL",
"count" : 29530.0
},
{
"openId" : "56662d",
"count" : 28640.0
}
]
複製代碼
查詢語句:函數
db.getCollection('voiceBusiData').group({
"key":{"openId":true},
"initial":{"count":0},
"reduce":function(cur,pre){
pre.count = pre.count + cur.times
},
"condition":{"times":{$lt:20}}
})
複製代碼
查詢結果:工具
/* 1 */
[
{
"openId" : "58968e92",
"count" : 97.0
},
{
"openId" : "59477470",
"count" : 819.0
},
{
"openId" : "ALL",
"count" : 841.0
},
{
"openId" : "56662cad",
"count" : 815.0
}
]
複製代碼
mapReduce實際上是一種編程模型,用在分佈式計算中,其中有一個「map」函數,一個」reduce「函數。ui
① map:this
這個稱爲映射函數,裏面會調用emit(key,value),集合會按照你指定的key進行映射分組。spa
② reduce:code
這個稱爲簡化函數,會對map分組後的數據進行分組簡化,注意:在reduce(key,value)中的key就是emit中的key,vlaue爲emit分組後的emit(value)的集合,這裏也就是不少{"count":1}的數組。
③ mapReduce:
這個就是最後執行的函數了,參數爲map,reduce和一些可選參數。
查詢語句:
db.getCollection('voiceBusiData').mapReduce(
function(){
if(this.openId!="ALL"){
emit(this.openId,{count:this.times})
}
},
function(key, value){
var result = {count:0}
for(var i = 0; i<value.length; i++){
result.count += value[i].count
}
return result
},
{"out":"testMapReduce"}
)
複製代碼
out: 輸出表名爲testMapReduce
執行信息:
/* 1 */
{
"result" : "testMapReduce",
"timeMillis" : 472.0,
"counts" : {
"input" : 727,
"emit" : 486,
"reduce" : 18,
"output" : 3
},
"ok" : 1.0,
...
複製代碼
result: "存放的集合名「;
input:傳入文檔的個數。
emit:此函數被調用的次數。
reduce:此函數被調用的次數。
output:最後返回文檔的個數。
執行結果(testMapReduce表):
/* 1 */
{
"_id" : "56662d",
"value" : {
"count" : 2640.0
}
}
/* 2 */
{
"_id" : "589682",
"value" : {
"count" : 97.0
}
}
/* 3 */
{
"_id" : "594770",
"value" : {
"count" : 135.0
}
}
複製代碼
查詢一個集合中特定字段的全部不一樣值,返回的是一個不一樣值組成的數組
The command takes the following form
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>
}
複製代碼
eg:
db.runCommand({"distinct":"voiceTime","key":"vn"})
複製代碼
或者:
db.getCollection('voiceTime').distinct("vn")
複製代碼
db.getCollection('location').distinct("itineraryId", {"did":"23958957", "sts":{$gt:1524355200000,$lt:1524441600000}})
複製代碼
如批量更改recResultStaticsInfo表中的時間戳
db.getCollection('recResultStaticsInfo').find({"timestamp" : 1538323200000}).forEach(
function(item){
db.getCollection('recResultStaticsInfo').update({"_id":item._id},{$set:{"timestamp":"1541260800000"}})
}
)
複製代碼
局部更新
db.[collectionName].update({查詢器},{修改器})
db.getCollection('recResultStaticsInfo').update({"timestamp":1535558400000},{$set:{"test":"test"}})
複製代碼
insertOrUpdate操做
查詢器查出來數據就執行更新操做,查不出來就替換操做
db.[collectionName].update({查詢器},{修改器},true)
複製代碼
第三個參數設置爲true,表明insertOrUpdate,即存在即更新,不然插入該數據
批量更新操做
即添加第四個參數,該參數爲true,則批量更新,爲false,則更新一條
db.[collectionName].update({查詢器},{修改器},false, true)
複製代碼
注意上面的修改器
$set修改器
$set修改器用來指定一個鍵值對,若是存在鍵就進行修改不存在則進行添加。
// 修改器名稱:$set
// 語法:
{$set:{field: value}}
// example:
{$set:{name:"Redis"}}
複製代碼
導出mongo表數據
./mongoexport -h 172.31.3.18 --port 27017 -u username -p password --authenticationDatabase=admin -d auto_analyse_test -c accInfoDaily -f openId,accUser,actUserRate,ts,newUser,actUser -o ../accInfoDaily.csv
複製代碼
其中:
-h : host
--port : port
-u : username
-p : password
-d : database
-c : collection
-q : query condition
-f : field // 字段名稱,以","分隔
--type : csv // 導出格式爲csv
-o : outputpath
複製代碼