• mongodb 常用的命令


    mongodb 常用的命令

    对数据库的操作,以及登录

    1 进入数据库

    use admin
    

    2 增加或修改密码

    db.addUser('wsc', '123')
    

    3查看用户列表

    db.system.users.find()
    

    4 #用户认证

    db.auth('wsc', 123)
    

    5 #删除用户

    db.removeUser('wsc')
    

    6 #查看所有用户

    show users
    

    7 #查看所有数据库

    show dbs
    

    8 #查看所有的collection集合

    show collections
    

    9 #查看各个collection的状态

    db.printCollectionStats()
    

    10 #查看主从复制状态

    db.repairDatabase()
    

    11 #修复数据库

    db.repairDatabase()
    

    12 #设置profiling,0:off 1:slow 2 all

    db.setProfilingLevel(1)
    

    13 #查看profiling

    show profiling
    

    14 #拷贝数据库

    db.copyDatabase('wsc', 'wsc copy')
    

    15 #删除集合collection

    db.wsc.drop()
    

    16 #删除当前数据库

    db.dropDatabase()
    




    mongodb的增删改查

    1 #存储嵌套的对象

    db.foo.save(
    	{
    		‘name’:wsc,
    		’age’:25,’address’:
    		{
    			‘city’:’shnaghai’,
    			’Province’:’shnaghai’
    		}
    	}
    )
    

    2#存储数组对象

    db.foo.save(
    	{
    		‘name’:wsc,
    		’age’:25,
    		’address’:
    		[
    			‘anhui Province’,
    			’anhui Province’
    		]
    	}
    )
    

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

    db.foo.update(
    	{
    		‘age’:’25’
    	},
    	{
    		‘$set’:
    		{
    			‘name’:’xingoo’
    		}
    	},
    	upsert=true,
    	multi=true
    )
    

    4 #删除yy=5的记录

    db.foo.remove({'name':'wsc'})
    

    5 #删除所有的记录

    db.foo.remove()
    




    索引

    1 #增加索引:1 asc -1 desc

    db.foo.ensureIndex(
    	{
    		firstname:1,
    		lastname:-1
    	},
    	{
    		unieap:true
    	}
    )
    

    2 #索引子对象

    db.foo.ensureIndex({‘Al.Em’:!})
    

    3 #查看索引信息

    db.foo.getIndexes()
    db.foo.getIndexKeys()
    

    4 #根据索引名删除索引

    db.foo.dropIndex(‘Al.Em_1’)
    




    查询,利用条件操作符进行查询,并和sql语句进行对比

    条件操作符

     1 gt ---- >
     2 $lt ---- <
     3 $gte ---- >=
     4 $lte ---- <=
     5 $ne ---- != 、<>
     6 $in ---- in
     7 $nin ---- not in
     8 $all ---- all
     9 $or ---- or
     10 $not ---- 反匹配
    

    1 查询所有记录

    db.foo.find()   --- select * from foo
    

    2 查询某列非重复的记录

    db.foo.distinct('wsc') ---- select distinct name from foo
    

    3 查询age = 22的记录

    db.foo.find({"age":22)  ----- select * from foo where age = 22
    

    4 查询age > 22的记录

    db.foo.find({age:{$gt:22}}) ---- select * from foo where age > 22
    

    5 查询age < 22 的记录

    db.foo.find({age:{$lt:22}}) ---- select * from foo where age < 22
    

    6 查询age <= 22 的记录

    db.foo.find({age:{$lte:22}}) ---- select * from foo where age <= 22
    

    7 查询age => 22 且 age <= 26 的记录

    db.foo.find({age:{$gte:22,$lte:26}}) ---- select * from foo where age => 22 and age <= 26
    

    8 #查询name中包含wsc的数据

    db.foo.find({name:/wsc/}  ---- select * from foo where name like '%wsc%'
    

    9 #查询name中以wsc开头的数据

    db.foo.find({name:/^wsc/}) ---- select * from foo where name like 'wsc%'
    

    10 #查询指定列name、age的数据

    db.foo.find({}, {name:1, age;1})  ---- select name , age from foo 
    

    11 #查询制定列name、age数据,并且age > 22

    db.foo.find({age:{$lt:22}}, {name:1, age;1})  ---- select name , age from foo where age > 22
    

    12 #按照年龄排序

    db.foo.find().sort({age:1}) 升序
    db.foo.find().sort({age:-1}) 降序
    

    13 #查询name=wsc .age=25的数据

    db.foo.find({name:'wsc', age:25}) ---- select * from foo where name = 'wsc' and age = '25'
    

    14#查询前5条数据

    db.foo.find().limit(5)  ---- select top 5 from fo0
    

    15 #查询10条以后的数据

    db.foo.find().skip(10)  ---- select * from foo where id not in (select top 10 from foo)
    

    16 #查询在5-10之间的数据

    db.foo.find().limit(10).skip(5)
    

    17 #or与查询

    db.foo.find({$or:[{age:22},{age:25}]})  ---- select * from foo where age = 22 or age = 25
    
    18 #查询第一条数据
    db.foo.findOne()
    db.foo.find.limit(1)  ---- select top 1 * from foo
    

    19 #查询某个结果集的记录条数

    db.foo.find({age:{$gte:25}}).count()  ---- select count(*)  from where age >= 20
    

    20 #按照某列进行排序(不懂)

    db.foo.find({sex:{$exit:true}}).count()  ---- select count(sex) from foo
    

    21 #查询age取模10等于0的数据

    db.foo.find('this.age%10 == 0')
    db.foo.find({age:{$mod:[10,0]}}) 
    

    22 #匹配所有

    db.foo.find({age:{$all:[22,25]}})
    

    23 #查询不匹配name=X*带头的记录

    db.foo.find({name:{$not:/^x.*/}})
    

    24 #排除返回age字段

    db.foo.find({name:'wsc'},{age:0})
    

    25 #判断字段是否存在

    db.foo.find({name:{$exits:true}})
    

    对collection的管理

    1 #查看collection数据大小

    db.wsc.dataSize()
    

    2 #查看collection状态

    db.wsc.stats()
    

    3 #查询所有索引的大小

    db.wsc.totalIndexSize()
    

    参考 连接
    http://www.100mian.com/mianshi/mongodb/49183.html

  • 相关阅读:
    Android提高UI性能技巧
    POJ-1785-Binary Search Heap Construction(笛卡尔树)
    atitit。企业组织与软件project的策略 战略 趋势 原则 attilax 大总结
    Remove Duplicates from Sorted List II 解答(有个比較特殊的case leetcode OJ没有覆盖)
    强名称程序集(strong name assembly)——为程序集赋予强名称
    虚函数练习:交通工具信息
    99 位职业设计师 99 个设计谏言
    在智能创业的风口鼓风,全国首个民间资本为主的物联网行业投融资平台诞生!
    具体解释。。设计模式5——DAO。。studying
    Android新控件RecyclerView剖析
  • 原文地址:https://www.cnblogs.com/wangshouchang/p/6918892.html
Copyright © 2020-2023  润新知