mongodb常用命令

it2026-03-17  5

集群搭建https://blog.csdn.net/kevinmcy/article/details/82712074

1.创建空文件注意点 将mkdir替换为touch,仅限创建空的文件 touch -p /home/mongodb/log/config.log touch -p /home/mongodb/log/mongos.log touch -p /home/mongodb/log/shard1.log touch -p /home/mongodb/log/shard2.log touch -p /home/mongodb/log/shard3.log

rs.initiate({_id:“configs”,members:[{_id:0,host:“192.168.23.132:27018”},{_id:1,host:“192.168.23.133:27018”}, {_id:2,host:“192.168.23.134:27018”}]})

rs.initiate({_id:“shard1”,members:[{_id:0,host:“192.168.23.132:27001”},{_id:1,host:“192.168.23.133:27001”},{_id:2,host:“192.168.23.134:27001”}]})

sh.addShard(“shard1/192.168.23.132:27001,192.168.23.133:27001,192.168.23.134:27001”) sh.addShard(“shard2/192.168.23.132:27002,192.168.23.133:27002,192.168.23.134:27002”) sh.addShard(“shard3/192.168.23.132:27003,192.168.23.133:27003,192.168.23.134:27003”)

db.status() db.user.getIndexes() db.user.findOne() db.user.stats()

db.qikegu.insert([ { title: ‘MongoDB 入门’, description: ‘MongoDB是一个以文档为中心的NOSQL数据库’, by: ‘qikegu.com’, url: ‘https://www.qikegu.com’, tags: [‘mongodb’, ‘database’, ‘NoSQL’], likes: 88 },

{ title: ‘MongoDB 优势’, description: “MongoDB相对于RDBMS的优势”, by: ‘qikegu.com’, url: ‘https://www.qikegu.com’, tags: [‘mongodb’, ‘database’, ‘NoSQL’], likes: 89, comments: [ { user:‘user1’, message: ‘My first comment’, dateCreated: new Date(2018,11,10,2,35), like: 0 } ] } ])

3.按时间查询

db.xxx.find({“ct”:{KaTeX parse error: Expected 'EOF', got '}' at position 24: …Date(2016,11,1)}̲}) mongoDB对时间的处…gt":ISODate(“2017-04-20T01:16:33.303Z”)}}) // 大于某个时间

db.getCollection(‘CustomerBehaviorLog_0407’). find({ CreateTime: { $gte: ISODate(“2020-03-31T16:00:00.000Z”), KaTeX parse error: Expected 'EOF', got '}' at position 45: …:00.000Z") }̲, "BehaviorType…in":[1,2]} }).count();

4.按条件导出数据 mongoexport -u user -p pwd! -d dbName -c users -q ‘{age:20}’ -o /data/date.json mongoexport -u user -p pwd! -d dbName -c users --type=csv -f _id,name,age -o /data/date.csv 导入数据 mongoimport -u user -p pwd -d dbName -c users --file /data/date.json mongoimport -u user -p pwd -d dbName -c users --type=csv --headerline --file /data/date.csv

mongoimport -u admin -p admin -d admin -c customer_0303 --file customer_0303.json

mongoexport -h 182.254.150.218:27017 -u leyou -p leyou -d leyou -c CustomerBehaviorLog_0407 -q ‘{“CreateTime”:{ g t e : I S O D a t e ( " 2020 − 03 − 30 T 16 : 00 : 00.000 Z " ) , gte: ISODate("2020-03-30T16:00:00.000Z"), gte:ISODate("20200330T16:00:00.000Z"),lt: ISODate(“2020-03-31T16:00:00.000Z”)}}’ -o customer_0330.json mongoexport -u admin -p admin -d admin -c CustomerBehaviorLog_1024 -q ‘{“CreateTime”:{“ g t e " : I S O D a t e ( " 2020 − 03 − 0306 : 22 : 56.580 Z " ) , " gte":ISODate("2020-03-03 06:22:56.580Z")," gte":ISODate("2020030306:22:56.580Z"),"lte”:ISODate(“2020-03-04 06:22:56.580Z”)}}’ -o customer_0303.json mongoexport -u admin -p admin -d admin -c CustomerBehaviorLog_1024 -q ‘{"_id":{“ g t e " : O b j e c t I d ( " 5 e 5 d f 7 c 071 a b 8 a 1 c e 48 e e a e 8 " ) , " gte":ObjectId("5e5df7c071ab8a1ce48eeae8")," gte":ObjectId("5e5df7c071ab8a1ce48eeae8"),"lt”:ObjectId(“5e5f48f82ddbfa05f0ee3d9d”)}}’ -o customer_0303.json

5.建库、建表 use test2 db.createCollection(‘customer_0303’);

最新回复(0)