• (转载)MongoingDB常用操作


    mongo –path

    db.AddUser(username,password)  添加用户

    db.auth(usrename,password)     设置数据库连接验证

    db.cloneDataBase(fromhost)     从目标服务器克隆一个数据库

    db.commandHelp(name)           returns thehelpforthe command

    db.copyDatabase(fromdb,todb,fromhost)  复制数据库fromdb—源数据库名称,todb—目标数据库名称,fromhost—源数据库服务器地址

    db.createCollection(name,{size:3333,capped:333,max:88888})  创建一个数据集,相当于一个表

    db.currentOp()                 取消当前库的当前操作

    db.dropDataBase()              删除当前数据库

    db.eval(func,args)             run code server-side

    db.getCollection(cname)        取得一个数据集合,同用法:db['cname']ordb.cname

    db.getCollenctionNames()       取得所有数据集合的名称列表

    db.getLastError()              返回最后一个错误的提示消息

    db.getLastErrorObj()           返回最后一个错误的对象

    db.getMongo()                  取得当前服务器的连接对象get the server connectionobject

    db.getMondo().setSlaveOk()     allow this connection to readfromthen nonmaster membr of a replica pair

    db.getName()                   返回当操作数据库的名称

    db.getPrevError()              返回上一个错误对象

    db.getProfilingLevel()         ?什么等级

    db.getReplicationInfo()        ?什么信息

    db.getSisterDB(name)           get the db at the same server as this onew

    db.killOp()                    停止(杀死)在当前库的当前操作

    db.printCollectionStats()      返回当前库的数据集状态

    db.printReplicationInfo()

    db.printSlaveReplicationInfo()

    db.printShardingStatus()       返回当前数据库是否为共享数据库

    db.removeUser(username)        删除用户

    db.repairDatabase()            修复当前数据库

    db.resetError()

    db.runCommand(cmdObj)          run a database command.ifcmdObj is a string, turns it into {cmdObj:1}

    db.setProfilingLevel(level)    0=off,1=slow,2=all

    db.shutdownServer()            关闭当前服务程序

    db.version()                   返回当前程序的版本信息

    数据集(表)操作语法

    db.linlin.find({id:10})          返回linlin数据集ID=10的数据集

    db.linlin.find({id:10}).count()  返回linlin数据集ID=10的数据总数

    db.linlin.find({id:10}).limit(2)返回linlin数据集ID=10的数据集从第二条开始的数据集

    db.linlin.find({id:10}).skip(8)  返回linlin数据集ID=10的数据集从0到第八条的数据集

    db.linlin.find({id:10}).limit(2).skip(8)  返回linlin数据集ID=1=的数据集从第二条到第八条的数据

    db.linlin.find({id:10}).sort()   返回linlin数据集ID=10的排序数据集

    db.linlin.findOne([query])       返回符合条件的一条数据

    db.linlin.getDB()                返回此数据集所属的数据库名称

    db.linlin.getIndexes()           返回些数据集的索引信息

    db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})

    db.linlin.mapReduce(mayFunction,reduceFunction,)

    db.linlin.remove(query)                      在数据集中删除一条数据

    db.linlin.renameCollection(newName)          重命名些数据集名称

    db.linlin.save(obj)                          往数据集中插入一条数据

    db.linlin.stats()                            返回此数据集的状态

    db.linlin.storageSize()                      返回此数据集的存储大小

    db.linlin.totalIndexSize()                   返回此数据集的索引文件大小

    db.linlin.totalSize()                        返回些数据集的总大小

    db.linlin.update(query,object[,upsert_bool])在此数据集中更新一条数据

    db.linlin.validate()                         验证此数据集

    db.linlin.getShardVersion()                  返回数据集共享版本号

    db.linlin.find({‘name’:'foobar’})    select*fromlinlin where name=’foobar’

    db.linlin.find()                     select*fromlinlin

    db.linlin.find({‘ID’:10}).count()    select count(*)fromlinlin whereID=10

    db.linlin.find().skip(10).limit(20)  从查询结果的第十条开始读20条数据  select * from linlin limit 10,20 ———-mysql

    db.linlin.find({‘ID’:{$in:[25,35,45]}})  select * from linlin where IDin(25,35,45)

    db.linlin.find().sort({‘ID’:-1})      select * from linlin order by IDdesc

    db.linlin.distinct(‘name’,{‘ID’:{$lt:20}})   select distinct(name)fromlinlin whereID<</code>20

    db.linlin.group({key:{‘name’:true},cond:{‘name’:'foo’},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})

    select name,sum(marks)fromlinlin group by name

    db.linlin.find(‘this.ID<</code>20′,{name:1})     select name from linlin where ID<</code>20

    db.linlin.insert({‘name’:'foobar’,'age’:25})  insert into linlin (‘name’,'age’)values(‘foobar’,25)

    db.linlin.insert({‘name’:'foobar’,'age’:25,’email’:'cclove2@163.com’})

    db.linlin.remove({})                   delete*fromlinlin

    db.linlin.remove({‘age’:20})           delete linlin where age=20

    db.linlin.remove({‘age’:{$lt:20}})     delete linlin where age<</code>20

    db.linlin.remove({‘age’:{$lte:20}})    delete linlin where age<</code>=20

    db.linlin.remove({‘age’:{$gt:20}})     delete linlin where age>20

    db.linlin.remove({‘age’:{$gte:20}})    delete linlin where age>=20

    db.linlin.remove({‘age’:{$ne:20}})     delete linlin where age!=20

    db.linlin.update({‘name’:'foobar’},{$set:{‘age’:36}})  update linlinsetage=36where name=’foobar’

    db.linlin.update({‘name’:'foobar’},{$inc:{‘age’:3}})   update linlinsetage=age+3where name=’foobar’

    超级用户相关:

    use admin

    #增加或修改用户密码

    db.addUser(ixigua,'pwd')

    #查看用户列表

    db.system.users.find()

    #用户认证

    db.auth(ixigua,'pwd')

    #删除用户

    db.removeUser('mongodb')

    #查看所有用户

    show users

    #查看所有数据库

    show dbs

    #查看所有的collection

    show collections

    #查看各collection的状态

    db.printCollectionStats()

    #查看主从复制状态

    db.printReplicationInfo()

    #修复数据库

    db.repairDatabase()

    #设置记录profiling,0=off 1=slow 2=all

    db.setProfilingLevel(1)

    #查看profiling

    show profile

    #拷贝数据库

    db.copyDatabase('mail_addr','mail_addr_tmp')

    #删除collection

    db.mail_addr.drop()

    #删除当前的数据库

    db.dropDatabase()

    客户端连接

    /usr/local/mongodb/bin/mongo 8.8.88/ixigualib -u ixigua -p 'pwd'

    增删改

    #存储嵌套的对象

    db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})

    #存储数组对象

    db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})

    #根据query条件修改,如果不存在则插入,允许修改多条记录

    db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)

    #删除yy=5的记录

    db.foo.remove({'yy':5})

    #删除所有的记录

    db.foo.remove()

    索引

    #增加索引:1(ascending),-1(descending)

    db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});

    #索引子对象

    db.user_addr.ensureIndex({'Al.Em': 1})

    #查看索引信息

    db.deliver_status.getIndexes()

    db.deliver_status.getIndexKeys()

    #根据索引名删除索引

    db.user_addr.dropIndex('Al.Em_1')

    查询

    #查找所有

    db.foo.find()

    #查找一条记录

    db.foo.findOne()

    #根据条件检索10条记录

    db.foo.find({'msg':'Hello 1'}).limit(10)

    #sort排序

    db.deliver_status.find({'From':'ixigua@sina.com'}).sort({'Dt',-1})

    db.deliver_status.find().sort({'Ct':-1}).limit(1)

    #count操作

    db.user_addr.count()

    #distinct操作

    db.foo.distinct('msg')

    #>操作

    db.foo.find({"timestamp": {"$gte" : 2}})

    #子对象的查找

    db.foo.find({'address.city':'beijing'})

    管理

    #查看collection数据的大小

    db.deliver_status.dataSize()

    #查看colleciont状态

    db.deliver_status.stats()

    #查询所有索引的大小

    db.deliver_status.totalIndexSize()

    http://blog.sina.com.cn/s/blog_657f548401016wev.html

    作者:HOH
    出处:http://hoh.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    MYSQL函数 Cast和convert的用法详解
    MySQL5.7.9(GA)的安装
    TMS Scripter importtool的使用
    MySQL 高可用架构在业务层面的应用分析
    UNIGUI:How to redirect and close session?
    HTML URL 编码:请参阅:http://www.w3school.com.cn/tags/html_ref_urlencode.html
    js 解决函数加载的问题
    必备函数
    Action 分离
    JavaScript.Remove
  • 原文地址:https://www.cnblogs.com/HOH/p/3227214.html
Copyright © 2020-2023  润新知