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 work with time series data in MongoDB?
- How to empty an array in MongoDB?
- How to use watch in MongoDB?
- How to check the version of MongoDB?
- How to use triggers in MongoDB?
- How to use unwind in MongoDB?
- What is MongoDB default port?
- How to filter by date in MongoDB?
- How to update an array element in MongoDB?
- How to do text search in MongoDB?
See more codes...