MongoDB是非关系型数据库NoSQL的代表,作为一款可分布式存储的数据库,对文档的操作是MongoDB的重中之重。在本文中,我们将着重为大家介绍如何在MongoDB中插入文档。
MongoDB一共为我们提供了三种方式用于插入文档:
使用insert语句可以将一个新的文档插入到一个collection中。如果该collection还不存在,insert语句会自动创建该collection。
db.collection.insertOne( <document>, { writeConcern: <document> } )
示例:插入一条学生数据:
db.collection.insertMany( [ <document 1> , <document 2>, ... ], { writeConcern: <document>, ordered: <boolean> } )
上面的方法主要有三个参数:
示例:
语法格式如下:
db.collection.insert( <document or array of documents>, { writeConcern: <document>, ordered: <boolean> } )
原文:https://www.cnblogs.com/collen7788/p/12702868.html