您的位置:首页 > 数据库 > Mongodb

MongoDB常用命令及SQL语法对比

2014-09-02 11:18 519 查看
传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。

1 客户端常用命令

数据库相关命令

1 show dbs // 列出所有数据库

2 use memo // 使用数据库memo。即使这个数据库不存在也可以执行,但该数据库不会立刻被新建,要等到执行了insert之类的操作时,才会建立这个数据库

3 show collections // 列出当前数据库的collections

4 查看各collection的状态 db.printCollectionStats()

5 db // 显示当前数据库

6 show users // 列出用户

7 db.system.users.find() // 列出用户

8 db.removeUser('user1') //删除用户

9 db.c1.drop()//删除集合c1

10 db.dropDatabase()//删除当前的数据库

安全与认证

1 use shine // 如果要root权限,就用admin库

2 db.addUser("username", "password") // 普通权限,可读写

3 db.addUser("username", "password", true) // 只可读,不可写

4 db.system.users.remove({user: username}) // 删除用户

生成主键

1 db.c1.save({name:"zhangsan",age:18}) //没有写主键,系统会自动生成一个主键,主键名为_id,

2 .每个MongoDB的document都有一个_id字段作为它的第一个属性,这个值通常是一个BSON对象id,因此,这个id对于集合中的每个成员 都是唯一的,如果用户插入一个document没有提供一个id,数据库将自动生成一个id,并存储在_id字段。

3. db.c1.save({_id:1,name:"lisi",age:22})//自己填写id主键值,id主键值可以是字符串类型,也可以是数字类型

4. db.c1.save({"name" : "MongoDB","type" : "database","count" : 1,"info" : {x : 203,y : 102}})

创建索引:

1 coll.ensureIndex({productid:1}) // 在productid上建立普通索引

2 coll.ensureIndex({district:1, plate:1}) // 多字段索引

3 coll.ensureIndex({productid:1}, {unique:true}) // 唯一索引

4 coll.ensureIndex({productid:1}, {unique:true, dropDups:true|) // 建索引时,如果遇到索引字段值已经出现过的情况,则删除重复记录

5 coll.getIndexes() // 查看索引

6 coll.dropIndex({productid:1}) // 删除单个索引

数据查找

1. db.coll.find() // select * from coll

2. db.coll.find().limit(10) // select * from coll limit 10

3. db.coll.find().sort({x:1}) // select * from coll order by x asc

4. db.coll.find().sort({x:-1}) // select * from coll order by x desc

5. db.coll.find().sort({x:1}).skip(5).limit(10) // select * from coll order by x asc limit 5, 10

6. db.coll.find({x:10}) // select * from coll where x = 10

7. db.coll.find({x: {$lt:10}}) // select * from coll where x <= 10

8. db.coll.find({}, {y:true}) // select y from coll

通过游标访问数据

.> var cursor = db.collect1.find();

> while(cursor.hasNext()) printjson(cursor.next())

当数据超过20行时候,使用it命令查看更多数据 通过forEach

10> db.collect1.find().forEach(printjson)

11. 在mongo shell中,你可以将游标认为是数组

12> var cursor =db.collect1.find();

13.> printjson(cursor[4])

14{ "_id" : ObjectId("4c691e72ed2a47b462dfa806"), "x" : 4, "y" : 3 }

使用这种方式存取需要注意的是,在cursor[4]之前的所有数据都会同时被加载到内存,对于很大的结果集,这样操作是不恰当的,会导致内存溢出,当查询巨大数据量大时候,游标应当作为迭代器使用。

SQL语句不能做到的,mongo也可以做到

1. coll.find({"address.city":"gz"}) // 搜索嵌套文档address中city值为gz的记录

2. coll.find({likes:"math"}) // 搜索数组

3. coll.ensureIndex({"address.city":1}) // 在嵌套文档的字段上建

1. db.user.update({uid:1},{$set:{age:26}}) //update user set age=26 where uid=1

2. db.user.update({uid:1},{$inc:{age:1}}) //update user set age=age+1 where uid=1 1.db.user.delete({uid:1})//delete user where uid=1

2.db.Position.remove({"id":10}) //delete * from Position where id=10 1. // json或csv格式,每次一个collection

mongoexport -d producttrade -c basic -o /home/data/mongo_backup/producttrade_100504.json

145.3. mongoimport -d producttrade -c basic --drop /home/data/mongo_backup/producttrade_100504.json // 二进制数据格式,常用于备份、还原

2 mongodump -d shine -o /home/data/mongo_backup

3 mongorestore -d shine --drop /home/data/mongo_backup/shine

2 sql语法对比





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: