MongoDB là một cơ sở dữ liệu, một phần của ứng dụng chịu trách nhiệm lưu trữ và truy xuất thông tin.
MongoDB là một cơ sở dữ liệu NoSQL. Ở dưới cáiNoSQLô chúng tôi đặt tất cả những cơ sở dữ liệu không sử dụng ngôn ngữ SQL để truy vấn dữ liệu.
Các đặc điểm chính của MongoDB
MongoDB là một cơ sở dữ liệu rất thân thiện với JavaScript. Nó cho thấy một API JavaScript mà chúng ta có thể sử dụng để tạo cơ sở dữ liệu và bộ sưu tập các đối tượng (được gọi làcác tài liệu).
nó làkhông có vảy, có nghĩa là bạn không cần xác định trước cấu trúc cho dữ liệu trước khi lưu trữ.
Trong MongoDB, bạn có thể lưu trữ bất kỳ đối tượng nào mà không cần phải lo lắng về các trường cụ thể tạo đối tượng này và cách lưu trữ chúng. Bạn yêu cầu MongoDB lưu trữ đối tượng đó.
Dữ liệu được lưu trữ ở định dạng tương tự như JSON, nhưng được nâng cao để cho phép lưu trữ nhiều hơn chỉ là các kiểu dữ liệu cơ bản.
Cài đặt
Hãy tiếp tục và cài đặt MongoDB. Bạn có thể sử dụng một trong nhiều nhà cung cấp dịch vụ đám mây cung cấp quyền truy cập vào phiên bản MongoDB, nhưng để tiện cho việc tìm hiểu, chúng tôi sẽ tự cài đặt nó.
Tôi sử dụng máy Mac, vì vậy hướng dẫn cài đặt trong hướng dẫn này đề cập đến hệ điều hành đó.
Mở thiết bị đầu cuối và chạy:
brew tap mongodb/brew
brew install mongodb-community
Đó là nó.
Hướng dẫn không quá dài hoặc phức tạp, giả sử bạn biết cách sử dụng thiết bị đầu cuối vàcách cài đặt Homebrew.
Việc cài đặt cho chúng ta biết điều này:
To have launchd start mongodb now and restart at login:
brew services start mongodb-community
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.confYou can choose to either launch MongoDB once and have it running forever as a background service in your computer (the thing I prefer), or you can run it just when you need it, by running the latter command.
The default configuration for MongoDB is this:
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1Logs are stored in /usr/local/var/log/mongodb/mongo.log
and the database is stored in /usr/local/var/mongodb
.
By default there is no access control, anyone can read and write to the database.
The Mongo Shell
The best way to experiment with MongoDB and starting to interact with it is by running the mongo
program, which starts the MongoDB shell.

You can now enter any command that Mongo understands.
Create a database
When you start, Mongo creates a database called test
. Run db
in the shell to tell you the name of the active database

To change the database, just write use newname
and the newname
database will be instantly created and the shell switches to using that.

Use show databases
to list the available databases:

As you can see, the something
database is not listed, just because there is no collection yet in it. Let’s create one.
Collections
In MongoDB, a collection is the equivalent of a SQL database table.
You create a collection on the current database by using the db.createCollection()
command. The first argument is the database name, and you can pass an options object as a second parameter.

Once you do so, show databases
will list the new database, and show collections
will list the collection.

You can also create a new collection by using it as a property of the db
object, and calling insert()
to add an object to the collection:
db.dogs.insert({ name: 'Roger' })

Listing objects in a collection
To show the objects added to a collection, use the find()
method:

As you can see there is an additional _id
property for the record we added. That’s automatically generated for us by MongoDB.
Now, add more dogs:
db.dogs.insert({ name: 'Buck' })
db.dogs.insert({ name: 'Togo' })
db.dogs.insert({ name: 'Balto' })
Calling db.dogs.find()
will give us all the entries, while we can pass a parameter to filter and retrieve a specific entry, for example with db.dogs.find({name: 'Roger'})
:

The find()
method returns a cursor you need to iterate on.
There is another method which is handy when you know you’ll only get one record, which is findOne()
, and it’s used in the same way. If multiple records match a query, it will just return the first one.

Updating records
To update a record you can use the update()
method on a collection:

Removing records
You can remove a record calling the remove()
method on a collection, passing an object to help identify it:

To remove all the entries from a collection, pass an empty object:
db.dogs.remove({})
More database tutorials:
- The MongoDB basics tutorial
- How MongoDB is different from a SQL database
- Information systems, data and information
- Data models
- Introduction to the ER Data Model
- The Relational Model
- Relational Algebra
- Relational Databases
- What is a Database? And a DBMS?
- Introduction to SQL
- How to install PostgreSQL on macOS
- Do you always need a database for your app?
- How to install SQLite on macOS
- Introduction to PostgreSQL
- PostgreSQL User Permissions
- How to list all users in PostgreSQL
- How to switch database using PostgreSQL
- How to list all databases using PostgreSQL
- How to list tables in the current database using PostgreSQL
- How to install MySQL on macOS
- Creating a user on MySQL
- PostgreSQL vs MySQL, a comparison
- SQLite User Permissions
- MySQL User Permissions
- SQL, creating a table
- SQL, adding data to a table
- SQL, how to use SELECT
- SQL, Handling empty cells
- SQL, Unique and Primary keys
- SQL, how to update data
- SQL, how to update a table structure
- SQL, how to delete data and tables
- SQL Joins
- SQL Views
- How to insert multiple items at once in a MongoDB collection
- How to remove all items from a MongoDB collection