$ project,$ lookup for multiple fields

时间:2017-10-10 09:32:17

标签: swift mongodb

如何从多个集合中投影有限的字段?我有三个集合,我使用$ lookup,$ project。在三个中,我只能使用$ project限制一个集合的字段。当我只需要3到4个字段时,剩下的两个作为一个整体对象。

       let bsonMatch = BSON()
            bsonMatch.append(key: jsonConstants.email, string: emailAddress)

            let match = BSON()
            match.append(key: "$match", document: bsonMatch)


        bsonFields.append(key: jsonConstants.email, int: 1)
        bsonFields.append(key: jsonConstants.compId, int: 1)
        bsonFields.append(key: jsonConstants.role, int: 1)
        bsonFields.append(key: jsonConstants.fullName, int: 1)
        bsonFields.append(key: jsonConstants.mobile, int: 1)
        bsonFields.append(key: jsonConstants.homeTele, int: 1)
        bsonFields.append(key: jsonConstants.zipCode, int: 1)
        bsonFields.append(key: jsonConstants.gender, int: 1)




        let compObject = BSON ()

            compObject.append(key: jsonConstants.compName, int: 1)
            compObject.append(key: jsonConstants.compId, int: 1)
            compObject.append(key: jsonConstants.compAddress, int: 1)



            let compProject = BSON ()

            compProject.append(key: "$project", document: compObject)

        let compLookup = BSON ()

            compLookup.append(key: "from", string: dbTableConstants.companyTable)
            compLookup.append(key: "localField", string: jsonConstants.compId)
            compLookup.append(key: "foreignField", string: jsonConstants.compId)
            compLookup.append(key: "as", string: "company")






            let lookup = BSON()
            lookup.append(key: "$lookup", document: bsonLookUp)



            let compBson = BSON ()

            compBson.append(key: "$lookup", document: compLookup)


            var bsonQuery = BSON()
            bsonQuery.append(key: "0", document: match)
            bsonQuery.append(key: "1", document: project)
            bsonQuery.append(key: "2", document: lookup)
        //  bsonQuery.append(key: "3", document: compBson)
            bsonQuery.append(key: "3", document: compLookup)





            let pipline = try? BSON(json: bsonQuery.asArrayString)

            print(bsonQuery.asArrayString)

            guard let serverResult = userCollection.aggregate(pipeline: pipline!) else{
                response.setBody(string: responseMsg(msg: msgConstants.internalServerError, success: false))
                response.status = HTTPResponseStatus.internalServerError
                response.completed()
                return

            }

在这个片段中,我只提供了两个集合,我希望这两个集合只能投影选择性字段。 第一个投影是usertable,另一个是公司表。关系:用户属于特定公司。所以我需要该特定用户必要的公司详细信息。

1 个答案:

答案 0 :(得分:0)

我终于找到了我所缺少的东西。甚至来自多个集合的字段都需要在一个$ project操作中。我们可以查找多个表,然后可以将每个表中的选择性字段附加到项目中。 所以上面的代码将是:

       let bsonMatch = BSON()
        bsonMatch.append(key: jsonConstants.email, string: emailAddress)

        let match = BSON()
        match.append(key: "$match", document: bsonMatch)


    bsonFields.append(key: jsonConstants.email, int: 1)
    bsonFields.append(key: jsonConstants.compId, int: 1)
    bsonFields.append(key: jsonConstants.role, int: 1)
    bsonFields.append(key: jsonConstants.fullName, int: 1)
    bsonFields.append(key: jsonConstants.mobile, int: 1)
    bsonFields.append(key: jsonConstants.homeTele, int: 1)
    bsonFields.append(key: jsonConstants.zipCode, int: 1)
    bsonFields.append(key: jsonConstants.gender, int: 1)




    let compObject = BSON ()

        compObject.append(key: jsonConstants.compName, int: 1)
        compObject.append(key: jsonConstants.compId, int: 1)
        compObject.append(key: jsonConstants.compAddress, int: 1)





    let compLookup = BSON ()

        compLookup.append(key: "from", string: dbTableConstants.companyTable)
        compLookup.append(key: "localField", string: jsonConstants.compId)
        compLookup.append(key: "foreignField", string: jsonConstants.compId)
        compLookup.append(key: "as", string: "company")

        bsonFields.append(key: "company", document: compObject)

  let project = BSON()
            project.append(key: "$project", document: bsonFields)

        let lookup = BSON()
        lookup.append(key: "$lookup", document: bsonLookUp)



        let compBson = BSON ()

        compBson.append(key: "$lookup", document: compLookup)


        var bsonQuery = BSON()
        bsonQuery.append(key: "0", document: match)
        bsonQuery.append(key: "1", document: compBson)
        bsonQuery.append(key: "2", document: project)








        let pipline = try? BSON(json: bsonQuery.asArrayString)

        print(bsonQuery.asArrayString)

        guard let serverResult = userCollection.aggregate(pipeline: pipline!) else{
            response.setBody(string: responseMsg(msg: msgConstants.internalServerError, success: false))
            response.status = HTTPResponseStatus.internalServerError
            response.completed()
            return

        }