经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MongoDB » 查看文章
mongodb操作
来源:cnblogs  作者:大激动  时间:2019/4/15 8:55:07  对本文有异议
  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位整数类型

     

 

原文链接:http://www.cnblogs.com/zhengze/p/10702123.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号