Basic Operation of MongoDB

[Database Operation]

After installing the database, if you are in the Linux environment, you can use the following two commands to check whether the mongodb service is started and connected to mongo. But please note that if you are in Win10 and the MongoDB version installed is the latest version, you need to download the Mongoshell to enter the interactive environment. As for opening the service, you can go to the task manager ->Services ->find MongoDB (maybe MongoDB, I forgot)

Pgrep mongo - l # Check whether mongodb has been started

Mongo # Connect mongo

This version is MongoDB shell version v4.0.0

Basic database operations: (create, delete, view, insert data, whatever)

After using Testdb, show dbs cannot display the Testdb database. Because the Testdb created by using the use command is stored in memory and there is no data in the database, you can see the Testdb by showing dbs after executing the insert statement.

MongoDB includes database admin, config, local, and test by default, but the test database is stored in memory and has no data, so it is invisible when executing show dbs.

Because the bottom layer of mongodb is a javascript engine, we can use js syntax to insert data.

[Data import and export]

mongoimport

Import data (csv/json, etc.) into mongodb data. The command used is mongoimport. The specific syntax is as follows:

#Import - mongoimport

mongoimport -d Testdb1 -c score --type csv --headerline --ignoreBlanks --file test.csv

-D Testdb1: specifies to import data into the Testdb1 database;

-C score: Import data into the collection score. If the collection does not exist before, it will automatically create a new collection (if the parameter -- collection is omitted, a collection named CSV file will be automatically created);

--Type csv: file type, here is CSV;

--Headerline: This parameter is very important. After adding this parameter, the content created will take the content in the first line of the CSV file as the field name (this parameter is not required for importing json files);

--IgnoreBlanks: This parameter can ignore the blank values in the CSV file (this parameter is not required for importing json files);

--File test.csv: This is the path of the CSV file. You need to use an absolute path.

mongoexport

Data export - mongoexport

[Set Operation]

Basic operation: CRUD

#Create a fixed collection test in the Testdb database. The total collection space is 512000KB, and the maximum number of documents is 1000.

use Testdb

#View all collections of the current database

show collections

#Create Collections Explicitly

db.createCollection("test", { capped : true, autoIndexId : true, size : 512000, max : 1000 } )

#Capped: It is a boolean type. When true, a fixed set is created, and size must be specified.

#A fixed set refers to a set with a fixed size. When it reaches the maximum value, it will automatically overwrite the oldest document. The default value is false;

#AutoIndexId: It is also a Boolean type. If it is true, it will automatically_ The id field creates an index. The default value is false;

#Size: specifies a maximum value for a fixed set (in bytes KB);

#Max: Specifies the maximum number of documents contained in a fixed collection.

#Implicitly create a collection -- db. collection name. insert()

Db.mytest2.insert ([{"name": "Wang Xiaoming", "sex": "male"}, {"name": "Li Xiaohong", "sex": "female"}])

Db. set name. find() # Query set

Db. set name. drop() # Delete a set

Unlike MySQL, in MongoDB, you don't have to create a collection first. MongoDB will automatically create a collection when you insert some documents.

One 🌰:

[Document Operation]

The first step is to operate on the collection object. Next, you need to operate on the data in the collection~

Add, delete and modify

All data stored in the collection is in BSON format. BSON is a binary storage format similar to JSON, which is called Binary JSON for short.

tips:

save()

Specified_ Id, update the document; Not specified_ ID will perform the insertion function. MongoDB automatically generates a non duplicate by default_ id。

update()

Criteria - query criteria,

ObjNew - updated,

Upsert - false by default. Whether to insert objNew when there is no update document,

Multi - default false, only the first one found is updated)

Document Query

In MySQL, we often filter data to a certain extent. Just like most people like beautiful little sisters and handsome little brothers, MongoDB must have such an important operation!

Some basic operations are somewhat similar to LaTeX syntax~

[Cursor]

The cursor is not the query result, but the return resource or interface of the query. Through this interface, you can read one by one. Just like fopen opens a file and gets a resource, you can read the file line by line through the resource.

[Pipeline polymerization]

MongoDB aggregation operations include aggregation pipeline operations and Map Reduce operations.

The aggregation pipeline operation is to transfer the processing result to the next pipeline for re processing after the document is processed in one pipeline; The Map Reduce operation is to decompose the batch documents in the collection, and then merge and output the processed results.

Give an example

$project Modify Document Structure Output

Scenario: Sometimes you only need to output several columns in the document, and use $project to operate. You can also use this aggregator to rename the column names.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us