MongoDB University笔记总结-M001_Chapter3 Creating and Manipulating Documents

Source: MongoDB University   https://university.mongodb.com/

Course: M001 MongoDB bascis

 

【Chapter3】: Creating and Manipulating Documents  

Key points:

1.Inserting New Documents  - insert() and errors

2.Inserting New Documents - insert() Order

3.Updateing Documents - Data Explorer 

4.Updating Documents - mongo shell

5.Deleting Documents and Collections

 

#------------------------

连接MongoDB

1.在mongo shell中连接数据库

1)cmd界面输入下面这行连接数据库,输入账号密码

mongo "mongodb+srv://sandbox.*****.mongodb.net/<dbname>" --username m001-student

2)连接成功,即可在命令行输入语句操作数据。

常用语句如下:

show dbs // 显示数据库名称

use sample_training //使用其中sample_training的数据库

use collections // 显示sample_trainging 里面的表格

db.inspections.findOne(); //获inspections表中中任意数据

db.inspections.find().pretty()  //将inspections中全部数据已更可读的格式展示出来(用了pretty()函数)

db.inspections.find({ "city": "HUDSON" }).count()  //统计 inspections表格中,city = HUDSON的数据有多少行,

 

 

1 & 2  Inserting New Documents  - insert() &  errors  &  Order

下面以pet表格为例子

插入数据:

db.pet.insert( {"_id":1,"pet":"cat"}, {"_id":2,"pet":"dog"},{"_id":3,"pet":"sheep"})

即插入了3条数据,如果不定义"_id" 唯一标识列,则系统会自己生成该行。

 

如果自定义的_id有重复的情况

 情况1:"_id":1  出现两次

MongoDB Enterprise atlas-mgr67a-shard-0:PRIMARY> db.pet.insert([ {"_id":1,"pet":"cat"}, {"_id":1,"pet":"dog"},{"_id":3,"pet":"sheep"}])

运行结果显示:只插入了一行数据,因为第二行数据中"_id":1 重复,则程序不再往下进行了

写插入数据的语句时候,记得" [ ]" 用方括号将插入的数据包括在内。

情况2:"_id":1  出现两次,后面语句加上{"ordered":false}。插入数据之前将pet表删掉重新插入数据。

db.pet.drop()

db.pet.insert([{"_id":1,"pet":"cat"},{"_id":1,"pet":"dog"},{"_id":2,"pet":"chicken"}],{"ordered":false})

MongoDB Enterprise atlas-mgr67a-shard-0:PRIMARY> db.pet.insert([{"_id":1,"pet
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值