0

我是 mongodb 的新手。我正在尝试每天计数。我想计算过去 3 个月存在 I_C 或 L2_C 字段的那些元素。我已经编写了这段代码,但它显示错误 - “FieldPath 字段名称可能不包含'。'”。这样做的正确方法是什么?

这是我要查询的数据的架构。

{
    "_id" : ObjectId("5b83af839bc195a7cfbabefb"),
    "firstName" : null,
    "lastName" : null,
    "realm" : null,
    "username" : null,
    "password" : "$2a$10$WVTWLevvoJXcMs/x9o/qMOEV56owh2ppjZXJt4pSR8PIDuBeRehwW",
    "credentials" : null,
    "challenges" : null,
    "email" : "ritesh.rnagpure@gmail.com",
    "emailVerified" : null,
    "verificationToken" : null,
    "status" : null,
    "created" : null,
    "lastUpdated" : ISODate("2019-08-28T12:15:07.808Z"),
    "dateCreated" : ISODate("2018-08-27T08:00:02.595Z"),
    "source" : "web",
    "isFromAPUrl" : false,
    "personal" : {
        "phone" : "9975891779",
        "referralcode" : "",
        "iccode" : "221930",
        "isiccode" : "yes",
        "referrerEmail" : "zohaibrulz@gmail.com",
        "referrerName" : "Momin Zohaib Hasanali",
        "residency" : {
            "type" : "Indian",
            "status" : "Status_Resident_Individual",
            "nationality" : "Nationality_Indian",
            "nationalityOther" : ""
        },
        "isNameFromIT" : true,
        "name" : "RITESH RAJENDRA NAGPURE",
        "birthdate" : {
            "date" : "7",
            "month" : "4",
            "year" : "1993"
        },
        "previousPan" : "AJEPN0774B",
        "previousDOB" : "07/04/1993",
        "address" : {
            "correspondence" : {
                "line1" : "Flat No-404, G Wing, Sai Satyam Residency,",
                "line2" : "Near Don Bosco School, Kalyan West, Opposite Raunak city, Kalyan, Thane",
                "city" : "Kalyan",
                "state" : "Maharashtra",
                "pincode" : "421301",
                "proof" : ""
            },
            "permanent" : {
                "line1" : "Flat No-404, G Wing, Sai Satyam Residency,",
                "line2" : "Near Don Bosco School, Kalyan West, Opposite Raunak city, Kalyan, Thane",
                "city" : "Kalyan",
                "state" : "Maharashtra",
                "pincode" : "421301",
                "proof" : "",
                "proofOtherText" : ""
            },
            "sameAsCorrespondence" : true
        },
        "isaddressFromQR" : false,
        "isaadhar" : false,
        "iskra" : false,
        "fatherOrSpouseName" : "RAJENDRA",
        "ipvdetails" : {
            "ipvrequire" : false,
            "ipvstatus" : "done"
        },
        "aadharNumber" : "875847348082",
        "mothername" : "SAVITA",
        "gender" : "male",
        "polexposed" : "no",
        "maritalStatus" : "single",
        "incomeType" : "annual",
        "income" : "below_1L",
        "occupation" : "Private Sector",
        "tradingExperience" : "< 1 Year",
        "promocode" : "CASHBACK",
        "requireddoc" : {
            "ispanrequire" : true,
            "isuidrequire" : true,
            "ischequerequire" : true,
            "isphotorequire" : true,
            "iscoraddressrequire" : false,
            "issignaturerequire" : true,
            "isaddressrequire" : false,
            "isbankstatementrequire" : true,
            "isoptionaldoc" : false
        },
        "uploadeddoc" : {
            "ispan" : true,
            "isuid" : true,
            "isaddress" : false,
            "isphoto" : true,
            "iscoraddress" : false,
            "ischeque" : true,
            "issignature" : true,
            "isoptionaldoc" : false,
            "isbankstatement" : true,
            "issignaturebmp" : true
        },
        "docname" : {
            "uid" : "5b83af839bc195a7cfbabefb_uid.pdf",
            "pan" : "5b83af839bc195a7cfbabefb_pan.jpg",
            "photo" : "5b83af839bc195a7cfbabefb_photo.jpg",
            "address" : "",
            "coraddress" : "",
            "cheque" : "5b83af839bc195a7cfbabefb_cheque.pdf",
            "bankstatement" : "5b83af839bc195a7cfbabefb_bankstm.pdf",
            "signature" : "5b83af839bc195a7cfbabefb_sign.jpg",
            "other" : "",
            "optionaldoc" : ""
        },
        "doctype" : {
            "pan" : "pan",
            "uid" : "uid",
            "add" : "uid",
            "cheque" : "bankstatement",
            "bankstm" : "bankstatement",
            "photo" : "photo"
        },
        "isaddressmodified" : true,
        "isAllEsigned" : true,
        "isEsignMerged" : true,
        "iskycFile" : true,
        "isKycEsigned" : true,
        "isApplicationCompleted" : true,
        "applicationStatus" : "completed",
        "isPanVerified" : true,
        "panStatus" : "E",
        "panName" : "RITESH RAJENDRA NAGPURE",
        "rejectionReasonList" : [],
        "makerAssigned" : false,
        "checkerAssigned" : true,
        "checkerOfficer" : "robot",
        "checkerTime" : ISODate("2019-05-29T11:30:33.004Z")
    },
    "txn" : {
        "L1_L2" : ISODate("2018-08-27T11:06:35.251Z"),
        "L1_L2_officer" : "prajakta.rane",
        "L2_R" : ISODate("2018-08-27T10:43:19.063Z"),
        "L2_R_officer" : "avilon.pereira",
        "R_L1" : ISODate("2018-08-27T10:48:51.377Z"),
        "L2_I" : ISODate("2018-08-27T11:43:12.718Z"),
        "L2_I_officer" : "rajkumar.kesari",
        "I_C" : ISODate("2018-08-27T12:58:52.343Z"),
        "I_C_officer" : "suraj.pandey"
    },
    "sales" : {
        "openStateOfficer" : "bhaskar.geera",
        "aadhaar" : false,
        "assignStep" : "registration",
        "assignTo" : "bhaskar.geera",
        "callTime" : "2018-08-27T11:35:15.446Z",
        "calledMessage" : "Ringing",
        "calledStatus" : "Ringing_Not_Picked",
        "iscalled" : true,
        "callCount" : 5
    },
    "utm_params" : {},
    "landingPage" : "",
    "suiteID" : "b9245931-a66f-3647-11b8-5b83af881208",
    "_scopeMeta" : {},
    "panPageVisited" : true,
    "kra" : {
        "pan" : "AJEPN0774B",
        "dob" : "07/04/1993",
        "name" : "",
        "fatherOrSpouseName" : "",
        "agency" : "",
        "email" : "",
        "mobile" : "",
        "address" : {
            "correspondence" : {
                "line1" : "",
                "line2" : "",
                "city" : "",
                "state" : "",
                "pincode" : "",
                "proof" : ""
            },
            "permanent" : {
                "line1" : "",
                "line2" : "",
                "city" : "",
                "state" : "",
                "pincode" : "",
                "proof" : ""
            }
        },
        "gender" : "",
        "maritalStatus" : "",
        "appStatus" : "005",
        "appUpdateStatus" : "005",
        "authMode" : "0"
    },
    "aadhar" : {},
    "overlayOpenData" : {
        "showSegmentsOverlay" : false,
        "showBrokerageOverlay" : false,
        "showReviewOverlay" : true,
        "showVerifyOverlay" : true
    },
    "currentStep" : "review/verify",
    "namePageVisited" : true,
    "options" : {
        "BSE_EQ" : true,
        "NSE_EQ" : true,
        "BCD_FO" : false,
        "NCD_FO" : true,
        "NSE_FO" : true,
        "MCX" : true,
        "MF" : true
    },
    "pricingPlan" : "BASIC-D0",
    "bank" : {
        "primary" : {
            "ifsc" : "SBIN0017460",
            "name" : "STATE BANK OF INDIA",
            "address" : "MADHUBAN TIRTHDHAM COMPLEX,SHOP NO 4,ADHARWADI ROAD,KALYAN WEST,DISTT.THANE.MAHARASHTRA 421301",
            "branch" : "ADHARWADI ROAD BRANCH",
            "city" : "THANE",
            "state" : "MAHARASHTRA",
            "country" : "INDIA",
            "micr" : "",
            "pincode" : "421301",
            "type" : "savings",
            "account" : "31527410599"
        }
    },
    "kycSplitStart" : 63,
    "kycDocsLength" : 4,
    "eSignProperties" : {
        "signername" : "Ritesh Rajendra Nagpure"
    },
    "formSubmittedDate" : ISODate("2018-08-27T10:48:51.377Z"),
    "ucccode" : "177762",
    "crmPaymentSynced" : true,
    "passwordResetCode" : "",
    "lastPaymentDate" : ISODate("2018-10-26T09:55:24.149Z"),
    "registrationWorkflowId" : 0,
    "currentWorkflowId" : 0,
    "isPaymentFromLedger" : false,
    "lastLoginDate" : ISODate("2019-08-28T12:14:58.569Z"),
    "viewedPreSales" : true
}

