mongodb操作

  1. 查询用户:db.system.users.find()

  2. 添加用户:db.addUser('admin', '1234')

  3. 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 

  4. 创建索引:db.user.ensureIndex({"name":1},{name:'indexname'})

  5. 查看索引:db.status.getIndexes()

  6. 查看具体信息:db.mytest.find().explain()

  7. 删除索引: 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位整数类型

     

原文地址:https://www.cnblogs.com/zhengze/p/10702123.html