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 check the version of MongoDB?
- How to use watch in MongoDB?
- How to list MongoDB users?
- How to remove a field from MongoDB?
- How to create a many to many relation in MongoDB?
- How to use MongoDB queue?
- What is MongoDB default port?
- How to update an array element in MongoDB?
- How to select specific fields in MongoDB query?
- How to check if array is empty in MongoDB?
See more codes...