3.13. MongoDB inserts a document

发布时间 :2025-10-25 12:32:58 UTC      

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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.