The insertOne method in MongoDB is used to insert a single document into a collection. When you call this method, you provide a document in JSON format, and MongoDB will add it to the specified collection. If the collection does not exist, it will be created automatically.
Here’s an example of how to use insertOne:
db.books.insertOne({
title: "MongoDB Basics",
author: "Jane Smith",
year: 2023
})
In this example, a document representing a book is inserted into the books collection. The document contains fields for the title, author, and year. The method returns an acknowledgment of the operation and the ID of the inserted document.
