美文网首页我爱编程
mongodb基本语法

mongodb基本语法

作者: 猫久伴你入眠 | 来源:发表于2018-03-29 10:49 被阅读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({})

相关文章

  • mongodb基本语法

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

  • MongoDB基本语法

  • MongoDB 基本语法

    Mongodb 此数据库的语法都是 db打头 mac安装mongodb参考地址:http://www.jiansh...

  • MongoDB基本语法

    在用Mongo库的时候,无法用generator生成底层,所以需要自己写实现。 查询的话就用mongoTempla...

  • Mongodb基本语法

    术语解析 |--------|:--------:|| database | 数据库 || collection ...

  • MongoDB基本语法

    1.查看所有数据库: 2.切换到数据库test: 3.创建集合(创建数据库): 4.插入文档/数据: test是...

  • MongoDB学习记录(二)

    MongoDB学习记录(二) MongoDB的查询 基本查询语句 基本的查询语句的语法:db.[集合名].fin...

  • MongoDB基本语法(1)

    一、创建数据库 启动mongo后输入 创建test数据库之后show dbs没有test数据库,必须insert数...

  • MongoDB基本语法(2)

    show users //查看所有权限用户 一、删除集合 二、MongoDB 插入文档 MongoDB 使用...

  • MongoDB的基本语法

    MongoDB的基本用法 整体操作 查看数据库列表 切换(创建)到demo数据库(如果是创建该数据库则还需要往数据...

网友评论

    本文标题:mongodb基本语法

    本文链接:https://www.haomeiwen.com/subject/ubnpcftx.html