一、增加数据
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"