查詢用戶:db.system.users.find() 添加用戶:db.addUser('admin', '1234') mongodb導入csv數據 mongoimport -h localhost --port 27017 -u tor_tester -p 123456 -d torstatus ...
-
查詢用戶:db.system.users.find()
-
添加用戶:db.addUser('admin', '1234')
-
mongodb導入csv數據
mongoimport -h localhost --port 27017 -u tor_tester -p 123456 -d torstatus -c countrytocode -f country,ISO2 --file cc6.csv --type csv --ignoreBlanks
導出數據至csv
mongoexport -h localhost --port 27017 -u tor_tester -p 123456 -d torstatus -c countrytocode -f country,ISO2 -o cc6.csv
-
創建索引:db.user.ensureIndex({"name":1},{name:'indexname'})
-
查看索引:db.status.getIndexes()
-
查看具體信息:db.mytest.find().explain()
-
刪除索引: collection.dropIndex({xxx:1/-1})
======================================================================================
8.聚合查詢
db.status.group({
keyf : function(status){
var date = new Date(status.created.replace(/-/g, "/")); //字元串類型的時間轉為ISODate時間格式
var dateKey = ""+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
return {'day':dateKey};
},
initial : {"count":0},
reduce : function Reduce(status, out) {
if(status.ipaddress){
out.count +=1;
}
},
});
db.status.group({
keyf: function(status){
return {'platform':status.platform};
},
initial:{count:0},
reduce:function(status, out){
out.count++;
},
});
db.status.group({
key:{country_code:{"$ne":""}},
initial:{count:0},
reduce:function(status, out){
out.count++
},
});
db.status.aggregate([
{$match:{'platform':{$ne:""}}},
{$group: {
_id: "$platform",
count: {$sum: 1}}
},
{$sort: {"_id": 1}}
]);
db.status.aggregate([
{$match:{'country_code':{$ne:""}}},
{$group: {
_id: "$country_code",
count: {$sum: 1}}
},
{$sort: {"_id": 1}}
]);
db.status.aggregate([
{$match:{'exit_flag':{$ne:""}}},
{$group: {
_id: "$exit_flag",
count: {$sum: 1}}
},
{$sort: {"_id": 1}}
]);
var str ='2012-08-12 23:13:15';
str = str.replace(/-/g,"/");
var date = new Date(str);
======================================================================================
9.添加新欄位:
db.blocks.update({}, {$set: {'uncles':[]}}, {multi: 1})
======================================================================================
10. mongodb添加索引時,索引的名字跟mongoengine的models的ordering一致時,才有效。這樣做分頁時才能快速獲取每頁數據。
======================================================================================
11.
127.0.0.1:6379> flushall
(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
OK
======================================================================================
12.
db.transactions.find({"blockNumber":{$type:2}}).forEach(function(x){x.blockNumber=parseInt(x.blockNumber);db.transactions.save(x)})
======================================================================================
13.
1 Double 浮點型
2 String UTF-8字元串都可表示為字元串類型的數據
3 Object 對象,嵌套另外的文檔
4 Array 值的集合或者列表可以表示成數組
5 Binary data 二進位
7 Object id 對象id是文檔的12位元組的唯一 ID 系統預設會自動生成
8 Boolean 布爾類型有兩個值TRUE和FALSE
9 Date 日期類型存儲的是從標準紀元開始的毫秒數。不存儲時區
10 Null 用於表示空值或者不存在的欄位
11 Regular expression 採用js 的正則表達式語法
13 JavaScript code 可以存放Javasript 代碼
14 Symbol 符號
15 JavaScript code with scope
16 32-bit integer 32位整數類型
17 Timestamp 特殊語義的時間戳數據類型
18 64-bit integer 64位整數類型