msforest / notebook

好记性不如烂笔头,记录知识的点点滴滴。
https://github.com/msforest/notebook/wiki
0 stars 0 forks source link

mongodb聚合,日志 #21

Open msforest opened 6 years ago

msforest commented 6 years ago

  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 commented 6 years ago

聚合

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}})


2. 数学操作法
- $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']}}})


3. 日期表达式
- $year
- $month
- $week
- $dayOfMonth
- $dayOfWeek
- $dayOfYear
- $hour
- $minute
- $second

db.test.aggregate({$project: {hireIn: {$month: '$hireDate'}}})


4. 字符串表达式
- $substr : [expr, startOffset, numToReturn]
- $concat : [expr1[, expr2, ..., expr3]]
- $toLower 
- $toUpper

5. 其他
- $cmp
- $strcasecmp
- $ne / $eq / $lt / $lte / $gt / $gte
- $and / $or / $not / $in /$nin
- $slice / $inc / $push / $each / $addToSet / $pop / $pull 

<hr>

//{ "_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](#210)
msforest commented 6 years ago

问题:

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

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

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

replicaSet.initiate(config)



[top](#210)
msforest commented 6 years ago

journal

link: how mongodb journal works journal