mongoDB improve, update no repeat paging query +

Updated mongoDB no repeat subdocument + + paging query

Many of my friends have a variety of questions about the use of mongoDB process. For example: how to improve the efficiency mongoDB update? How to achieve a subdocument (storage arrays, etc.) in mongoDB pagination inquiry

This article will be a consolidation point for these scattered features, and made a demo to achieve for your reference.

One, first understand the update under mongoDB, mongoDB update methods are two categories: save () and update ()

Description official update method: https://www.runoob.com/mongodb/mongodb-update.html

The need here is the update method update () class:

MongoDB advantage of that inquiry quickly, all data is stored in the form of a single document, many of which are tens of millions or even billions of records on the amount of data, some complex document will contain an array of sub-document content, (usually the same configuration as a plurality of data). When a business needs to do additional recording of the sub-document in the document, if carried out alone save () or update () operation will lead to the full text when updates. :

1. Preliminary Methods: category we choose to use update () update method

Insert the initial data structure:

{
"_id" : "1.ch",
    "no" : "1"
    "company" "ch"
    "name" : "小明",
    "comments" : [ 
	   {
            "num1" : "123"
          }
    ]
}

save (): is to re-create a new document to replace the existing document ducument documents,

update (): In the original document is directly update the field contents such as (to improve efficiency, but there are certain problems, a detailed analysis below):

插入:"comments" : [ { "num1" : "124"}]。

After inserting hope to get results:

{
"_id" : "1.ch",
    "no" : "1"
    "company" "ch"
    "name" : "小明",
    "comments" : [ 
	   {
            "num1" : "123"
          },
	  {
            "num1" : "124"
          }

    ]
}

Use update (), the original field "comments": [{ "num1": "123"}], after updating was: "comments": [{ "num1": "124"}]

actual results:

{
"_id" : "1.ch",
    "no" : "1"
    "company" "ch"
    "name" : "小明",
    "comments" : [ 
	   {
            "num1" : "124"
          }
    ]
}

This text is to first clear that we can have two records updated after the comments field: "comments": [{ "num1": "123"}, { "num1": "124"}]

So simple to use update () method can not fully applicable, then the next step of the update method optimization.

2. $ Push can be achieved using additional document recorded in the sub-array functions:

db.getCollection.update({_id:"1.ch"},{$push:{comments:[{"num1":"124"}]}})

After the first operation: "comments": [{ "num1": "123"}, { "num1": "124"}] results:

{
"_id" : "1.ch",
    "no" : "1"
    "company" "ch"
    "name" : "小明",
    "comments" : [ 
	   {
            "num1" : "123"
          },
	  {
            "num1" : "124"
          }

    ]
}

However, when repeating this operation, the recording will be repeated insertion same recording (no limit)

"comments" : [ { "num1" : "123"},{ "num1" : "124"},{ "num1" : "124"},{ "num1" : "124"},{ "num1" : "124"}] :

{
"_id" : "1.ch",
    "no" : "1"
    "company" "ch"
    "name" : "小明",
    "comments" : [ 
	      {
            "num1" : "123"
          },
	      {
            "num1" : "124"
          },
          {
            "num1" : "124"
          },
          {
            "num1" : "124"
          },
          {
            "num1" : "124"
          }
    ]
}

This can not be achieved is still required in the present embodiment repeatedly updated without updating request (update data batches in order to avoid missing data have data redundancy, but do not need to repeat the same recording insert or update operations). So we also need to select further update () method

3. $ addToSet: in the presence of an array of records not been updated, on the contrary updated insert:

db.getCollection.update({_id:"1.ch"},{$addToSet:[{"num1":"124"}]}})

After the first operation: "comments": [{ "num1": "123"}, { "num1": "124"}],

Result of repeated operation remains: "comments": [{ "num1": "123"}, { "num1": "124"}]

Therefore, we selected as the update of the update statement data ({_ id: "1.ch"}, {$ addToSet: [{ "num1": "124"}]}}).

    Now the data has been the best way to update, the next step is how the array when the document implement paging query, and the query can guarantee mongoDB still efficient.

Second, the index selection

Here we must first consider a problem. More than one million level data is what kind of structure.

E.g:

The first case: a total of ten million flow records are a water accounts, each account has a one million water level. At this point need to select the index to consider using more joint index.

The second case: the water also has ten million records, but there are accounts of hundreds of thousands or even millions, while the water under each account up to more than 10,000. Then the time selected index still use a unique index (_id) can be, because such data query response time can still provide millisecond-level response time.

Three: paging query

After you select a good index we began paging query an array of sub-documents: 

Insert a piece of data for testing:

{
"_id" : "1.ch",
    "no" : "1"
    "company" "ch"
    "name" : "小明",
    "comments" : [ 
	   {
            "num1" : "123",
            "num2" : "00R33",
            "date" : "2020-01-16 : 14:32:00",
            "flag" : "2"
	    },
	    {
	    "num1" : "124",
            "num2" : "00R34",
            "date" : "2020-01-16 : 14:32:00",
            "flag" : "2"
	    },
	     {
	    "num1" : "125",
            "num2" : "00R35",
            "date" : "2020-01-16 : 14:32:00",
            "flag" : "1"
	    },
	     {
	    "num1" : "126",
            "num2" : "00R36",
            "date" : "2020-01-16 : 14:32:00",
            "flag" : "1"
	    }
    ]
}

Use paging query:

db.getCollection.find({_id:"1.ch"},{"comments":{ "$slice":[0,2]}})

search result:

{
"_id" : "1.ch",
    "no" : "1"
    "company" "ch"
    "name" : "小明",
    "comments" : [ 
	   {
            "num1" : "123",
            "num2" : "00R33",
            "date" : "2020-01-16 : 14:32:00",
            "flag" : "2"
	    },
	    {
	    "num1" : "124",
            "num2" : "00R34",
            "date" : "2020-01-16 : 14:32:00",
            "flag" : "2"
	    }
    ]
}

comments: needs to be done sub-paged document, { "$ slice": [0,2]} do paging query, 0 is the starting position, 2 is the number of queries

 

These are his real worry was, we welcome comments discuss the use of mongoDB and improved

Released three original articles · won praise 1 · views 2591

Guess you like

Origin blog.csdn.net/flyinghe1986/article/details/103953445