mongodb use summary-1

In fact, I have been in contact with Mongodb for more than a year, but there is no serious summary. . .

Now, some basic common commands will be sorted out.

 

Last week, I encountered something very strange. For a project that was operated and maintained on AWS, the customer suddenly called and said that the information list on the mobile phone was very slow, or even could not be opened. Execute the top -c command on the dbServer and find that the memory has been up to 92%. . . The cpu is also working at full capacity. . . , I checked the weblog and the log of mongodb. In the log of mongodb, I found that the retrieval operation for a collection is very slow, it takes about 15 seconds to 20 seconds. . .

 

I have deleted this collection before, but the deletion happened a week or two ago. Why did it become slow that day? It was incomprehensible. . .

 

Because there are two sets of environments, one for verification, and one for commercial use, I tried to compare the indexes of these two tables and found that the verification index is one more than the commercial index. . . So the blind cat encountered the dead mouse and solved the problem, but after thinking about it, it was not because of the problem of one more index. This issue needs to be discussed in detail in the future. Let’s get some basics sorted out today.

 

--About the index

1. View the index of database documents

db.document_name.getIndexes();

2. Delete the specified index

db.document_name.dropIndex({"index_1" : 1,"index_2" :1});

3. Create a single index

db.document_name.ensureIndex({"index": 1});

4. Create a composite index

 

db.document_name.ensureIndex({"index_1" : 1,"index_2" :1},{"unique":true},{"name":"index_1_1_index_2_2"},{"background":true});

Regarding background: true, if you create an index for a document with existing data, you can execute the following command to make MongoDB create the index in the background, so that other operations will not be blocked during creation. In contrast, creating an index in a blocking manner makes the entire creation process more efficient, but MongoDB will not be able to receive other operations during creation.

 

--About search

db.document_name.find({ $query: { column_1: "*******************" }, $orderby: { _id: -1 } })

db.document_name.find({"column_1":"*******************"}).sort({_id:-1})

 

--delete document

db.document_name.drop();

 

-- Fuzzy query

1.db.document_name.remove({name:{$regex:"aaa"}});--Remove documents that contain "aaa" in the value of name

2.db.document_name.find({name:{$regex:"aaa",$options:"$i"}}); - ignore case, retrieve documents that contain "aaa" in the value of name

3.db.coll_push_info.find({"push_body.content":{$regex:"aaaa",$options:"$i"}});// Fuzzy query of nested fields

 

--sort

db.document_name.find({"name":"..........................."}).sort({test_id:1});

Note: 1 is ascending order, -1 is descending order

 

--Multiple value matching

db.document_name.find({id:{$in:["888888888888","999999999999","7777777777777"]}});

 

--Deduplication

db.document_name.distinct("test_id");

 

 

 

 

The isodate of mongodb is UTC time, China time = UTC+8

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647739&siteId=291194637