Disclosure of Invention
In view of the above-mentioned deficiencies of the prior art, the present invention aims to: the MongoDB updating data driving method can save memory and cpu expenses.
In order to achieve the purpose, the invention provides the following technical scheme:
a driving method of MongoDB updating data comprises the following steps:
s1: inquiring data which needs to be updated into target data in the database at this time, and caching the inquired data into a memory;
s2: checking whether each object in an outermost object array of data in a memory contains a _ id field;
if the _ id field is not included, directly entering step S3; if the data comprise the _ id field, assigning the _ id field, combining statements used for assigning the _ id field, and updating the data needing to be updated in the database according to the statements used for assigning the _ id field to ensure that the data are consistent with the data in the memory;
s3: copying data in a memory, and performing updating operation on one part of data to update the part of data into the target data;
s4: comparing the two data in the memory and combining a statement for updating the database;
s5: and updating corresponding data in the database into the target data according to the statement for updating the database.
According to a specific implementation mode, in the driving method for the MongoDB update data, statements used for updating the database are transmitted into the bulk handle as parameters to be executed, so that corresponding data in the database is updated to be the target data.
According to a specific embodiment, in the driving method for the montgodb update data of the present invention, according to the data type and the operation type of the object, a corresponding operator is selected, and the selected operator is added to the combined statement.
In an aspect of specific implementation, the present invention further provides a driving system for updating data by MongoDB, including:
the memory module is used for caching data;
the query module is used for querying data which needs to be updated into target data in the database at this time, and caching the queried data into the memory module;
a field check module, configured to determine whether each object in an outermost object array of data in the memory module includes a _ id field; if the _ id field is not included, triggering a copying module to work; if the _ id field is contained, sequentially triggering a field assignment module, a statement combination module, a database updating module and a copying module to work;
the copying module is used for copying a copy of data in the memory module according to the triggering of the field checking module and triggering the statement combination module to work;
the field assignment module is used for assigning the value to the _ id field according to the triggering of the field checking module;
the statement combination module is used for combining statements used for assigning values to the _ id fields according to the triggering of the field checking module and combining statements used for updating the database according to the triggering of the copying module;
the data updating module is used for updating a copy of copied data to enable the copy of copied data to be updated into the target data;
and the database updating module is used for updating corresponding data in the database according to the statement used for assigning the value to the _ id field and the statement used for updating the database.
In an aspect of specific implementation, the present invention further provides a readable storage medium, on which one or more programs are stored, and the one or more programs, when executed by one or more processors, implement the driving method of the MongoDB update data of the present invention.
Compared with the prior art, the invention has the beneficial effects that:
in the driving method for MongoDB update data, original data in the database are directly adopted in the query stage, so that the memory space is saved, the data in the database is packaged with a layer of object arrays in the query stage in the prior art for conveniently operating the data, and once the query results are more, a larger memory is occupied; in the driving method for MongoDB updating data, combined statements are directly updated by using the native bulk operation in the updating stage, so that the use expense of the cpu is greatly reduced.
Detailed Description
The embodiments of the present invention are described below with reference to specific embodiments, and other advantages and effects of the present invention will be easily understood by those skilled in the art from the disclosure of the present specification. The invention is capable of other and different embodiments and of being practiced or of being carried out in various ways, and its several details are capable of modification in various respects, all without departing from the spirit and scope of the present invention.
As shown in fig. 1, the method for driving the montodb update data according to the present invention includes the following steps:
s1: inquiring data which needs to be updated into target data in the database at this time, and caching the inquired data into a memory;
s2: checking whether each object in an outermost object array of data in a memory contains a _ id field;
if the _ id field is not included, directly entering step S3; if the data comprise the _ id field, assigning the _ id field, combining statements used for assigning the _ id field, and updating the data needing to be updated in the database according to the statements used for assigning the _ id field to ensure that the data are consistent with the data in the memory;
s3: copying data in a memory, and performing updating operation on one part of data to update the part of data into the target data;
s4: comparing the two data in the memory and combining a statement for updating the database;
s5: and updating corresponding data in the database into the target data according to the statement for updating the database.
Specifically, in the method for driving the MongoDB update data, statements for updating the database are used as parameters and are transmitted to the bulk handle for execution, so that corresponding data in the database is updated to the target data.
In the method for driving the MongoDB update data according to the present invention, the corresponding words are combined in the following manner: and selecting a corresponding operator according to the data type and the operation type of the object, and adding the selected operator to the combined statement.
Js is taken as an example, it is set that the original data a1, a2, A3 … in the montodb need to be updated to B1, B2, B3 … according to some rule. Then, the driving method for the montodb update data according to the present invention includes the following steps:
(1) and inquiring data A1, A2 and A3 … which need to be updated to a memory at this time, wherein the data are Object objects corresponding to the JSON format documents in the database.
(2) For each data a1, a2, A3 … in the memory that needs to be updated, the outermost object array, i.e., the first level key and value, respectively, is checked, and the value characterizes the condition of the object array. Specifically, for each object array in the value, if the _ id attribute is not included, the value needs to be assigned with an _ id attribute, the value is the ObjectId in the MongoDB, and the update statement of the _ id is set in combination, and at this time, the data in the memory becomes C1, C2, and C3 ….
(3) The statements with the _ id set in the previous step are used to update the data A1, A2 and A3 … in the database to be consistent with the data C1, C2 and C3 … in the memory, namely, the data in the database is also updated to C1, C2 and C3 … at this time.
(4) Copying data in the memory, and performing an updating operation on the data, and updating one of the data C1, C2 and C3 … into target data B1, B2 and B3 …, so that the memory has two data, namely the data C1, C2 and C3 … before updating and the final data B1, B2 and B3 … after updating.
(5) And comparing the two data in the memory, and combining the update statements of the database.
(6) And updating the database by using the updating statement of the previous step, so that the data C1, C2 and C3 … in the database are also updated to be B1, B2 and B3 ….
In practice, the set _ id attribute operation in step (2) (3) aims to: and (5) providing prompt information when the update statements are combined in the step (5), and when the values of a certain object array of the new data and the old data are compared, the objects in the original database have _ id fields, and the new objects have no _ id fields, so that push operation should be adopted. Since the Javascript has only five data types of String, Number, Boolean, Array and Object, for the value of the Array type, we can update its value using $ push and $ pull operators, and for the other four types we can update its value using $ set and $ unset operators, in the case of two new and old data, we can completely combine the corresponding update statements, so that the old data is updated as new data, and the purpose of step (5) is to obtain such an Array of update statements containing these 4 update operators, which is transmitted as a parameter to the bulk handle for execution, i.e., the update of the data in the database can be completed.
As shown in fig. 2, in an aspect of specific implementation, the present invention further provides a driving system for updating data by MongoDB, including:
the memory module is used for caching data;
the query module is used for querying data which needs to be updated into target data in the database at this time, and caching the queried data into the memory module;
a field check module, configured to determine whether each object in an outermost object array of data in the memory module includes a _ id field; if the _ id field is not included, triggering a copying module to work; if the _ id field is contained, sequentially triggering a field assignment module, a statement combination module, a database updating module and a copying module to work;
the copying module is used for copying a copy of data in the memory module according to the triggering of the field checking module and triggering the statement combination module to work;
the field assignment module is used for assigning the value to the _ id field according to the triggering of the field checking module;
the statement combination module is used for combining statements used for assigning values to the _ id fields according to the triggering of the field checking module and combining statements used for updating the database according to the triggering of the copying module;
the data updating module is used for updating a copy of copied data to enable the copy of copied data to be updated into the target data;
and the database updating module is used for updating corresponding data in the database according to the statement used for assigning the value to the _ id field and the statement used for updating the database.
In an aspect of specific implementation, the present invention further provides a readable storage medium, on which one or more programs are stored, wherein the one or more programs, when executed by one or more processors, implement the driving method of the montgodb update data of the present invention.
It should be understood that the disclosed system may be implemented in other ways. For example, the division of the modules into only one logical function may be implemented in another way, for example, multiple units or components may be combined or integrated into another system, or some features may be omitted, or not implemented. In addition, the communication connection between the modules may be an indirect coupling or communication connection through some interfaces, devices or units, and may be electrical or in other forms.
In addition, functional modules in the embodiments of the present invention may be integrated into one processing unit, or each module may exist alone physically, or two or more modules are integrated into one processing unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a removable hard disk, a magnetic or optical disk, and other various media capable of storing program codes.