Mongodb merge subdocuments

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


Abstract

        Mongodb of BSON flexible storage format to help entry-learning MongoDB is. After the SPL have set solver language support, Mongodb can be achieved as easily as data SQL queries.

Copy Summary

        MongoDB is a document storage format BSON, one kind of binary form of JSON-storage format. If you are familiar JSON format, MongoDB will be very helpful in getting started, however, and JSON, as BSON flexible structure, organizational forms, providing a powerful data presentation capabilities, while easy to implement similar data SQL query that has changed It became a very easy thing to do.

        To address this issue, set the SPL language solver built a rich interface, can greatly facilitate the users to use Mongodb. The following example will use the embedded sub merged document structure are exemplified.

Collection C1 partial data 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"
}
 

      Name required by the packet, each data field is the same as the users name corresponding to the sub-documents, and the data can not be repeated. The final result is calculated like the following:

{
result : [
       {
               _id: "ABC",
               readUsers : [
                      ObjectId("54f5bfb0336a15084785c393"),
                      ObjectId("54f5bfb0336a15084785c392"),
                     ObjectId("54f5bfb0336a15084785c398"),
                      ObjectId("54f5bfb0336a15084785c365"),
                     ObjectId("54f5bfb0336a15084785c370")
              ]
               }
]
}

      使用集算器SPL的代码如下:


A B
1 =mongo_open("mongodb://localhost:27017/local?user=test&password=test")
2 =mongo_shell(A1,"c1.find(,{_id:0};{name:1})")
3 for A2;name =A3.(acls.read.users|acls.append.users|acls.edit.users|acls.fullControl.users)
4
=B3.new(A3.name:_id,B3.union().id():readUsers)
5
=@|B4.group@1(~._id,~.readUsers)
6 =mongo_close(A1)

      A1:连接MongoDB,连接字格式为mongo://ip:port/db?arg=value&…

      A2: 使用find函数从MongoDB中取数并排序,形成游标:collectoin是c1,过滤条件是空,取出_id之外的所有字段,并按name排序。

      A3: 循环从游标读数,每次取name字段相同的一组文档。A3循环的作用范围是缩进的B3到B5,在这个作用范围内可以用A3来引用循环变量。

      B3:取出本组文档的所有users字段,如下:

      a4b1aaa4c47046ddb0dbdd3854ab3dfb_a100.png

      B4:合并本组各文档的users。

      B5:将B4去除重复记录后不断地追加到B5中,其中group@1实现去重处理。B5如下:

      a91d91457660421193f5142b903016a3_a101.png

      B5就是本案例的计算目标。如果计算结果太多导致内存放不下,可以在B5中用函数export@j将B4转为json串,不断地追加到文本文件中。

      A6:关闭mongodb。

      MongoDB丰富灵活的存储结构轻量化、高效性,让人印象深刻,而集算器能与它天然融合,提高使用效率,扩展了应用空间。


Guess you like

Origin blog.51cto.com/12749034/2438114