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 work with time series data in MongoDB?
- How to check the version of MongoDB?
- How to use unwind in MongoDB?
- How to use triggers in MongoDB?
- How to use transactions in MongoDB?
- How to use hint in MongoDB?
- How to remove a field from MongoDB?
- How to use findone in MongoDB?
See more codes...