MongoDB之学习【四】:mongoDB基本操作 - 增、删、查

一、增加数据

1、insert 插入数据
> db.c5.insert({name:"user1"});
WriteResult({ "nInserted" : 1 })
> db.c5.insert({name:"user2"});
WriteResult({ "nInserted" : 1 })
> db.c5.insert({name:"user3"});
WriteResult({ "nInserted" : 1 })
> db.c5.insert({name:"user4"});
WriteResult({ "nInserted" : 1 })
> db.c5.insert({name:"user5"});
WriteResult({ "nInserted" : 1 })
> db.c5.find();
{ "_id" : ObjectId("5d5e7b310bccde4320cfbfaf"), "name" : "user1" }
{ "_id" : ObjectId("5d5e7b390bccde4320cfbfb0"), "name" : "user2" }
{ "_id" : ObjectId("5d5e7b3d0bccde4320cfbfb1"), "name" : "user3" }
{ "_id" : ObjectId("5d5e7b430bccde4320cfbfb2"), "name" : "user4" }
{ "_id" : ObjectId("5d5e7b4a0bccde4320cfbfb3"), "name" : "user5" }


> db.c5.insert({name:"zhangsan", sex:"male", weight:"160"});
WriteResult({ "nInserted" : 1 })
> db.c5.find();
{ "_id" : ObjectId("5d5e7b310bccde4320cfbfaf"), "name" : "user1" }
{ "_id" : ObjectId("5d5e7b390bccde4320cfbfb0"), "name" : "user2" }
{ "_id" : ObjectId("5d5e7b3d0bccde4320cfbfb1"), "name" : "user3" }
{ "_id" : ObjectId("5d5e7b430bccde4320cfbfb2"), "name" : "user4" }
{ "_id" : ObjectId("5d5e7b4a0bccde4320cfbfb3"), "name" : "user5" }
{ "_id" : ObjectId("5d5e7c720bccde4320cfbfb4"), "name" : "zhangsan", "sex" : "male", "weight" : "160" }
Insert还可以插入子json
> db.c5.insert({name:"Yixiantian", post:{title:"kungfu", desc:"BaChiQuan"}});
WriteResult({ "nInserted" : 1 })
> db.c5.find();
{ "_id" : ObjectId("5d5e7b310bccde4320cfbfaf"), "name" : "user1" }
{ "_id" : ObjectId("5d5e7b390bccde4320cfbfb0"), "name" : "user2" }
{ "_id" : ObjectId("5d5e7b3d0bccde4320cfbfb1"), "name" : "user3" }
{ "_id" : ObjectId("5d5e7b430bccde4320cfbfb2"), "name" : "user4" }
{ "_id" : ObjectId("5d5e7b4a0bccde4320cfbfb3"), "name" : "user5" }
{ "_id" : ObjectId("5d5e7c720bccde4320cfbfb4"), "name" : "zhangsan", "sex" : "male", "weight" : "160" }
{ "_id" : ObjectId("5d5e7ce60bccde4320cfbfb5"), "name" : "user6" }
{ "_id" : ObjectId("5d5e7d0a0bccde4320cfbfb6"), "name" : "user6" }
{ "_id" : 5, "name" : "user55" }
{ "_id" : ObjectId("5d5f62cf8ee97b67104bcc89"), "name" : "Yixiantian", "post" : { "title" : "kungfu", "desc" : "BaChiQuan" } }
Insert 也可以插入数组
> db.c5.insert({name:"YeWen", student:["Bruce Lee", "liang", "many"]});
WriteResult({ "nInserted" : 1 })
> db.c5.find();
{ "_id" : ObjectId("5d5e7b310bccde4320cfbfaf"), "name" : "user1" }
{ "_id" : ObjectId("5d5e7b390bccde4320cfbfb0"), "name" : "user2" }
{ "_id" : ObjectId("5d5e7b3d0bccde4320cfbfb1"), "name" : "user3" }
{ "_id" : ObjectId("5d5e7b430bccde4320cfbfb2"), "name" : "user4" }
{ "_id" : ObjectId("5d5e7b4a0bccde4320cfbfb3"), "name" : "user5" }
{ "_id" : ObjectId("5d5e7c720bccde4320cfbfb4"), "name" : "zhangsan", "sex" : "male", "weight" : "160" }
{ "_id" : ObjectId("5d5e7ce60bccde4320cfbfb5"), "name" : "user6" }
{ "_id" : ObjectId("5d5e7d0a0bccde4320cfbfb6"), "name" : "user6" }
{ "_id" : 5, "name" : "user55" }
{ "_id" : ObjectId("5d5f62cf8ee97b67104bcc89"), "name" : "Yixiantian", "post" : { "title" : "kungfu", "desc" : "BaChiQuan" } }
{ "_id" : ObjectId("5d5f64408ee97b67104bcc8a"), "name" : "YeWen", "student" : [ "Bruce Lee", "liang", "many" ] }
建议:不要插入层级太复杂的数据,否则,PHP来处理数据的时候,会非常麻烦!

2、save 插入数据
> db.c5.save({name:"user6"});
WriteResult({ "nInserted" : 1 })
> db.c5.find();
{ "_id" : ObjectId("5d5e7b310bccde4320cfbfaf"), "name" : "user1" }
{ "_id" : ObjectId("5d5e7b390bccde4320cfbfb0"), "name" : "user2" }
{ "_id" : ObjectId("5d5e7b3d0bccde4320cfbfb1"), "name" : "user3" }
{ "_id" : ObjectId("5d5e7b430bccde4320cfbfb2"), "name" : "user4" }
{ "_id" : ObjectId("5d5e7b4a0bccde4320cfbfb3"), "name" : "user5" }
{ "_id" : ObjectId("5d5e7c720bccde4320cfbfb4"), "name" : "zhangsan", "sex" : "male", "weight" : "160" }
{ "_id" : ObjectId("5d5e7ce60bccde4320cfbfb5"), "name" : "user6" }
如果再插入“user6”会怎么样?
> db.c5.save({name:"user6"});
WriteResult({ "nInserted" : 1 })
> db.c5.find();
{ "_id" : ObjectId("5d5e7b310bccde4320cfbfaf"), "name" : "user1" }
{ "_id" : ObjectId("5d5e7b390bccde4320cfbfb0"), "name" : "user2" }
{ "_id" : ObjectId("5d5e7b3d0bccde4320cfbfb1"), "name" : "user3" }
{ "_id" : ObjectId("5d5e7b430bccde4320cfbfb2"), "name" : "user4" }
{ "_id" : ObjectId("5d5e7b4a0bccde4320cfbfb3"), "name" : "user5" }
{ "_id" : ObjectId("5d5e7c720bccde4320cfbfb4"), "name" : "zhangsan", "sex" : "male", "weight" : "160" }
{ "_id" : ObjectId("5d5e7ce60bccde4320cfbfb5"), "name" : "user6" }
{ "_id" : ObjectId("5d5e7ce60bccde4320cfbfb6"), "name" : "user6" }
我们发现多了一条数据,那“user6”都已经有了,怎么还能插入“user6”? 这是因为这两条“user6”的id不同,只要id不同,那么它们就不是相同的数据。
插入一条自定义id的数据!
> db.c5.save({_id:5, name:"user55"});
WriteResult({ "nMatched" : 0, "nUpserted"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值