有没有办法在没有usinf for循环的情况下取消嵌套和取消列出MongoDb查询内联?

时间:2019-07-04 22:20:27

标签: arrays r mongodb dataframe

我在MongoDb中有树集:一个用于学生,一个用于班级,另一个用于老师。

数据:

学生:

{"_id":{"$oid":"5be1f434ad68a80947702255"},"Name":"Harrison Good Hair","Age":"34","Class":[{"ID":{"$oid":"5bdb3bf574da1c0011564435"}},{"ID":{"$oid":"5d1de7b00f4f4d0104a11d59"}}]}

{"_id":{"$oid":"5d1df4802765c806e0a8f341"},"Name":"Harrison Bad Hair","Age":"43","Class":[{"ID":{"$oid":"5bdb3bf574da1c0011564435"}}]}

老师:

{"_id":{"$oid":"5bd89c66969f3600136edf92"},"Name":"Hellen I"}
{"_id":{"$oid":"5bd9f84e7cf5ec00110c66d4"},"Name":"Hellen II"}
{"_id":{"$oid":"5d1de6af0f4f4d0104a11d58"},"Name":"Hellen III"}

课程:

{"_id"{"$oid":"5bdb3bf574da1c0011564435"},"Status":"Active","Teacher": 
[{"$oid":"5bd89c66969f3600136edf92"}, 
{"$oid":"5bd9f84e7cf5ec00110c66d4"}],"Day":["Tue","Thu"]}
{"_id": 
{"$oid":"5d1de7b00f4f4d0104a11d59"},"Status":"Active","Teacher": 
[{"$oid":"5d1de6af0f4f4d0104a11d58"}],"Day":["Tue"]}

我需要一个管道来以正确的顺序取消嵌套和取消列出此dataFrames unsing for循环以获得预期的回报:

"ID Class"                 "Student"            "Teacher"         "Day"
"5bdb3bf574da1c0011564435" "Harrison Good Hair" "Hellen I/Hellen II"    "Tue/Thu"
"5d1de7b00f4f4d0104a11d59" "Harrison Good Hair" "Hellen III"        "Tue"
"5bdb3bf574da1c0011564435" "Harrison Bad Hair"  "Hellen I/Hellen II"    "Tue/Thu"

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我明白了,下面做了这样的事情:

Students <- mongo(db = "test", collection = "collectionStudents")

Table <- Students$aggregate(sprintf('[
                                        {"$unwind": "$Class"},

                                        {"$lookup": {
                                        "from": "collectionClasses",
                                        "localField": "Class.ID",
                                        "foreignField": "_id",
                                        "as": "Classes"
                                        }},

                                        {"$unwind": "$Classes"},

                                        {"$lookup": {
                                        "from": "collectionTeachers",
                                        "localField": "Classes.Teacher",
                                        "foreignField": "_id",
                                        "as": "Teacher"
                                        }},

                                        {"$project": {
                                        "_id":0,
                                        "Name":1,
                                        "Class.ID":1,
                                        "Classes.Day":{
                                                "$reduce": {
                                                "input": "$Classes.Day",
                                                "initialValue": "",
                                                "in": {
                                                    "$concat": ["$$value", "/", "$$this"]
                                                }
                                            }
                                        },   
                                        "Teacher.Name":1
                                        }}

                                    ]'))


TableFinal <- cbind(Table$Class$ID,Table$Name,
                 (gsub("\"","",
                       gsub("Name = ","",
                            gsub("listName = ","",
                                 gsub(")\"","",
                                      gsub("listName = c","",
                                           gsub("\", \"","/",
                                                gsub("list","",str_replace_all(paste(Table$Teacher,sep = "/"),"[(.)]",""),ignore.case=T),ignore.case=T),ignore.case=T),ignore.case=T),ignore.case=T),ignore.case=T),ignore.case=T)),
                                 substr(Table$Classes$Day,2,100))

TableFinal[substr(TableFinal[,c(3)],1,1)=="c",3]<-substr(TableFinal[substr(TableFinal[,c(3)],1,1)=="c",3],2,500)

TableFinal <- data.frame("ID" = TableFinal[,1],"Name" = TableFinal[,2],"Teacher" = TableFinal[,3],"Day" = TableFinal[,4])