Author: Unreal good

Source: Hang Seng LIGHT Cloud Community

An overview of the

MongoDB is a persistent document-oriented database for storing and processing data in the form of documents.

Like other database management systems, MongoDB manages and interacts with data through four basic types of data operations:

  • C: Create operation, which involves writing data to the database
  • R: Read operation that queries the database to retrieve data from it
  • U: The update operation changes the existing data in the database
  • D: Delete operation to permanently delete data from the database

The preceding four operations are called CRUD operations. This document describes the principles and commands of the four operations.

Specific operation

Connected mongo Server

First, connect to the MongoDB Server that can be used locally or remotely before operation, as shown below:

Once you have successfully connected to the MongoDB database, you can start creating new documents.

Create a document

First, I will focus on how to create data documents in MongoDB.

For example, create an Oriental Pearl attraction object, which may have relevant information in the country, city, coordinates and so on:

{" name ", "Oriental pearl", "country" : "Chinese", "city", "Shanghai", "location" : {" lat ": 121.537," LNG ": 31.258}}Copy the code

MongoDB documentation is written in BSON, a binary form of JSON, which is an easy-to-read data format. All data in a BSON or JSON document is represented as fields and value pairs in the form of field: value.

The document consists of four fields, first the name of the attraction, then the city and country. All three fields contain strings. The final field coordinate, Location, is a nested document that specifies the location coordinates of the attraction.

insertOne

Use the insertOne method to insert this document into a new collection named Spots. As the name suggests, insertOne is used to create a single document, not multiple documents at once.

From the command line, run the following command:

Db. Spots. InsertOne ({" name ":" Oriental pearl ", "country" : "Chinese", "city", "Shanghai", "location" : {" lat ": 121.537," LNG ": 31.258}}) # output {" Indispensable ": true, "insertedId" : ObjectId(" 61b5d4963d2fc20a8483DF1A ")}Copy the code

Before executing this insertOne method, ensure that the spots collection is not created.

By executing the sample insertOne() method, it not only inserts documents into the collection, but also creates the collection automatically. The output of this action will inform you that it has been successfully executed and provide the ObjectId: 61b5D4963d2fc20A8483DF1A that it automatically generated for the new document

In MongoDB, each document in the collection must have a unique _ID field as the primary key, so the _ID field is unique. If the _ID field is set when a new document is inserted, MongoDB will automatically generate an object identifier (in the form of an ObjectId object) as the value of the _ID field.

After a document is created, you can run the following command to check the number of objects in the spots collection to check whether the document is inserted:

"> db.spots. Count () #Copy the code

insertMany

If you need to create multiple documents, it becomes cumbersome to insert them one by one using the insertOne method. So MongoDB provides the insertMany method, which you can use to insert multiple documents in a single operation.

Run the following example command, which uses the insertMany method to insert new scenic spot information into a collection of spots:

Db. Spots. InsertMany ([{" name ":" the Palace Museum ", "city" : "Beijing", "country" : "Chinese", "GPS" : {" lat ": 116.403," LNG ": 39.924}}, {" name" : "Great Wall", "city" : "Beijing", "country" : "Chinese", "GPS" : {" lat ": 106.384," LNG ": 39.031}}, {" name" : "White House", "city" : "Washington" and "country", "us", "GPS" : {" lat ": 116.652," LNG ": 40.121}}, {" name" : "London eye", "city" : "London", "country" : "British", "GPS" : {" lat ": 116.348," LNG ": 34.430}}])Copy the code

Notice the square brackets ([]) around the six documents that represent the document array. Within square brackets, multiple objects can appear one after another, separated by commas. In cases where the MongoDB method requires multiple objects, you can provide a list of objects as an array like this.

MongoDB responds to multiple object identifiers, one for each newly inserted object:

# output {" indispensable ": true, "insertedIds" : [ ObjectId("61b5d7ba3d2fc20a8483df1b"), ObjectId("61b5d7ba3d2fc20a8483df1c"), ObjectId("61b5d7ba3d2fc20a8483df1d"), ObjectId("61b5d7ba3d2fc20a8483df1e") ] }Copy the code

You can check the number of objects in the Spots collection to check whether a document is inserted:

"> db.spots. Count () #Copy the code

Query the document

The create operation stores documents in the Spots collection, and you can query the database to retrieve those documents and read their data. This step first Outlines how to query all documents in a given collection and then describes how to narrow down the list of documents retrieved using filters.

