欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongodb入门教程之命令汇总

程序员文章站 2024-01-22 22:57:10
...

MongoDB 是由 C++ 语言编写的一个面向文档存储的数据库,操作起来比较简单和容易。MongoDB旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。同时MongoDB也是最适合前端小伙伴学习的一门数据库。

本文对mongodb的基本命令进行了汇总,希望对学习mongodb的小伙伴有所帮助!

1、挂机命令
>mongod --dbpath d:\mongo

–dbpath指的是存储数据库文件的文件夹。一旦执行以上命令就不要关了。一旦出现问题,数据库就会自动关闭了。如果需要人为退出可以ctrl+c

2、批量导入数据

挂机命令执行后,可以最小化,不要管它了!
首先咱们可以在d盘根目录创建一个名字叫做data的txt文件,内容为:

{"name":"张三","age":18,"sex":"男","transcript":{"yuwen":50,"shuxue":100,"yingyu":90}}
{"name":"李四","age":212,"sex":"女","transcript":{"yuwen":10,"shuxue":100,"yingyu":90}}
{"name":"王五","age":31,"sex":"男","transcript":{"yuwen":12,"shuxue":4356,"yingyu":45}}
{"name":"赵六","age":12,"sex":"女","transcript":{"yuwen":50,"shuxue":56,"yingyu":12}}
{"name":"严七","age":56,"sex":"男","transcript":{"yuwen":78,"shuxue":100,"yingyu":90}}
{"name":"沈八","age":67,"sex":"女","transcript":{"yuwen":78,"shuxue":34,"yingyu":1}}

然后重新打开一个cmd。输入如下命令:

>mongoimport --db zhang --collection score --drop --file d:\data.txt

mongoimport为mongodb的导入数据库命令。通过该命令可以将data.txt文件当中的数据导入到名字为zhang数据库中的集合名为score的集合当中。
–db zhang:指定导入的数据库,如果数据库不存在会新建。
–collection score:指定导入的集合,如果集合不存在会新建。
–drop:将集合清空后添加数据库。省略该项为在原有数据的基础上追加数据
–file d:\data.txt:导入的文件目录

3、通过mongo命令使用数据库

执行以下命令,运行环境即是mongo的环境了。可以在该环境下执行对数据库的增删改查操作。

>mongo
注:后面的所有命令均在该环境下执行
4、列出所有数据库
> show dbs
admin  0.000GB
local  0.000GB
zhang  0.000GB

admin与local为自带数据库,zhang为刚刚创建的数据库。

4、使用数据库
> use zhang
switched to db zhang

以上代码即是要使用名字叫zhang的数据库。
注:如果要新建数据库,也是use。use一个不存在的,就是新建。比如:

> use wang
switched to db wang

然后查看数据库:

> show dbs
admin  0.000GB
local  0.000GB
zhang  0.000GB

你会发现刚才新建的wang数据库并没有出现在列表中,而要显示它,我们需要在数据库wang中插入数据。

5、插入数据库

首先咱们先创建一个数据库wang

> use wang
switched to db wang

数据库中不能直接插入数据,只能在集合(collection)中插入数据:

> db.userList.insert({name:"zhangpeiyue"})
WriteResult({ "nInserted" : 1 })

userList指的是集合。集合中存储着很多JSON。需要注意的是userList是第一次使用,集合会自动创建。否则为在原来的基础上追加数据。
通过db.userList.find()查看集合当中的数据:

> db.userList.find()
{ "_id" : ObjectId("5b6ef62d90b88aa984b5ed21"), "name" : "zhangpeiyue" }

查看当前数据库列表:

> show dbs
admin  0.000GB
local  0.000GB
wang   0.000GB
zhang  0.000GB

到此你会发现数据库wang也已经创建好了。

6、查看当前所使用数据库
> use wang
switched to db wang
> db
wang
> use zhang
switched to db zhang
> db
zhang

通过db命令可以查看当前所在的数据库。

7、删除数据库

假如现在要将数据库wang删除掉,首先要进入到该数据库中:

> use wang
switched to db wang

然后执行dropDatabase命令:

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

查看数据库列表:

