我是靠谱客的博主 幸福流沙,这篇文章主要介绍【mongoDB基本操作】,现在分享给大家,希望可以做个参考。

###主从切换,指定时间30S后切换

复制代码
1
2
rs.stepDown(30)

###查看副版本状态

复制代码
1
2
rs.status()

###创建集合

复制代码
1
2
db.createCollection('student')

###集合列表

复制代码
1
2
show collections

###删除集合

复制代码
1
2
db.student.drop()

###插入一条数据

复制代码
1
2
db.test1201.insertOne({test:1201})

###插入多条数据

复制代码
1
2
db.test1201.insertMany([{'test':1202,'test2':1203},{'test3':1201,'test4':1203}])

###查看集合文档内容

复制代码
1
2
rs.collecttest.find()

###查看集合详情

复制代码
1
2
db.student.stats()

###查看索引

复制代码
1
2
db.getCollection('xx').getIndexes()

###创建索引
代表升序,-1代表降序,name 指定索引名

复制代码
1
2
db.getCollection('xx').createIndex( {"title": 1}, {"name":"idx_xxx"} );

###创建复合索引

复制代码
1
2
db.getCollection('xx').createIndex( {"title": 1, "created_at": -1} );

###内嵌字段创建索引

复制代码
1
2
db.集合名.createIndex( {"字段名.内嵌字段名":1}, {"name":'idx_字段名_内嵌字段名'} )

###删除索引

复制代码
1
2
db.getCollection('xx').dropIndex("idx_xxx");

###查询前三条

复制代码
1
2
db.book.find().limit(3);

###只显示某个字段

复制代码
1
2
db.T_ServiceTypeTemplate.find({},{"createTime": 1})

###多条件查询

复制代码
1
2
db.集合名称.find( { $or : [ { 字段1 : 值1 }, { 字段2 : 值2 } ... ] } )

###修改字段内容

复制代码
1
2
db.T_U.update({"wid":"102"},{$set:{"nId":"5170b3ede4b035e16493be37"}},{multi:true})

###加条件更改

复制代码
1
2
db.T_U.update({"wid" : "101","jobTitle" : "职员"},{$set:{"jobTitle" : ""}},{multi:true})

###导出-q 过滤条件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
./mongoexport -h xxx.xxx.xxx.xxx --port=27017 --username=xxx --password='xxxxxx' --authenticationDatabase=admin -d dbbame -c collection -q '{"aId" : "500000009", sendTime: {$lt: ISODate("2022-10-21T00:00:00Z"),$gte: ISODate("2021-01-01T22:00:00Z")}}' -f _id,sourceId --type csv -o T_D1021.csv

###导入

复制代码
1
2
3
4
5
6
7
./mongoimport -h xxx.xxx.xxx.xxx --port 27017 -u xxx -p 'xxxxxxxxxxx' --authenticationDatabase=dbbame -d dbbame -c collection T_P0310bak.json

###shell执行mongo命令
####查询库

复制代码
1
2
3
4
5
6
7
echo -e "show dbs" | /path/to/mongo --quiet --host=xx.xx.xx.xx --port=27017 -u username -p 'XXX' --authenticationDatabase=admin

####查询表

复制代码
1
2
3
4
5
6
7
8
echo -e "use testDB;n db.testColl.findOne()" | /path/to/mongo --quiet --host=xx.xx.xx.xx --port=27017 -u username -p 'XXX' --authenticationDatabase=admin

####清空表

复制代码
1
2
3
4
5
6
7
8
echo -e "use testDB;n db.testColl.remove({})" | /path/to/mongo --quiet --host=xx.xx.xx.xx --port=27017 -u username -p 'XXX' --authenticationDatabase=admin

###shell脚本中使用

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash db=xxx tables=' xxx ' for table in ${tables[@]} do echo $table echo -e "use $db;n db.${table}.remove({})" | /path/to/mongo --quiet --host=xx.xx.xx.xx --port=27027 -u username -p 'XXX' --authenticationDatabase=admin done

最后

以上就是幸福流沙最近收集整理的关于【mongoDB基本操作】的全部内容,更多相关【mongoDB基本操作】内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(142)

评论列表共有 0 条评论

立即
投稿
返回
顶部