Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb聚合,日志 #21

Open
msforest opened this issue Mar 20, 2018 · 3 comments
Open

mongodb聚合,日志 #21

msforest opened this issue Mar 20, 2018 · 3 comments
Labels

Comments

@msforest
Copy link
Owner

msforest commented Mar 20, 2018

  1. 聚合
  2. 问题
  3. journal

导入导出

导出单个表: mongoexport -d dbname -c collectionname -o file -q '{a: 1}'
导入单个表: mongoimport -d dbname -c collectionname --file filename 
	数据添加:mongoimport -d dbname -c collectionname < filename
导出数据库: mongodump -h dbhost -d dbname -o dbdirectory
导入数据库: mongorestore -h dbhost -d dbname --dir dbdirectory

查看服务器的命令行参数

// mongo shell > db.serverCmdLineOpts()

{
	"argv" : [
		"/usr/local/opt/mongodb/bin/mongod",
		"--config",
		"/usr/local/etc/mongod.conf"
	],
	"parsed" : {
		"config" : "/usr/local/etc/mongod.conf",
		"net" : {
			"bindIp" : "127.0.0.1"
		},
		"storage" : {
			"dbPath" : "/usr/local/var/mongodb"
		},
		"systemLog" : {
			"destination" : "file",
			"logAppend" : true,
			"path" : "/usr/local/var/log/mongodb/mongo.log"
		}
	},
	"ok" : 1
}

// 查看当前mongo一次允许插入最大的bson大小
> db.isMaster().maxBsonObjectSize/(1024*1024)+' MB'

// 查看一个collection的bson大小
> Object.bsonsize(db.test.findOne())
@msforest
Copy link
Owner Author

msforest commented Apr 7, 2018

聚合

https://docs.mongodb.com/manual/reference/operator/

  1. 管道表达式
  • 筛选match: {$match: {key: value}}
  • 投射project
  • 分组group
  • 拆分unwind: {$unwind: '$property'}
  • 排序sort
  • 限制limit
  • 跳过skip
db.articles.aggregate({$project: {author: 1}}, 
  {$group: {_id: '$author', count: {$sum: 1}}},
  {$sort: {count: -1}},
  {$limit: 5})

// rename: _id => id
db.test.aggregate({$project: {id: '$_id', _id: 0}})
  1. 数学操作法
  • $add : [expr1[, expr2, ..., exprN]]
  • $subtract : [expr1, expr2]
  • $multiply : [expr1[, expr2, ..., exprN]]
  • $divide : [expr1, expr2]
  • $mod : [expr1, expr2]
db.test.aggregate({$project: {total: {$subtract: [{$add: ['$a', '$b']}, '$c']}}})
  1. 日期表达式
  • $year
  • $month
  • $week
  • $dayOfMonth
  • $dayOfWeek
  • $dayOfYear
  • $hour
  • $minute
  • $second
db.test.aggregate({$project: {hireIn: {$month: '$hireDate'}}})
  1. 字符串表达式
  • $substr : [expr, startOffset, numToReturn]
  • $concat : [expr1[, expr2, ..., expr3]]
  • $toLower
  • $toUpper
  1. 其他
  • $cmp
  • $strcasecmp
  • $ne / $eq / $lt / $lte / $gt / $gte
  • $and / $or / $not / $in /$nin
  • $slice / $inc / $push / $each / $addToSet / $pop / $pull

//{ "_id" : ObjectId("5ace21a69ef8360d70cbf73c"), "a" : 1, "b" : [ { "a" : 2 }, { "a" : 3 }, { "a" : 5 } ] }
> db.test.find({'b.a': 3}, {'b.$': 1})
//{ "_id" : ObjectId("5ace21a69ef8360d70cbf73c"), "b" : [ { "a" : 3 } ] }  只列出匹配的一个


> db.test.find({a: 1}, {b: {$slice: 1}})
// { "_id" : ObjectId("5ace21a69ef8360d70cbf73c"), "a" : 1, "b" : [ { "a" : 2 } ] } 只列出第一个

// {x: 1}, {x: [14]}, {x: [5, 15]}
> db.test.find({x: {$elemMatch: {$gt: 10, $lt: 30}}})  //typeof x === array
// {x: [14]}

> db.test.find({x: {$gt: 10, $lt: 30}})
// {x: 1}, {x: [14]}, {x: [5, 15]}

top

@msforest
Copy link
Owner Author

msforest commented Apr 7, 2018

问题:

如何搭建副本集,参考《MongoDB权威指南第2版》:第9章节

    1. 在本地搭建副本集时,执行replicaSet.startSet()报错
this.getDB of undefined.

往回看第一个命令replicaSet = new ReplSetTest({node: 3})时发现ports属性为空数组,然后把本机的主机名改为“localhost”就好了

    1. 在创建副本集的时候,执行replicaSet.initiate()报错
No host described in new configuration 1 for replica set __unknown_name__ maps to this node

经过查找,https://dotmanila.com/2015/11/mongodb-no-host-in-new-replica-set-maps-to-this-node/
这里列出三点,最重要的是说重新核对replica set config,一看replicaSet.startSet()生成的host是name.xx.-book:20000,导致无法解析主机名,然后自己重新定义配置

config = {
  _id: '__unknown_name__',  // from cmd `ps -ef|grep mongo`, --replSet __unknown_name__
  members: [
  {
    _id: 0,
    host: 'localhost:20000'
  },
  ...
  ]
}

replicaSet.initiate(config)

top

@msforest
Copy link
Owner Author

msforest commented Apr 16, 2018

journal

link:
how mongodb journal works
journal

@msforest msforest added the mongo label Apr 18, 2018
@msforest msforest changed the title mongodb mongodb聚合,日志 Apr 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant