English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this chapter, we will learn how to insert documents into MongoDB collections.
To insert data into a MongoDB collection, you need to use MongoDB'sinsert() or save()method.
insert()The basic syntax of the command is as follows-
> db.COLLECTION_NAME.insert(document)
> db.users.insert({ ... _id : ObjectId("507f191e810c19729de860ea"), ... title: "MongoDB Overview", ... description: "MongoDB is no sql database", ... by: "Basic Tutorial", ... url: "https://www.oldtoolbag.com", ... tags: ['mongodb', 'database', 'NoSQL'], ... likes: 100 ... }) WriteResult({ "nInserted": 1 }) >
This is the name of the collection we created in the previous chapter mycol . If the collection does not exist in the database, MongoDB will create this collection and then insert the document into it.
If the _id parameter is not specified in the document being inserted, MongoDB will assign a unique ObjectId to this document.
_id is12The hexadecimal number of bytes, which is unique for each document in the collection.12Bytes are divided as follows:
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
You can also pass an array of documents to the insert() method, as shown below:
> db.createCollection("post") > db.post.insert([ { title: "MongoDB Overview", description: "MongoDB is not a SQL database", by: "基础教程", url: "http://www.oldtoolbag.com", tags: ["mongodb", "database", "NoSQL"], likes: 100 } { title: "NoSQL Database" description: "NoSQL数据库没有表", by: "基础教程", url: "http://www.oldtoolbag.com", tags: ["mongodb", "database", "NoSQL"], likes: 20, comments: [ { user: "user1", message: "My first comment", dateCreated: new Date(2013,11,10,2,35), like: 0 } ] } }) BulkWriteResult({ "writeErrors": [], "writeConcernErrors": [], "nInserted": 2, "nUpserted": 0, "nMatched": 0, "nModified": 0, "nRemoved": 0, "upserted": [], }) >
You can also usedb.post.save(document). If you do not specify _idthen the save() methodwithinsert() methodis the same. If an _id is specified, it will replace the entire data of the document containing _id specified in the save() method.
If you only need to insert a document into a collection, you can use this method.
The basic syntax of the insertOne() command is as follows:
> db.COLLECTION_NAME.insertOne(document)
The following example creates a new collection named empDetails and inserts a document using the insertOne() method.
> db.createCollection("empDetails") { "ok": 1 }
> db.empDetails.insertOne( { First_Name: "Radhika", Last_Name: "Sharma", Date_Of_Birth: "1995-09-26", e_mail: "[email protected]", phone: "9848022338" }) { "acknowledged": true, "insertedId": ObjectId("5dd62b4070fb13eec3963bea") } >
You can use the insertMany() method to insert multiple documents. For this method, you need to pass an array of documents.
The following example uses the insertMany() method to insert three different documents into the empDetails collection.
> db.empDetails.insertMany( [] { First_Name: "Radhika", Last_Name: "Sharma", Date_Of_Birth: "1995-09-26", e_mail: "[email protected]", phone: "9000012345" } { First_Name: "Rachel", Last_Name: "Christopher", Date_Of_Birth: "1990-02-16", e_mail: "[email protected]", phone: "9000054321" } { First_Name: "Fathima", Last_Name: "Sheik", Date_Of_Birth: "1990-02-16", e_mail: "[email protected]", phone: "9000054321" } ] ) { "acknowledged": true, "insertedIds": [ ObjectId("5dd631f270fb13eec3963bed"), ObjectId("5dd631f270fb13eec3963bee"), ObjectId("5dd631f270fb13eec3963bef") ] } >