$ project在mongo查询中不起作用

时间:2015-01-30 04:59:42

标签: java mongodb aggregation-framework

我正在编写一个查询,根据密钥(firstName + lastName +代码)从客户集合customer (id,firstName,lastName,code,source,Address(city,zip,state))获取唯一客户记录,如果出现重复,则选择特定来源。对于该查询是这个

db.customer.aggregate([{ "$match" : { "active" : true , "dealerCode" : { "$in" : [ "111391"]} , "source" : { "$in" : [ "email","TV"]}}}, {$group:{"_id":{"firstName":"$personalInfo.firstName","lastName":"$personalInfo.lastName","code":"$vehicle.code"}, "source":{$addToSet:{"source":"$source"},{"active":"$active"}}}},{$redact:{$cond:[{$eq:[{$ifNull:["$source","other"]},"email"]},"$$PRUNE","$$DESCEND"]}}, {$project:{"source":{$map:{"input":{$cond:[{$eq:[{$size:"$source"},0]},[{"source":"email"}],"$source"]},"as":"inp","in":"$$inp.source"}}}}])

此外,我想获取一些额外的字段来导出像state,zip信息这样的字段。由于组条件我得到的字段只存在于组条件中。有没有办法让这些字段与此查询一起?

示例文件:

{
        "_class" : "Customer",
        "_id" : ObjectId("546330b0a61818ed7ad88337"),
        "account" : {
                "accountCreationTimestamp" : ISODate("2014-11-12T10:04:32.062Z"),
                "accountModifiedTimestamp" : ISODate("2014-11-12T10:04:32.062Z"),
                "accountType" : "Unknown",                    
                "currentResidual" : 2145.34,
                "estimatedEquity" : 47896.85,
                "estimatedMarketValue" : 50000,  ,
                "originalOdometer" : 0,
                "originalSalesPerson" : {
                        "firstName" : "DATA",
                        "lastName" : "TEST7",
                        "fhash" : -2073145895,
                        "lhash" : 79713765,
                        "sortHash" : NumberLong("25714102020783")
                },
                "outstandingBalance" : 2103.15,
                "payoffQuoteRequested" : false,
                "purchaseOptionFee" : 0,
                "securityDeposit" : 0,
                "serviceAgreement" : false,
                "term" : 21,
                "totalAccounts" : 1,
                "turnInFee" : 0,
                "valuations" : [
                        {
                                "vin" : "1GTHK23688F113955",
                                "source" : "BB",
                                "value" : 50000,
                                "lastUpdated" : ISODate("2014-11-12T13:06:56.410Z")
                        }
                ],
                "warrantyTerm" : 0
        },
        "active" : true,
        "coBuyer" : {
                "firstName" : "cQjBRetBuq2m1ARv5vb13w==",
                "lastName" : "cQjBRetBuq2m1ARv5vb13w==",
                "fhash" : 985088,
                "lhash" : 985088,
                "sortHash" : NumberLong("985088985088"),
                "address" : "cQjBRetBuq2m1ARv5vb13w==",
                "city" : "",
                "state" : "",
                "zip" : "",
                "homePhone" : "cQjBRetBuq2m1ARv5vb13w==",
                "workPhone" : "cQjBRetBuq2m1ARv5vb13w==",
                "cellPhone" : "cQjBRetBuq2m1ARv5vb13w==",
                "email" : "cQjBRetBuq2m1ARv5vb13w=="
        },
        "creationTimestamp" : ISODate("2014-11-12T10:04:32.062Z"),
        "dealerCode" : "111391",
        "finRecordId" : "13",
        "maturity" : {
                "programEligibility" : false,
                "contacted" : false,
                "inspected" : false,
                "communicated" : false,
                "surveyed" : false,
                "customerContacted" : false,
                "closed" : false
        },
        "modifiedTimestamp" : ISODate("2014-11-12T15:10:43.553Z"),
        "personalInfo" : {
                "firstName" : "sGI6YaJ36WRfI4xuJQzI7A==",
                "lastName" : "99eQ7i+uTOqO8X+IPW+NOA==",
                "fhash" : 2575080,
                "lhash" : 2544060,
                "sortHash" : NumberLong("25440602575080"),
                "address" : "BAR+Hh6CWTWcgVo6rLtVaw==",
                "city" : "MENOKEN",
                "state" : "ND",
                "zip" : "58558",
                "homePhone" : "1cIO6YavoApacCMsw7kKMw==",
                "workPhone" : "cQjBRetBuq2m1ARv5vb13w==",
                "cellPhone" : "cQjBRetBuq2m1ARv5vb13w==",
                "email" : "cQjBRetBuq2m1ARv5vb13w=="
        },
        "source" : "WF",
        "valuationRequired" : true,
        "vehicle" : {
                "code" : "1GTHK23688F113955",
                "year" : "2005",
                "make" : "CHEVROLET TRUCK",
                "model" : "EQUINOX-V6",
                "Desc" : "2005 CHEVROLET TRUCK EQUINOX-V6",
                "options" : [ ],
                "invalidVin" : true
        }
}

我知道这很重:)。 personalInfo.firstName,personalInfo.lastName,vehichle.code。我需要从PersonalInfo,Vehicle,Account获取详细信息(不是所有字段,而是某些特定字段)

0 个答案:

没有答案