> show dbs
admin  0.000GB
local  0.000GB
zhang  0.000GB

你会发现数据库wang通过db.dropDatabase()命令删除掉了。

8、查找数据

进入到数据库zhang

> use zhang
switched to db zhang

通过find可以查找数据库zhang中集合为score的所有文档

> db.score.find()
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb526"), "name" : "张三", "age" : 18, "sex" : "男", "transcript" : { "yuwen" : 50, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb527"), "name" : "严七", "age" : 56, "sex" : "男", "transcript" : { "yuwen" : 78, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb528"), "name" : "沈八", "age" : 67, "sex" : "女", "transcript" : { "yuwen" : 78, "shuxue" : 34, "yingyu" : 1 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb529"), "name" : "李四", "age" : 212, "sex" : "女", "transcript" : { "yuwen" : 10, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52a"), "name" : "王五", "age" : 31, "sex" : "男", "transcript" : { "yuwen" : 12, "shuxue" : 4356, "yingyu" : 45 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52b"), "name" : "赵六", "age" : 12, "sex" : "女", "transcript" : { "yuwen" : 50, "shuxue" : 56, "yingyu" : 12 } }

精确查找:查找所有性别为男的文档:

> db.score.find({sex:"男"})
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb526"), "name" : "张三", "age" : 18, "sex" : "男", "transcript" : { "yuwen" : 50, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb527"), "name" : "严七", "age" : 56, "sex" : "男", "transcript" : { "yuwen" : 78, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52a"), "name" : "王五", "age" : 31, "sex" : "男", "transcript" : { "yuwen" : 12, "shuxue" : 4356, "yingyu" : 45 } }

多个条件:查找所有性别为男,且语文考了78分的文档:

> db.score.find({sex:"男","transcript.yuwen":78})
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb527"), "name" : "严七", "age" : 56, "sex" : "男", "transcript" : { "yuwen" : 78, "shuxue" : 100, "yingyu" : 90 } }

大于条件:查找所有年龄大于50的文档:

> db.score.find({age:{$gt:50}})
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb527"), "name" : "严七", "age" : 56, "sex" : "男", "transcript" : { "yuwen" : 78, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb528"), "name" : "沈八", "age" : 67, "sex" : "女", "transcript" : { "yuwen" : 78, "shuxue" : 34, "yingyu" : 1 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb529"), "name" : "李四", "age" : 212, "sex" : "女", "transcript" : { "yuwen" : 10, "shuxue" : 100, "yingyu" : 90 } }

//$gt:大于    $lt:小于    $gte:大于或等于  $lte:小于或等于

模糊查找:查找名字带有张的文档

> db.score.find({name:/张/})
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb526"), "name" : "张三", "age" : 18, "sex" : "男", "transcript" : { "yuwen" : 50, "shuxue" : 100, "yingyu" : 90 } }

或:查找年龄为12或18的文档

> db.score.find({$or:[{age:12},{age:18}]})
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb526"), "name" : "张三", "age" : 18, "sex" : "男", "transcript" : { "yuwen" : 50, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52b"), "name" : "赵六", "age" : 12, "sex" : "女", "transcript" : { "yuwen" : 50, "shuxue" : 56, "yingyu" : 12 } }
查找后的信息可以通过sort进行排序(1为正序,-1为倒序)

按照年龄的正序排列:

> db.score.find().sort({age:1})
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52b"), "name" : "赵六", "age" : 12, "sex" : "女", "transcript" : { "yuwen" : 50, "shuxue" : 56, "yingyu" : 12 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb526"), "name" : "张三", "age" : 18, "sex" : "男", "transcript" : { "yuwen" : 50, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52a"), "name" : "王五", "age" : 31, "sex" : "男", "transcript" : { "yuwen" : 12, "shuxue" : 4356, "yingyu" : 45 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb527"), "name" : "严七", "age" : 56, "sex" : "男", "transcript" : { "yuwen" : 78, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb528"), "name" : "沈八", "age" : 67, "sex" : "女", "transcript" : { "yuwen" : 78, "shuxue" : 34, "yingyu" : 1 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb529"), "name" : "李四", "age" : 212, "sex" : "女", "transcript" : { "yuwen" : 10, "shuxue" : 100, "yingyu" : 90 } }

