mongodb5文档操作1-插入数据
mongodb插入文档数据
1.insert插入单条数据
info = {
"name": "teacher01",
"num": 01,
"age": 33,
"sex": "man",
"phone": 18782940194,
"address": "成都龙泉"
}
db.teacher.insert(info)
2. insert插入多条数据
info1 = {
"_id":10,
"name": "teacher10",
"num": 10,
"age": 18,
"sex": "mn",
"phone": 18782940195,
"address": "成都10"
}
info2 = {
"_id":11,
"name": "teacher11",
"num": 11,
"age": 18,
"sex": "mn",
"phone": 18782940195,
"address": "成都11"
}
info3 = {
"_id":12,
"name": "teacher12",
"num": 12,
"age": 18,
"sex": "mn",
"phone": 18782940195,
"address": "成都12"
}
db.teacher.insert([info1,info2,info3])
3.插入数据并指定_id
info = {
"_id":3,
"name": "teacher03",
"num": 03,
"age": 18,
"sex": "woman",
"phone": 18782940195,
"address": "成都龙泉"
}
db.teacher.insert(info)
4.使用save插入数据
使用save时:
如果_id已经存在,则更新已有_id的数据;
如果_id不存在或者不指定_id则为新增数据