MongoDB常用命令记录

一、连接数据库

连接数据操作

[root@blga-0006 ~]# mongo 192.168.113.58:27017

MongoDB shell version v3.6.17

connecting to: mongodb://192.168.113.58:27017/test?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("b61d6fa2-cae0-49e1-8917-b15fa8227b8d") }

MongoDB server version: 3.6.17

Server has startup warnings: 

2020-02-11T01:18:29.998+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2020-02-11T01:18:29.998+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

> use epidemic_control

switched to db epidemic_control

> show collections

enterpriseInfo

staffInfo

> db.enterpriseInfo.find({"name":"天真模具厂"})

{ "_id" : ObjectId("5e444836c94564b7e884763e"), "name" : "天真模具厂", "applyDate" : NumberLong("1581841080489")} 

 

 

 

 

 

一、查询操作

 

按名称查询公司

db.enterpriseInfo.find({"name":"天真模具厂"})

 

按名称查询公司格式化结果

db.enterpriseInfo.find({"name":"天真模具厂"}).pretty()

 

按名称模糊查询企业

db.enterpriseNonLocalInfo.find({"name":/海洋/})

 

按名称模糊查询企业并按街道id排倒序

db.enterpriseNonLocalInfo.find({"name":/科技/}).pretty().sort({"streetId":-1})

 

 

二、更新操作

 

按名称更新企业密码

md5批量修改:http://tools.bugscaner.com/stringtomd5/

更新name的值是root记录,修改passord的值成123456

db.enterpriseInfo.update({'name':'root'},{$set:{'passord':'123456'}})

 

按名称更新企业所属街道

db.enterpriseInfo.update({"name":"物流有限公司"},{$set:{"streetId":"08"}})

 

 

三、删除操作

 

删除name的值是中国船舶的记录

db.enterpriseInfo.remove({"name":"中国船舶"})

 

四、索引操作

 

对字段applyDate创建倒序索引

db.enterpriseInfo.createIndex({"applyDate": -1})

 

五、新建文档

新建一个文档commonDict

db.createCollection("commonDict", {autoIndexId : true} )

 

插入文档,字段code、name、street、other

db.commonDict.insert({code: '04', name: '大石桥街道', type: 'street', other: ''})

 

六、导入导出

批量导出人员数据到csv

mongoexport -h 192.168.13.51 -u admin -p mpoi456 --db epidemic_control --collection staffInfo --type csv --fields identifyCode,name,cellphone,healthStatus,vocationLivingFrom,vocationLivingCode,vocationLivingCn,backTransportation,backTransportationDate,nativeLivingCode,nativeLivingCn,highRiskAreaContact,highRiskAreaPeopleContact,ifmHighRiskAreaContact,approvalStatus,goOutOfNingBo,commuting,returnToWorkLiving,returnToWorkLivingOtherStreet,returnToWorkLivingOtherAddress,idOrPassPort,riskLevel,collisionResult,permitStatus,stopOver,ifm --out staff.csv

 

 

[root@blga-0006 dataoper]# cat export.sh

mongoexport -h 192.168.13.51 -u admin -p mpoi456 --db epidemic_control --collection staffInfo --type csv --fields _id,cellphone  --noHeaderLine  --out /home/dataoper/dataoper/export.csv

[root@blga-0006 dataoper]# cat import.sh

mongoimport -h 192.168.13.51 -u admin -p mlamp123456 --db epidemic_control --collection staffInfo --type csv --mode=merge --fields '_id.string(),riskCode.int32(),riskLevel.int32(),permitStatus.int32(),approvalStatus.int32()' --columnsHaveTypes --file /home/dataoper/dataoper/import.csv

 

 

 

批量导出再导入的另一种方式:相当于关系型(mysql)数据库的insert into tab1... select ... from tab2

举例:把非北仑企业表enterpriseNonLocalInfo的记录追加到北仑企业表enterpriseInfo中,操作脚本如下

var docs = db.enterpriseNonLocalInfo.find();

docs.forEach(function(d){

    var newDoc = new Object;

    if(d._id != null) newDoc["_id"] = d._id;

if(d.name != null) newDoc["name"] = d.name;

if(d.code != null) newDoc["code"] = d.code;

if(d.tel != null) newDoc["tel"] = d.contactTel;

if(d.streetId != null) newDoc["streetId"] = d.streetId;

if(d.location != null) newDoc["location"] = d.location;

if(d.contactPerson != null) newDoc["contactPerson"] = d.contactPerson;

if(d.password != null) newDoc["password"] = d.password;

if(d.registerDate != null) newDoc["registerDate"] = d.registerDate;

if(d._class != null) newDoc["_class"] = "com.control.manage.entity.EnterpriseInfo";

if(d.enterpriseId != null) newDoc["enterpriseId"] = d.enterpriseId;

if(d.IsLocalEnterprise != null) newDoc["IsLocalEnterprise"] = 1;

if(d.continuousWorkApplyStatus != null) newDoc["continuousWorkApplyStatus"] = d.continuousWorkApplyStatus;

if(d.returnToWorkApplyStatus != null) newDoc["returnToWorkApplyStatus"] = d.returnToWorkApplyStatus;

if(d.applyDate != null) newDoc["applyDate"] = d.registerDate;

if(d.applyType != null) newDoc["applyType"] = d.applyType;

db.enterpriseInfo.insert(newDoc);

});

 

批量更新:

db.enterpriseInfo.update({"IsLocalEnterprise":1},{$set:{"isLocalEnterprise":1}},{multi:true})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值