1

How to Create Documents in MongoDB

 1周前 阅读数 4
以下为 快照 页面,建议前往来源网站查看,会有更好的阅读体验。
原文链接: https://www.makeuseof.com/how-to-create-documents-in-mongodb/

How to Create Documents in MongoDB

By Kadeisha Kean

Published 8 hours ago

Learning MongoDB? Make use of these essential methods.

MongoDB is a NoSQL database that stores unique data such as documents, which are grouped into collections. For example, in a MongoDB database, each customer’s data is stored as a document, and all the customer documents are stored in a collection.

In this tutorial article, you’ll learn how to create documents in MongoDB.

MongoDB Create Operations

MongoDB has two create operations—insertOne() and insertMany(). Each create operation is restricted to manipulating a single collection per execution. However, you can insert one, or many documents on each execution.

Related: How Is Data Modeling Different In MongoDB?

Therefore, both create operations have the following structure:

db.collection.createOperation()

Where db is the name of the database, and createOperation() is the appropriate operation (insertOne () or insertMany()).

Using the insertOne() Operation

The insertOne() operation inserts a single document into a collection, using the following code:

db.collection(‘customers’).insertOne({
name: “Sarah Wilson”,
age: 22
})

If there’s a problem creating the new document, the insertOne() operation returns an error. And if the collection you’re trying to add a document to doesn’t exist, MongoDB will create the collection and add the document to it.

You should notice that there’s no ID assigned to the document. This is because MongoDB automatically creates a unique ID for each document in a collection.

Using the insertMany() Operation

The insertMany() operation works in much the same way as the insertOne() operation. It creates a new collection if the one provided doesn’t exist, and it returns an error if there’s a problem creating a new document.

However, the main difference is that the insertMany() operation allows you to create multiple documents per execution.

Using the insertMany() Operation Example

db.collection(‘customers’).insertMany({
name: “Roy Williams”,
age: 21
},
{
name: “James Brown”,
age: 38
},
{
name: “Jessica Jones”,
age: 25
})

The example above creates three documents in the customer collection, and each document is separated with a comma. Related: Database Engines to Consider for Your Next Project

Explore the Other CRUD Operations

Creating new documents is only the beginning of what you can do with MongoDB. MongoDB allows you to perform the CRUD operations, so you can develop complete databases.

About The Author

604a585247210-Kadeisha-profile-picture.jpg?fit=crop&w=100&h=100

Kadeisha Kean (28 Articles Published)

Kadeisha Kean is a Full-Stack Software Developer and Technical/Technology Writer. She has the distinct ability to simplify some of the most complex technological concepts; producing material that can be easily understood by any technology novice. She is passionate about writing, developing interesting software, and traveling the world (through documentaries).

More From Kadeisha Kean

Subscribe to our newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!

Click here to subscribe

猜你喜欢

关于极客头条


聚合每日国内外有价值,有趣的链接。