6.mongo命令提示符帮助
admin
2023-01-19 02:40:06
0

6.mongo命令提示符帮助

最新内容会在源站更新.转载请保留原文链接: http://dashidan.com/article/mongodb/index.html


① mongo命令行参数帮助

通过--help参数显示命令行帮助信息

mongo --help

显示:

MongoDB shell version: 2.0.4usage: mongo [options] [db address] [file names (ending in .js)]db address can be:
  foo                   foo database on local machine  192.169.0.5/foo       foo database on 192.168.0.5 machine  192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999options:
  --shell               run the shell after executing files  --nodb                don't connect to mongod on startup - no 'db address'
                        arg expected
  --norc                开始不执行".mongorc.js"文件
  --quiet               安静模式
  --port arg            端口
  --host arg            IP
  --eval arg            运行javascript脚本
  -u [ --username ] arg 用户名
  -p [ --password ] arg 密码
  -h [ --help ]         显示这个帮助信息
  --version             版本号
  --verbose             increase verbosity
  --ipv6                开启IPv6支持(默认关闭)

② mongo指令帮助

通过命令提示符连上mongo数据库后,可以输入help来显示命令提示符帮助.

help

会显示:

db.help()                    数据库方法帮助信息db.mycoll.help()             集合方法帮助信息rs.help()                    help on replica set methods
help admin                   管理员帮助信息help connect                 连接数据库帮助help keys                    快捷键help misc                    杂项信息帮助help mr                      mapreduce帮助show dbs                     显示全部数据库名show collections             显示当前数据库中的全部集合名show users                   显示当前数据库的全部用户show profile                 show most recent system.profile entries with time >= 1msshow logs                    显示可以连接的日志(`logger`)名show log [name]              输出内存中的最近log的片段, 默认输出`global`use                 设定当前数据库db.foo.find()                显示集合`foo`中的对象列表db.foo.find( { a : 1 } )     查询foo集合中`a == 1`的对象it                           输入it, 继续迭代显示结果, 输出更多结果DBQuery.shellBatchSize = x   设置显示结果条数exit                         退出命令行

③ 数据库帮助

1.显示全部数据库

show dbs

2.显示部数据操作帮助

db.help()

3.显示方法的实现

显示一个数据的方法的具体实现,输入db.不带(). 例如:

db.updateUser

显示:

test.updateUser

④ 集合帮助

1.显示全部集合

show collections

2.显示集合帮助

db.collection.help()

显示

db.collection.find().help() - show DBCursor help
db.collection.count()db.collection.dataSize()db.collection.distinct( key ) - eg. db.collection.distinct( 'x' )db.collection.drop() drop the collection
db.collection.dropIndex(name)db.collection.dropIndexes()db.collection.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
db.collection.reIndex()db.collection.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
                                              e.g. db.collection.find( {x:77} , {name:1, x:1} )db.collection.find(...).count()db.collection.find(...).limit(n)db.collection.find(...).skip(n)db.collection.find(...).sort(...)db.collection.findOne([query])db.collection.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )db.collection.getDB() get DB object associated with collection
db.collection.getIndexes()db.collection.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )db.collection.mapReduce( mapFunction , reduceFunction ,  )db.collection.remove(query)db.collection.renameCollection( newName ,  ) renames the collection.db.collection.runCommand( name ,  ) runs a db command with the given name where the first param is the collection name
db.collection.save(obj)db.collection.stats()db.collection.storageSize() - includes free space allocated to this collection
db.collection.totalIndexSize() - size in bytes of all the indexes
db.collection.totalSize() - storage allocated for all data and indexes
db.collection.update(query, object[, upsert_bool, multi_bool])db.collection.validate(  ) - SLOW
db.collection.getShardVersion() - only for use with sharding
db.collection.getShardDistribution() - prints statistics about data distribution in the cluster

3.显示集合方法实现

显示方法实现输入db.., 不带(). 例如输入:

db.collection.save

显示:

function (obj) {
    if (obj == null || typeof obj == "undefined") {
        throw "can't save a null";
    }
    if (typeof obj == "number" || typeof obj == "string") {
        throw "can't save a number or string";
    }
    if (typeof obj._id == "undefined") {
        obj._id = new ObjectId;
        return this.insert(obj);
    } else {
        return this.update({_id:obj._id}, obj, true);
    }}

