Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Merge mongodb subdocuments

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Source: https://groups.google.com/forum/#!topic/mongodb-user/BpgEaRqrKsA

[abstract]

Mongodb's BSON storage format is flexible and diverse, which is helpful for getting started with MongoDB. With the support of the aggregator SPL language, Mongodb can realize the convenient query like the data SQL.

Copy summary

The storage format of MongoDB documents is BSON, a binary format similar to JSON. If you are familiar with the JSON format, it will be very helpful to get started with MongoDB, but like JSON, BSON has a flexible structure and a variety of organizational forms. while providing a strong ability to express data, it has become a very difficult task to achieve a convenient query like data SQL.

To solve this problem, the aggregator SPL language has built-in rich interfaces, which can greatly facilitate users to use Mongodb. Here is an example of merging embedded subdocument structures.

Some of the data for Collection C1 are as follows:

{

"_ id": ObjectId ("55014006e4b0333c9531043e")

"acls": {

"append": {

"users": [ObjectId ("54f5bfb0336a15084785c393")]

"groups": []

}

"edit": {

"groups": []

"users": [

ObjectId ("54f5bfb0336a15084785c392")

]

}

"fullControl": {

"users": []

"groups": []

}

"read": {

"users": [ObjectId ("54f5bfb0336a15084785c392")

ObjectId ("54f5bfb0336a15084785c398")]

"groups": []

}

}

Name: "ABC"

}

{

"_ id": ObjectId ("55014006e4b0333c9531043f")

"acls": {

"append": {

"users": [ObjectId ("54f5bfb0336a15084785c365")]

"groups": []

}

"edit": {

"groups": []

"users": [

ObjectId ("54f5bfb0336a15084785c392")

]

}

"fullControl": {

"users": []

"groups": []

}

"read": {

"users": [ObjectId ("54f5bfb0336a15084785c392")

ObjectId ("54f5bfb0336a15084785c370")]

"groups": []

}

}

Name: "ABC"

}

It is required to be grouped by name. Each set of data is an users field in a subdocument corresponding to the same name, and the data cannot be repeated. The final calculation is similar to the following:

{

Result: [

{

_ id: "ABC"

ReadUsers: [

ObjectId ("54f5bfb0336a15084785c393")

ObjectId ("54f5bfb0336a15084785c392")

ObjectId ("54f5bfb0336a15084785c398")

ObjectId ("54f5bfb0336a15084785c365")

ObjectId ("54f5bfb0336a15084785c370")

]

}

]

}

The code to use the aggregator SPL is as follows:

AB1=mongo_open ("mongodb://localhost:27017/local?user=test&password=test") 2=mongo_shell (A1, "c1.find (, {_ id:0}; {name:1})") 3for A2bot nameplate A3. (acls.read.users | acls.append.users | acls.edit.users | acls.fullControl.users) 4

= B3.new (A3.nameVuzhuidjia B3.union () .id (): readUsers) 5

= @ | B4.group@1 (~. _ id,~.readUsers) 6=mongo_close (A1)

A1: connect MongoDB, the connection word format is mongo://ip:port/db?arg=value& …

A2: use the find function to fetch numbers from MongoDB and sort them to form a cursor: collectoin is C1, the filter condition is empty, take out all the fields except _ id, and sort by name.

A3: loop the reading from the cursor, fetching a set of documents with the same name field each time. The scope of the A3 loop is indented B3 to B5, within which A3 can be used to refer to the loop variable.

B3: take out all the users fields of this group of documents, as follows:

B4: merge the users of each document in this group.

B5: B4 is continuously appended to B5 after removing duplicate records, where group@1 implements de-reprocessing. B5 is as follows:

B5 is the calculation goal of this case. If there are too many calculation results to be stored in B5, you can use the function export@j to convert B4 into a json string and constantly append it to the text file.

A6: close mongodb.

MongoDB's rich and flexible storage structure is impressive because of its lightweight and efficient storage structure, and the aggregator can naturally integrate with it to improve the use efficiency and expand the application space.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report