find

After completing the previous step, you can use the find() method to retrieve all documents in a single operation:

"> db.spot () # output: {"_id" : ObjectId(" 61b5d4963d2fc20a8483DF1a "), "name" : "country" :" China ", "city" : spots # output: {"_id" : ObjectId(" 61b5d4963d2fc20a8483DF1a "), "name" : "Shanghai ", "location" : {"lat" : 121.537," LNG ": 31.258}} {"_id" : ObjectId(" 61b5d7BA3d2FC20A8483DF1b "), "name" : "The Palace Museum", "city", "Beijing", "country" : "Chinese", "GPS" : {" lat ": 116.403," LNG ": 39.924}} {" _id" : ObjectId(" 61B5D7BA3D2FC20A8483DF1c "), "Name" : "The Great Wall "," City ":" Beijing ", "Country" : "China "," GPS ": {"lat" : 106.384, "LNG" : 39.031}} {"_id" : ObjectId(" 61B5D7BA3D2FC20A8483DF1D "), "name" : "White House "," City ":" Washington ", "country" : "United States "," GPS ": {" LAT ": 116.652, "LNG ": 40.121}} {"_id" : ObjectId(" 61B5D7BA3D2FC20A8483DF1e "), "name" : "London eye", "city" : "London", "country" : "Britain", "GPS" : {" lat ": 116.348," LNG ": 34.43}}Copy the code

When this method is used without any parameters, it does not apply any filtering and requires MongoDB to return any objects available in the specified collection: spots.

Note that each of these objects has an ID attribute that you have not defined. As mentioned earlier, the ID field is used as the primary key for their respective documents and was created automatically when the insertMany method was run in the previous step.

To make the output of the find() method more readable, you can use its pretty method printing function, as follows:

Db.find (). Pretty () # output: {"_id" : ObjectId(" 61b5d4963d2fc20a8483DF1A "), "name" : "east Pearl ", "country" : "Chinese", "city", "Shanghai", "location" : {" lat ": 121.537," LNG ": 31.258}} {" _id" : ObjectId(" 61B5D7BA3D2FC20A8483DF1B "), "Name" : "Forbidden City "," City ":" Beijing ", "Country" : "China "," GPS ": {"lat" : 116.403, "LNG" : 39.924}}Copy the code
The following find() method returns a single object by taking a query filter document as an argument. The query filter document follows the same structure as the document inserted into the collection and consists of fields and values, but they are used to filter the query results. Shell db.find ({"_id": "shell db.find ":" shell db.find ": "shell db.find ":" shell db.find ": "shell db.find ": ObjectId(" 61b5d4963d2fc20A8483DF1A ")}). Pretty () # output: {"_id" : ObjectId(" 61b5d4963d2fc20a8483DF1A "), "name" : "Oriental pearl" and "country", "Chinese", "city", "Shanghai", "location" : {" lat ": 121.537," LNG ": 31.258}}Copy the code

Other fields in the document can also be used for effective filtering:

Db.find ({"country": "China "}). Pretty () # output: {" _id" : ObjectId(" 61b5d4963d2fc20a8483DF1a "), "name" : spots. "Oriental pearl" and "country", "Chinese", "city", "Shanghai", "location" : {" lat ": 121.537," LNG ": 31.258}} {" _id" : ObjectId(" 61B5D7BA3D2FC20A8483DF1B "), "Name" : "Forbidden City "," City ":" Beijing ", "Country" : "China "," GPS ": {"lat" : 116.403, "LNG" : 39.924}} {"_id" : ObjectId(" 61B5D7BA3D2FC20A8483DF1c "), "name" : "Great Wall "," City ":" Beijing ", "country" : "Chinese", "GPS" : {" lat ": 106.384," LNG ": 39.031}}Copy the code

The query filter documentation is very powerful and flexible and can help us query data efficiently.

conclusion

MongoDB provides a powerful query system that allows precise selection of documents of interest based on complex criteria, and MongoDB tips will be shared in more detail later.

By learning the notes in this article, we can get a quick start on the basic CRUD operation of Mongo. Of course, after learning, we also need to practice by ourselves.


Want to learn more from the tech gurus? Where are problems encountered during development discussed? How to access the massive resources of fintech?

Hang Seng LIGHT Cloud community, a professional fintech community platform built by Hang Seng Electronics, shares practical technology dry goods, resource data, and fintech industry trends, embracing all financial developers.

Scan the qr code below and join us!