⑤ cursor帮助

当你在mongo命令行使用find()方法时, 可以使用很多cursor方法来修改find()行为和结果.

  • 显示find()方法可用的修改器和cursor处理方法

    db.collection.find().help()

显示:

> db.collection.find().help()find() modifiers    .sort( {...} )
    .limit( n )
    .skip( n )
    .count() - total # of objects matching query, ignores skip,limit
    .size() - total # of objects cursor would return, honors skip,limit
    .explain([verbose])
    .hint(...)
    .showDiskLoc() - adds a $diskLoc field to each returned objectCursor methods    .forEach( func )
    .map( func )
    .hasNext()
    .next()
  • cursor方法的实现, 输入db..find()., 不包含(). 例如:

db.collection.find().toArray

显示:

> db.collection.find().toArrayfunction () {
    if (this._arr) {
        return this._arr;
    }
    var a = [];
    while (this.hasNext()) {
        a.push(this.next());
    }
    this._arr = a;
    return a;}

常用的cursor方法

  • hasNext() 查询cursor是否还有数据.

  • next() 返回下一个数据对象, 并且cursor指向位置加1.

  • forEach( 

    ) 方法遍历执行全部结果. 只有1参数, 迭代器中指向的对象.

⑥ 包装对象帮助

可以通过输入help misc来获取对象包装类.

help misc

显示:

> help misc
    b = new BinData(subtype,base64str)  create a BSON BinData value
    b.subtype()                         the BinData subtype (0..255)
    b.length()                          length of the BinData data in bytes
    b.hex()                             the data as a hex encoded string
    b.base64()                          the data as a base 64 encoded string
    b.toString()

    b = HexData(subtype,hexstr)         create a BSON BinData value from a hex string
    b = UUID(hexstr)                    create a BSON BinData value of UUID subtype
    b = MD5(hexstr)                     create a BSON BinData value of MD5 subtype

    o = new ObjectId()                  create a new ObjectId
    o.getTimestamp()                    return timestamp derived from first 32 bits of the OID
    o.isObjectId()
    o.toString()
    o.equals(otherid)

⑦ 参考文章

 官方文档


相关内容

热门资讯

2026 WAIC观察:机器人... 摘要:具身智能的“iPhone时刻”何时来? 今年WAIC的火爆超出了所有人的预期,黄牛票卖到200...
票据扫描后显示查无此票?沈阳一... 近日,有市民向潇湘晨报·晨视频爆料,自己在辽宁沈阳臻悦影城购买《功夫女足》电影票,出票后用国家电影事...
没钱打伊朗了?美媒爆料:美军连... 前线炮火不断,后方战费告急。由于伊朗冲突带来的巨额开支超乎预期,美国国防部正步入“断粮”边缘。美国《...
三星 Galaxy Z Fol... 三星目前已经确认,将于7月22日 21:00带来三星Galaxy全球新品发布活动。也就是说,明天大家...
航天员举办公益科普 为民众沉浸... 中新网广州7月21日电 (记者 王坚)在中国航天事业创建70周年之际,嫦娥六号月背玄武岩国旗实物近日...
消失30年的功夫童星,又打回来... 作者 | 肖瑶 编辑 | 吴擎这个夏天“打”得最爽的电影《火遮眼》里,人们认出了谢苗。他是主角。那个...
太空算力,将数据中心搬上天(经... 本报记者 王 政近年来,我国加强人工智能科技创新,积极推进“人工智能+”行动,智能经济核心产业规模已...
半自动洗衣机洗涤电机嗡嗡响不转 1、有可能是使用的时候洗衣机使用的电流功率比电源线和插线板之间的输出电流功率大,在这种情况下的话就会...
油烟机电机不转有什么故障 油烟机电机不转,可能存在以下故障:1.电源故障:若油烟机的电源线有擦伤、切割、线路松动等导致电源供电...
全自动洗衣机电机响但是不转 这种不转的情况有很多种比如定时器故障、电源线插头与插座接触不良、电机启动电容损坏、电机线圈损坏、电机...