我爱编程

mongodb基本语法

2018-03-29  本文已影响0人  猫久伴你入眠

参考链接 https://www.tutorialspoint.com/mongodb/index.htm

一.创建和删除数据库

1.查看数据库列表

>show dbs
admin  0.000GB
local  0.000GB

2.使用名称为newdb的数据库,没有则自动创建

>use newdb
switched to db newdb

3.查看当前数据库,db代表当前使用的数据库

>db
newdb

4.查看当前数据库列表,刚刚创建的newdb并不在列表中,需要至少插入一个集合

>show dbs
admin  0.000GB
local  0.000GB

5.当前数据库下创建名称为users的集合

> db.createCollection("users")
{ "ok" : 1 }

6.此时查看数据库列表,已经显示刚刚创建的数据库newdb

> show dbs
admin  0.000GB
local  0.000GB
newdb  0.000GB

7.删除当前数据库

> db.dropDatabase()
{ "dropped" : "newdb", "ok" : 1 }

8.查看数据库列表,newdb已经成功删除

> show dbs
admin  0.000GB
local  0.000GB
二.数据集合的创建与删除

1.创建名为mycollection的集合到test数据库

> use test
switched to db test
> db.createCollection("mycollection")
{ "ok" : 1 }

2.显示集合列表

> show collections
mycollection

3.再创建一个

> db.createCollection("articles")
{ "ok" : 1 }
> show collections
articles
mycollection

4.删除名为articles的集合

> db.articles.drop()
true
> show collections
mycollection

5.再删除一个

> db.mycollection.drop()
true
> show collections

6.此时集合为空,数据库test不会在列表中显示

> db
test
> show dbs
admin  0.000GB
local  0.000GB
三.文档的插入
语法
>db.COLLECTION_NAME.insert(document)

1.在mycol的集合中插入一条数据,如果没有mycol的集合,则自动创建,并插入数据

> use test
switched to db test
> db.mycol.insert({"name":"马里奥",age:28})
WriteResult({ "nInserted" : 1 })
四.文档的查询
语法
>db.COLLECTION_NAME.find()

1.查看所有数据,_id为自动生成

> db.mycol.find({})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "马里奥", "age" : 28 }

2.再插入两条数据

> db.mycol.insert({"name":"汤姆",age:2})
WriteResult({ "nInserted" : 1 })
> db.mycol.insert({"name":"杰瑞",age:99})
WriteResult({ "nInserted" : 1 })

3.查看所有数据

> db.mycol.find({})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "马里奥", "age" : 28 }
{ "_id" : ObjectId("5abc4bcb3e7971325d6c6618"), "name" : "汤姆", "age" : 2 }
{ "_id" : ObjectId("5abc4bea3e7971325d6c6619"), "name" : "杰瑞", "age" : 99 }

4.查看姓名为"马里奥"的数据

> db.mycol.find({"name":"马里奥"})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "马里奥", "age" : 28 }

5.查看年龄大于30的数据

> db.mycol.find({age:{$gt:30}})
{ "_id" : ObjectId("5abc4bea3e7971325d6c6619"), "name" : "杰瑞", "age" : 99 }

6.查看年龄小于30的数据,其他(小于:$lt,大于:$gt,小于等于:$lte,大于等于:$gte,不等于:$ne)

> db.mycol.find({age:{$lt:30}})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "马里奥", "age" : 28 }
{ "_id" : ObjectId("5abc4bcb3e7971325d6c6618"), "name" : "汤姆", "age" : 2 }

7.查看年龄为1或者年龄为99的数据

> db.mycol.find({$or:[{"age":1},{"age":99}]})
{ "_id" : ObjectId("5abc4bea3e7971325d6c6619"), "name" : "杰瑞", "age" : 99 }
五.文档的更新
语法
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

1.将name为"杰瑞"的年龄改为98

> db.mycol.update({"name":"杰瑞"},{$set:{"age":98}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycol.find({"name":"杰瑞"})
{ "_id" : ObjectId("5abc4bea3e7971325d6c6619"), "name" : "杰瑞", "age" : 98}
六.文档的删除
语法
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

1.删除name为"杰瑞"的数据

> db.mycol.remove({"name":"杰瑞"})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find({})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "马里奥", "age" : 28 }
{ "_id" : ObjectId("5abc4bcb3e7971325d6c6618"), "name" : "汤姆", "age" : 2 }

2.删除年龄大于5的数据

> db.mycol.remove({age:{$gt:5}})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find({})
{ "_id" : ObjectId("5abc4bcb3e7971325d6c6618"), "name" : "汤姆", "age" : 2 }

3.删除所有

> db.mycol.remove({})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find({})

上一篇 下一篇

猜你喜欢

热点阅读