MongoDB 基础命令

下载安装后


1.在 “你自己MongoDB安装目录/bin” 下按Shift+鼠标右键后选择打开PowerShell(需要输入.\)或cmd(不需要输入.\)
2.输入[.\]mongod --dbpath XXX(任意指定但最好在你自己MongoDB安装目录\data下)确认后就打开了MongoDB的服务端
3.再打开一个PowerShell或cmd输入:[.\]mongo就打开了MongoDB的客户端(也不知道啥情况,有时候连接服务端的时候还要去服务端随便按点什么)

1.展示数据库里的表

> show dbs;
admin    0.000GB
config   0.000GB
local    0.000GB

2.使用某个数据库

> use students
switched to db students

这里可以直接选择尚不存在的数据库,如果有写入等操作后,表(文档)会被直接创建

3.查看正在使用的数据库

> db
students

4. 创建一个对象(

> stu = {
... name: 'Jhon',
... age: 21}
{ "name" : "Jhon", "age" : 21 }

5.把对象stu插入到表students中去

> db.students.insert(stu)
WriteResult({ "nInserted" : 1 })

6.查询students中的数据

> db.students.find()
{ "_id" : ObjectId("5c7e585576ba887d0e8629d0"), "name" : "Jhon", "age" : 21 }

其中的_id是MongoDB自动创建并维护的类似主键的字段

7.在不建立对象的情况下插入数据

> db.students.insert({name: 'Amy'})
WriteResult({ "nInserted" : 1 })

8.查询一条数据

> db.students.findOne()
{
        "_id" : ObjectId("5c7e585576ba887d0e8629d0"),
        "name" : "Jhon",
        "age" : 21
}

数据被格式化了

9.修改某条数据

> obj = db.students.findOne()
{
        "_id" : ObjectId("5c7e585576ba887d0e8629d0"),
        "name" : "Jhon",
        "age" : 21
}
> obj.name = "lux"
lux
> db.students.update({name:'Jhon'}, obj)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.findOne()
{ "_id" : ObjectId("5c7e585576ba887d0e8629d0"), "name" : "lux", "age" : 21 }

先把某条数据查询出来赋值给一个变量,
然后改变变量的一条属性,
然后更新满足条件的记录。(好傻)
为啥最后一句的findOne不格式化了??(⊙_⊙)?

10.删除数据

> db.students.find()
{ "_id" : ObjectId("5c7e585576ba887d0e8629d0"), "name" : "lux", "age" : 21 }
{ "_id" : ObjectId("5c7e598176ba887d0e8629d1"), "name" : "Amy" }
> db.students.remove({name: 'Amy'})
WriteResult({ "nRemoved" : 1 })
> db.students.find()
{ "_id" : ObjectId("5c7e585576ba887d0e8629d0"), "name" : "lux", "age" : 21 }

先查询看到有两条数据,
执行删除满足条件的记录,
再查询看看是少了一条

11.清空表

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

从删库到跑路

任务

1.创建一个学生信息表(至少包含:姓名,性别,成绩,年龄)
2.写入十五条不同的数据
3.查询所有的男生数据(只需要学生的年龄和姓名)
4.查询成绩及格的学生的信息
5.查询所有18岁的男生和16岁女生的数据
6.按照年龄对学生进行排序
7.将所有的女学生年龄增加一岁

练习

1.创建一个学生信息表(至少包含:姓名,性别,成绩,年龄)
2.写入十五条不同的数据

> db.students.insertMany(
...   [
...     { name: 'bob', age: 16, sex: 'male', grade: 95},
...     { name: 'lulu', age: 16, sex: 'female', grade: 50},
...     { name: 'san', age: 18, sex: 'male', grade: 80},
...     { name: 'mac', age: 16, sex: 'male', grade: 60},
...     { name: 'sli', age: 18, sex: 'female', grade: 70},
...     { name: 'tom', age: 20, sex: 'male', grade: 90},
...     { name: 'tim', age: 17, sex: 'male', grade: 50},
...     { name: 'ann', age: 80, sex: 'female', grade: 60},
...     { name: 'joy', age: 45, sex: 'female', grade: 45},
...     { name: 'wm', age: 13, sex: 'female', grade: 60},
...     { name: 'ty', age: 14, sex: 'female', grade: 70},
...     { name: 'lu', age: 15, sex: 'male', grade: 75},
...     { name: 'le', age: 16, sex: 'male', grade: 80},
...     { name: 'jo', age: 14, sex: 'male', grade: 75},
...     { name: 'lce', age: 16, sex: 'female', grade: 85},
...   ]
... )
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5c7e689f76ba887d0e8629df"),
                ObjectId("5c7e689f76ba887d0e8629e0"),
                ObjectId("5c7e689f76ba887d0e8629e1"),
                ObjectId("5c7e689f76ba887d0e8629e2"),
                ObjectId("5c7e689f76ba887d0e8629e3"),
                ObjectId("5c7e689f76ba887d0e8629e4"),
                ObjectId("5c7e689f76ba887d0e8629e5"),
                ObjectId("5c7e689f76ba887d0e8629e6"),
                ObjectId("5c7e689f76ba887d0e8629e7"),
                ObjectId("5c7e689f76ba887d0e8629e8"),
                ObjectId("5c7e689f76ba887d0e8629e9"),
                ObjectId("5c7e689f76ba887d0e8629ea"),
                ObjectId("5c7e689f76ba887d0e8629eb"),
                ObjectId("5c7e689f76ba887d0e8629ec"),
                ObjectId("5c7e689f76ba887d0e8629ed")
        ]
}

