MongoDb $ group,指望错误的字段

时间:2017-07-11 20:33:57

标签: mongodb aggregation-framework

我编写的这个查询几乎完美无缺,但计数不是在正确的属性或字段上进行的。

以下是查询:

breed [supras supra]
breed [subs sub]

turtles-own [age]
subs-own [status]

to setup
  clear-all

  ;; Color the patches so they're easier to see
  ask patches [ set pcolor random-float 2 ]

  ;; 1/2 of num-turtles patches will sprout subs
  ask n-of (num-turtles / 2) patches [
    if not any? turtles-on patch-set self [
      sprout-subs 1
    ]
  ]

  ;; 1/2 of num-turtles patches will sprout supras
  ask n-of (num-turtles / 2) patches [
    if not any? turtles-on patch-set self [
      sprout-supras 1
    ]
  ]

  ;; Set breed colors and own-variables
  ask subs [
    set color blue
    set shape "dot"
    set age 0
    set status random 10
  ]

  ask supras [
    set color pink
    set shape "dot"
    set age 0
  ]

  reset-ticks
end

to go

  ask turtles [
    let empty-patches neighbors with [not any? turtles-here]
    if any? empty-patches[
      let target one-of empty-patches
      face target
      move-to target
    ]
  ]

  ;; Mating conditions
  ask supras [
    if any? subs-on neighbors [
      ;; Mate with highest status sub
      mate
    ]
  ]

  tick
end

to mate
  move-to max-one-of subs [status]
end

实际上,计数反映的是至少包含2的数组历史记录的文档数量。但我希望计数在最后一个字段:db.getCollection("applications").aggregate([ {$match: { "history.1": {"$exists": true} }}, {$project: { _id: "$_id", dateDeb: { // beginning date $arrayElemAt: [("$history.createdOn"), 0] }, dateFin: { // end date $arrayElemAt: [("$history.createdOn"), 1] } }}, {$project: { dateDiff: { $divide: [ {$subtract: ["$dateFin", "$dateDeb"]}, (1000 * 3600 * 24) ] } }}, {$project: { diffArrondi: {$trunc: "$dateDiff"} // rounded diff }}, {$project: { diffFinal: { $cond: [ {$lte: ["$diffArrondi", 1]}, -1, "$diffArrondi" ] } }}, {$group: { _id: null, nbrJour: {$addToSet: "$diffFinal"}, // days count count: {$sum: 1} }}, {$unwind: {path: "$nbrJour"}}, {$sort: {"nbrJour": 1}} ]) (天计数)。

查看实际结果。我哪里出错了。

The actual result

3 个答案:

答案 0 :(得分:0)

谢谢Veeram,几乎在那里,但不完全,现在我对阵列中的每个项目得到8的计数,这仍然是不对的。我猜的数量只是第一项的数量。

enter image description here

答案 1 :(得分:0)

export interface IApplication {     id:string,

status: IStatus,

organization: IOrganization,

audit: IAudit,

history: IHistory[],

watchers?: IWatchers,

sections: {
    applicant?: {
        organizationName?: string,
        name?: string,
        phone?: string,
        phoneExtension?: string,
        mobile?: string,
        address?: string,
        email?: string,
    },
    project?: {
        urgent?: boolean,
        name?: string,
        number?: string,
        location?: string,
        drawing?: string,
        description?: string,
        attachedSketchPlanNumber?: string,
        specialRequirements?: string,
        municipality?: string,
        workType?: string,
        CSEMRequired?: boolean,
        CSEMApplicationNumber?: string
    },
    work?: {
        startDate?: string,
        endDate?: string,
        obstructingTraffic?: boolean,
        obstructionDescription?: string,
        excavation?: boolean,
        surfaceArea?: string,
        paving?: string,
        pavement?: string,
        other?: string,
        signature?: string,
        signatureDate?: string,
    },
    publicRightsManager?: {
        name?: string,
        phone?: string,
        phoneExtension?: string,
        mobile?: string,
        address?: string,
        email?: string
    },
    internalConsultations?: IInternalConsultation[],
    consent?: {
        trafficPlan?: boolean,
        expiresOn?: string,
        specialRequirements?: string,
        CSEMApproved?: boolean,
        CSEMApprovedOn?: string,
        CSEMApprovedBy?: string,
        CSEMApprovalAttached?: boolean,
        estimatedCostOfDegradation?: number,
        fee?: number,
        granted?: boolean,
        revise?: boolean,
        signature?: string,
        signatureDate?: string,
        startDate?: string,
        endDate?: string,
        distanceArterial?: string,
        pavementAgeArterial?: string,
        typeLastInterventionArterial?: string,
        distanceLocal?: string,
        pavementAgeLocal?: string,
        typeLastInterventionLocal?: string
    },
    documents?: IAttachedDocument[],
    comments?: IComment[],
}

}

interface ApplicationDocument扩展了IApplication,mongoose.Document { }

答案 2 :(得分:0)

“导出类ApplicationDAO扩展DAOImpl {     constructor(){         const auditUser = {             id:String,             name:String,         }

    const audit = {
        createdBy: auditUser,
        createdOn: Date,
        lastModifiedBy: auditUser,
        lastModifiedOn: Date,
    };

    const auditableEntry = {
        id: String,
        createdBy: auditUser,
        createdOn: Date,
        disabled: Boolean,
    };

    const internalConsultation = Object.assign(auditableEntry, {
        service: String,
        documentsAttached: Boolean,
    });

    const comment = Object.assign(auditableEntry, {
        comment: String,
        included: Boolean,
    });

    const attachedDocument = Object.assign(auditableEntry, {
        name: String,
        link: String,
    });


    const history = {
        // id: String,
        createdBy: auditUser,
        createdOn: Date,
        summary: {
            content: Boolean,
            comments: Boolean,
            documents: Boolean,
            status: {
                from: String,
                to: String,
            }
        }
    };

    const watchers = {
        applicants: String,
        reviewers: String
    }"