3.13. MongoDB inserts a document

发布时间 : 2025-10-25 13:34:22 UTC      

Page Views: 9 views

In this section, we will show you how to insert data into a collection of MongoDB.

The data structure of the document is basically the same as that of JSON.

All data stored in the collection is in BSON format.

BSON is a binary storage format similar to JSON, which is short for Binary JSON.

3.13.1. Insert document

MongoDB usage insert() Or save() Method to insert a document into the collection with the following syntax:

db.COLLECTION_NAME.insert(document)  db.COLLECTION_NAME.save(document) 
  • save() Update the data if the_ id primary key exists, and insert the data if it does not exist. This method is obsolete in the new version and can be used db.collection.insertOne() Or db.collection.replaceOne() To replace it.

  • insert() Will be thrown if the inserted data primary key already exists org.springframework.dao.DuplicateKeyException Exception, prompting that the primary key is repeated and the current data is not saved.

Added after version 3.2 db.collection.insertOne() And db.collection.insertMany() .

db.collection.insertOne() Used to insert a new document into the collection in the following syntax format:

db.collection.insertOne( <document>, { writeConcern: <document> } ) 

db.collection.insertMany() Used to insert multiple documents into the collection in the following syntax format:

db.collection.insertMany( [ <document 1> , <document 2>, ... ], { writeConcern: <document>, ordered: <boolean> } ) 

Parameter description:

  • Document: the document to write to.

  • WriteConcern: write policy, default is 1, that is, confirm write operation is required, 0 is not required.

  • Ordered: specifies whether to write sequentially, default true To write sequentially

Example

The following documents can be stored in the col collection of MongoDB’s runoob database:

>db.col.insert({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) 

In the above example col Is the name of our collection, and if the collection is not in the database, MongoDB automatically creates the collection and inserts the document.

View the inserted document:

> db.col.find() { "_id" : ObjectId("56064886ade2f21f36b03134"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } > 

We can also define the data as a variable, as follows:

> document=({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }); 

After execution, the results are displayed as follows:

{ "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } 

Perform the insert operation:

> db.col.insert(document) WriteResult({ "nInserted" : 1 }) > 

You can also insert a document using the db.col.save(document) Orders. If you do not specify the_ id field save() The method is similar to insert() Method. If you specify the_ id field, the data for that_ id is updated.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。