> db.students.find()
{ "_id" : ObjectId("5c7e689f76ba887d0e8629df"), "name" : "bob", "age" : 16, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e0"), "name" : "lulu", "age" : 16, "sex" : "female", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e2"), "name" : "mac", "age" : 16, "sex" : "male", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e3"), "name" : "sli", "age" : 18, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e4"), "name" : "tom", "age" : 20, "sex" : "male", "grade" : 90 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e5"), "name" : "tim", "age" : 17, "sex" : "male", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e6"), "name" : "ann", "age" : 80, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e7"), "name" : "joy", "age" : 45, "sex" : "female", "grade" : 45 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e8"), "name" : "wm", "age" : 13, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e9"), "name" : "ty", "age" : 14, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ea"), "name" : "lu", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629eb"), "name" : "le", "age" : 16, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ec"), "name" : "jo", "age" : 14, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ed"), "name" : "lce", "age" : 16, "sex" : "female", "grade" : 85 }

为了方便这里写一个文本文件

db.students.insertMany(
  [
    { name: 'bob', age: 16, sex: 'male', grade: 95},
    { name: 'lulu', age: 16, sex: 'female', grade: 50},
    { name: 'san', age: 18, sex: 'male', grade: 80},
    { name: 'mac', age: 16, sex: 'male', grade: 60},
    { name: 'sli', age: 18, sex: 'female', grade: 70},
    { name: 'tom', age: 20, sex: 'male', grade: 90},
    { name: 'tim', age: 17, sex: 'male', grade: 50},
    { name: 'ann', age: 80, sex: 'female', grade: 60},
    { name: 'joy', age: 45, sex: 'female', grade: 45},
    { name: 'wm', age: 13, sex: 'female', grade: 60},
    { name: 'ty', age: 14, sex: 'female', grade: 70},
    { name: 'lu', age: 15, sex: 'male', grade: 75},
    { name: 'le', age: 16, sex: 'male', grade: 80},
    { name: 'jo', age: 14, sex: 'male', grade: 75},
    { name: 'lce', age: 16, sex: 'female', grade: 85},
  ]
)

3.查询所有的男生数据(只需要学生的年龄和姓名)

> db.students.find({sex: 'male'})
{ "_id" : ObjectId("5c7e689f76ba887d0e8629df"), "name" : "bob", "age" : 16, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e2"), "name" : "mac", "age" : 16, "sex" : "male", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e4"), "name" : "tom", "age" : 20, "sex" : "male", "grade" : 90 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e5"), "name" : "tim", "age" : 17, "sex" : "male", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ea"), "name" : "lu", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629eb"), "name" : "le", "age" : 16, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ec"), "name" : "jo", "age" : 14, "sex" : "male", "grade" : 75 }