按照语文的成绩倒序排列,如果语文成绩相同,则按年龄的正序排列

> db.score.find().sort({"score.yuwen":-1,age:1})
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52b"), "name" : "赵六", "age" : 12, "sex" : "女", "transcript" : { "yuwen" : 50, "shuxue" : 56, "yingyu" : 12 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb526"), "name" : "张三", "age" : 18, "sex" : "男", "transcript" : { "yuwen" : 50, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52a"), "name" : "王五", "age" : 31, "sex" : "男", "transcript" : { "yuwen" : 12, "shuxue" : 4356, "yingyu" : 45 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb527"), "name" : "严七", "age" : 56, "sex" : "男", "transcript" : { "yuwen" : 78, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb528"), "name" : "沈八", "age" : 67, "sex" : "女", "transcript" : { "yuwen" : 78, "shuxue" : 34, "yingyu" : 1 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb529"), "name" : "李四", "age" : 212, "sex" : "女", "transcript" : { "yuwen" : 10, "shuxue" : 100, "yingyu" : 90 } }
可以通过limit取指定的条数

显示前两条记录:

> db.score.find().limit(2)
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb526"), "name" : "张三", "age" : 18, "sex" : "男", "transcript" : { "yuwen" : 50, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb527"), "name" : "严七", "age" : 56, "sex" : "男", "transcript" : { "yuwen" : 78, "shuxue" : 100, "yingyu" : 90 } }

limit指定数字为0,则显示所有数据。在此不再举例。

可以通过skip跳过指定的条数

跳过前两条记录

> db.score.find().skip(2)
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb528"), "name" : "沈八", "age" : 67, "sex" : "女", "transcript" : { "yuwen" : 78, "shuxue" : 34, "yingyu" : 1 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb529"), "name" : "李四", "age" : 212, "sex" : "女", "transcript" : { "yuwen" : 10, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52a"), "name" : "王五", "age" : 31, "sex" : "男", "transcript" : { "yuwen" : 12, "shuxue" : 4356, "yingyu" : 45 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52b"), "name" : "赵六", "age" : 12, "sex" : "女", "transcript" : { "yuwen" : 50, "shuxue" : 56, "yingyu" : 12 } }
另外sort,limit,skip可以结合使用

按年龄的正序,跳过2条记录,显示前3条记录:

> db.score.find().sort({age:1}).skip(2).limit(3)
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb52a"), "name" : "王五", "age" : 31, "sex" : "男", "transcript" : { "yuwen" : 12, "shuxue" : 4356, "yingyu" : 45 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb527"), "name" : "严七", "age" : 56, "sex" : "男", "transcript" : { "yuwen" : 78, "shuxue" : 100, "yingyu" : 90 } }
{ "_id" : ObjectId("5b6eef02b9a20d17c08cb528"), "name" : "沈八", "age" : 67, "sex" : "女", "transcript" : { "yuwen" : 78, "shuxue" : 34, "yingyu" : 1 } }
9、修改记录

将名字为“王五”的年龄修改为100

> db.score.update({name:"王五"},{$set:{age:100}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

将所有男同胞,年龄修改为1000

> db.score.update({sex:"男"},{$set:{age:1000}}) )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

你会发现每次只能有一条记录被修改。
如果有修改多条,可以通过multi来实现。
将所有男同胞,年龄修改为10000

> db.score.update({sex:"男"},{$set:{age:10000}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

完整替换,只需要将$set去掉即可:

> db.score.update({sex:"男"},{age:10000})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

将张三的年龄在原来的基础上加1

> db.score.update({name:"张三"},{$inc:{age:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
10、删除数据(remove)

删除所有女同胞:

> db.score.remove({sex:"女"}))
WriteResult({ "nRemoved" : 3 })

删除所有男同胞,且只删除一条:

> db.score.remove({sex:"男"},{justOne:true})
WriteResult({ "nRemoved" : 1 })

删除掉集合中的所有文档:

> db.score.remove({})
WriteResult({ "nRemoved" : 2 })
11、显示当前数据库当中的所有集合
> show collections
score

mongodb入门教程之命令汇总