我写了这个查询 -

    "collection": "user",
    "aggregate": [

        {
            "$match": {
                "$or" : [
                    {
                    "$and" : [
                            {"txn.I_C" : {"$exists": true}}, {"txn.I_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
                    },
                    {
                         "$and" : [
                            {"txn.L2_C" : {"$exists": true}}, {"txn.L2_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
                    }
                ]}
        },
        { "$addFields" : {"date" :{"$cond": { "if": {"txn.I_C" : {"$exists" : true}}, "then": "txn.I_C", "else": "txn.L2_C"}}}},
        {
            "$project" : {
                "_id" : 0,
                "datePartDay": {
                    "$concat": [
                        {
                            "$substr": [
                                {
                                    "$year": "$date"
                                },
                                0,
                                4
                            ]
                        },
                        "-",
                        {
                            "$substr": [
                                {
                                    "$month": "$date"
                                },
                                0,
                                2
                            ]
                        },
                        "-",
                        {
                            "$substr": [
                                {
                                    "$dayOfMonth": "$date"
                                },
                                0,
                                2
                            ]
                        }
                    ]
                }
            }
        },
        {
            "$group": {
                "_id": "$datePartDay",
                "count": {
                    "$sum": 1
                }
            }
        }
    ]
}
4

1 回答 1

0

请尝试这种方式:

"collection": "user",
    "aggregate": [

        {
            "$match": {
                "$or" : [
                    {
                    "$and" : [
                            {"txn.I_C" : {"$exists": true}}, {"txn.I_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
                    },
                    {
                    "$and" : [
                            {"txn.L2_C" : {"$exists": true}}, {"txn.L2_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
                    }
                ]}
        },
        { "$addFields" : {"date" :{"$cond": {"if": {"$ne" : ["$txn.I_C", undefined]}, "then": "$txn.I_C", "else": "$txn.L2_C"}}}},
        {
            "$project" : {
                "_id" : 0,
                "datePartDay": {
                    "$concat": [
                        {
                            "$substr": [
                                {
                                    "$year": "$date"
                                },
                                0,
                                4
                            ]
                        },
                        "-",
                        {
                            "$substr": [
                                {
                                    "$month": "$date"
                                },
                                0,
                                2
                            ]
                        },
                        "-",
                        {
                            "$substr": [
                                {
                                    "$dayOfMonth": "$date"
                                },
                                0,
                                2
                            ]
                        }
                    ]
                }
            }
        },
        {
            "$group": {
                "_id": "$datePartDay",
                "count": {
                    "$sum": 1
                }
            }
        }
    ]
}
于 2019-10-30T23:45:07.807 回答