Mongodb官方文档地址:http://docs.mongodb.com/manual
Mongodb像mysql,oracle的RDBMS一样,也支持CRUD操作,类似于平常写的sql。
Insert操作如果集合不存在,将会创建collection,没有指定_id字段,mongodb将自动创建_id字段,并且生成一个唯一的ObjectId值
向user集合中插入一条记录:db.user.insertOne({
"name":"yangfu",
"age":25,
"gender":"male"
})
返回_id字段,ObjectId("5e722d63e8b6a86e483ad6fa")
向user中插入多条记录
db.user.insertMany(
[{"name":"张三","age":20,"gender":"male"},
{"name":"李四","age":21,"gender":"male"}])
返回两个_id字段值
insert的语法:db.collection.insert(
<document or array of documents>, 单个文档或者文档的数组
{
writeConcern: <document>,
ordered: <boolean>,数组的数据插入时是否按照顺序
}
)
单条记录插入返回WriteResult
多条记录批量插入返回BulkWriteResult