mongodbHow to insert new document into MongoDB?
To insert a new document into MongoDB, use the insertOne()
method. This method takes two parameters: a document object and an optional options object.
Example
db.collection.insertOne({
name: "John Doe",
age: 25
});
Output example
{
"acknowledged" : true,
"insertedId" : ObjectId("5f3f9f9f9f9f9f9f9f9f9f9f")
}
Code explanation
insertOne()
: The method used to insert a new document into MongoDB.{name: "John Doe", age: 25}
: The document object containing the data to be inserted.options
: An optional parameter that can be used to specify additional options for the operation.
Helpful links
More of Mongodb
- How to use watch in MongoDB?
- How to check the version of MongoDB?
- How to rename a field in MongoDB?
- How to update an array element in MongoDB?
- What is MongoDB default port?
- How to create a many to many relation in MongoDB?
- How to use MongoDB queue?
- How to join two collections in MongoDB?
- How to drop a collection in MongoDB?
- How to specify a password for MongoDB Docker?
See more codes...