MongoDB modifies an example of data structure and small grooming

Source: Internet
Author: User
Tags mongodb client mongodb query mongo shell

The storage structure of MongoDB is flexible, but it does not mean that we are arbitrarily using irregular document structures. The irregular document structure is a disaster for both development and later maintenance. So, there is a convention format.

However, due to the lack of detailed design and other reasons, the database document structure in the development process is always difficult to avoid, should minimize this modification. However, when it has to be changed, it must be changed:

1 {2"_id": ObjectId ("54a1f775e4b03dad3af55c3c"),3"MyId": "54a0b115e4b00712935204ba",4"Name": "Action",5"Key": "M_0",6"Index": 0,7"Createtime": Isodate ("2014-12-30t00:53:09.483z"),8"Subms" : [9 {Ten"_ID":NULL, One"RM" : { A"MT": "TEXT", -"Content": "Scatter things" - }, the"Name": "The ground says", -"Key": "Menu_0_0", -' Type ': ' Click ' - } + ] - } + { A"_id": ObjectId ("54b87996e4b04b29b92a71b1"), at"MyId": "54b5e8cce4b045d4121f5d63", -"RM" : { -"Msgtype": "URL", -"url": "Http://www.abc.com" - }, -"Name": "Usercenter", in"Key": "User-center", -' type ': ' VIEW ', to"Index": 0, +"Createtime": Isodate ("2015-01-16t02:38:14.643z") - }] the  *}

There are many such types of documents. There are several documents embedded in the Subms field in this document. The thing to do is to detach several documents from the SUBMS into a separate document, and leave the newly-torn document with the ID of the original parent document.

The script is as follows:

1 varMlist =Db.m.find ();2 varMlength =mlist.length;3 varMarray =NewArray ();4 //This step is more critical because the Find method returns a cursor,5 //If you do not close first, or if you run out of this cursor, continue to6 //inserting data into MongoDB causes the cursor to become cluttered. 7 //Therefore, the cursor is exhausted before being modified in MongoDB. 8  while(Mlist.hasnext ()) {9 Marray.push (Mlist.next ());Ten } One  for(vari = 0; i < mlength; i + +) { A     varMitem =Marray.pop (); -     varSmlist =Mitem.subms; -Mitem.subms =NewArray (); the     if(smlist = = 0 | | smlist.slength = = 0) { -         Continue; -     } -     varSmlength =smlist.slength; +      for(varj = 0; J < Smlength; J + +) { -         varSmitem =Smlist.pop (); +Smitem.pid =Mitem._id.str; ASmitem.index =J; atSmitem.subms =NewArray (); -smitem._id =undefined; - Db.m.insert (smitem); -     } - Db.m.save (mitem); -}

It was a little hard to write for the first time.

First, is because JS's very unfamiliar. It took several w3school to see something.

Second, the understanding of the MongoDB query cursor.

Third, MONGO Shell, there is a good thing, when you hit a command, do not knock the following parentheses, you can display the command will be executed under the JS method.

such as the following command:

> Db.m.find will display the corresponding method after return.

In addition, there is a very useful MongoDB client: Robomongo worth recommending.

MongoDB modifies an example of data structure and small grooming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.