但这不符合题目要求

> db.students.find({sex: 'male'},{name: 1, age: true, _id: 0})
{ "name" : "bob", "age" : 16 }
{ "name" : "san", "age" : 18 }
{ "name" : "mac", "age" : 16 }
{ "name" : "tom", "age" : 20 }
{ "name" : "tim", "age" : 17 }
{ "name" : "lu", "age" : 15 }
{ "name" : "le", "age" : 16 }
{ "name" : "jo", "age" : 14 }

其中name后的1和age后的true是等价的,0和false是等价的
4.查询及格的学生的信息

> db.students.find({grade: {'$gte': 60}})
{ "_id" : ObjectId("5c7e689f76ba887d0e8629df"), "name" : "bob", "age" : 16, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e2"), "name" : "mac", "age" : 16, "sex" : "male", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e3"), "name" : "sli", "age" : 18, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e4"), "name" : "tom", "age" : 20, "sex" : "male", "grade" : 90 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e6"), "name" : "ann", "age" : 80, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e8"), "name" : "wm", "age" : 13, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e9"), "name" : "ty", "age" : 14, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ea"), "name" : "lu", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629eb"), "name" : "le", "age" : 16, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ec"), "name" : "jo", "age" : 14, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ed"), "name" : "lce", "age" : 16, "sex" : "female", "grade" : 85 }

5.查询所有18岁的男生和16岁女生的数据

> db.students.find({'$or': [{sex: 'male', age: 18}, {sex: 'female', age: 16}]})
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e0"), "name" : "lulu", "age" : 16, "sex" : "female", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ed"), "name" : "lce", "age" : 16, "sex" : "female", "grade" : 85 }

6.按照年龄对学生进行排序

> db.students.find().sort({age: 1})
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e8"), "name" : "wm", "age" : 13, "sex" : "female", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e9"), "name" : "ty", "age" : 14, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ec"), "name" : "jo", "age" : 14, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ea"), "name" : "lu", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629df"), "name" : "bob", "age" : 16, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e0"), "name" : "lulu", "age" : 16, "sex" : "female", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e2"), "name" : "mac", "age" : 16, "sex" : "male", "grade" : 60 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629eb"), "name" : "le", "age" : 16, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629ed"), "name" : "lce", "age" : 16, "sex" : "female", "grade" : 85 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e5"), "name" : "tim", "age" : 17, "sex" : "male", "grade" : 50 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e1"), "name" : "san", "age" : 18, "sex" : "male", "grade" : 80 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e3"), "name" : "sli", "age" : 18, "sex" : "female", "grade" : 70 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e4"), "name" : "tom", "age" : 20, "sex" : "male", "grade" : 90 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e7"), "name" : "joy", "age" : 45, "sex" : "female", "grade" : 45 }
{ "_id" : ObjectId("5c7e689f76ba887d0e8629e6"), "name" : "ann", "age" : 80, "sex" : "female", "grade" : 60 }

age后面那个1代表升序,如果是 -1代表降序
7.将所有的女学生年龄增加一岁

> db.students.update({sex: 'female'}, {'$inc': {age: 1}}, {multi: true})
WriteResult({ "nMatched" : 15, "nUpserted" : 0, "nModified" : 15 })

这里用到了修改器,其中 '$inc': {age: 1}是指age增加1,multi=true指多条数据
'$set': {XXX: XXX }可以用来新增属性

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容

  • 安装后在控制台输入: mongo启动client。 show dbs显示所有数据库。 use xxx选中数据库为当...
    1angxi阅读 955评论 0 1
  • 简介 MongoDB 是一个基于分布式文件存储的NoSQL数据库 由C++语言编写,运行稳定,性能高 旨在为 WE...
    大熊_7d48阅读 35,902评论 1 9
  • MYSQL 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 MySQL基本操...
    Kingtester阅读 7,772评论 5 116
  • 我有一个很要好的朋友,从小玩到大的那种。我们俩都属于死要面子活受罪的类型。可随着年龄的增长,现实的残酷,无数次低下...
    ZJ一人一心阅读 154评论 0 0