博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB 常用shell命令汇总
阅读量:6878 次
发布时间:2019-06-26

本文共 1191 字,大约阅读时间需要 3 分钟。

//指定用户名和密码连接到指定的MongoDB数据库

mongo 192.168.1.200:27017/admin -u user -p password

use youDbName

 

1.MongoDB 查询所有索引的命令:

 

2.MongoDB 创建索引的命令:

Unique Index:

注:

  MongoDB无法创建指定的索引字段唯一索引(S)如果集合已经包含了将违反唯一性约束的数据指标。

  MongoDB cannot create a  on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index.

       

解决方法:

  1.把集合数据导出(mongoexport)至csv文件备份。

  2.清空集合数据。

  3.创建 Unique Index

  4.将 步骤1 备份的csv文件导入(mongoimport)集合。会自动过滤掉重复的数据。

效果:

  

 

3.MongoDB 模糊匹配查询:

 

4.MongoDB 查询结果按指定字段排序:

 

5.MongoDB update功能:

  1.新增字段

  

  2.更新字段值($set

   

  db.StrategyInfo.update({},{$set:{“test”:”股票池”}},false,true);

  其中update()3个参数,表示不存在的记录,是否插入,默认false,不插入。第4个参数,默认false只更新找到的第一条记录;true表示全部更新。

 

  3.删除某一列($unset

    

  

  4.重命名列名($rename

  

 

 6.更改某一列的字段类型

  1.查询某一列的字段类型($type)

  

  

  2.更改(forEach)

  

  db.calcgsdataflash_once.find().forEach(function(x){x.f2=String(x.f2);db.calcgsdataflash_once.save(x)})

  

 

  

 

7.修改用户密码:

  db.changeUserPassword("root","123456")

  

8.修改某一列的值为另一列的值

db.student.find().forEach( function(item){   db.student.update({"id":item._id},{"$set":{"newColumn":item.oldColumn}},true) })

  

 

转载于:https://www.cnblogs.com/SZxiaochun/p/6283151.html

你可能感兴趣的文章
XVI Open Cup named after E.V. Pankratiev. GP of Ekaterinburg.
查看>>
iOS-中app启动闪退的原因
查看>>
iOS--高级技术
查看>>
struct内存对齐
查看>>
Fiddler使用教程
查看>>
模式识别之车牌识别项目---车牌识别资料大全
查看>>
基于MeanShift的目标跟踪算法及实现
查看>>
linux 查看日志
查看>>
《CSS世界》读书笔记(八)
查看>>
Spark学习之路 (十三)SparkCore的调优之资源调优JVM的基本架构
查看>>
redis-cluster介绍
查看>>
web cookie and session
查看>>
构建一个通用的php验证的函数
查看>>
面向对象(单列模式)
查看>>
SpringMVC 实现文件下载
查看>>
七、rdd究竟是什么
查看>>
Ubuntu系统利用docker容器发布简单的应用
查看>>
学习网站
查看>>
HTML 5 <input> placeholder 属性
查看>>
应用场景是什么?怎样判断、描述一个产品的应用场景?
查看>>