MongoDb$组,计算错误的字段
我编写的这个查询几乎可以完美地工作,但是没有对正确的属性或字段进行计数 以下是查询:MongoDb$组,计算错误的字段,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我编写的这个查询几乎可以完美地工作,但是没有对正确的属性或字段进行计数 以下是查询: db.getCollection("applications").aggregate([ {$match: { "history.1": {"$exists": true} }}, {$project: { _id: "$_id", dateDeb: { // beginning date $arrayElemAt:
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}}
])
实际上,计数反映了数组历史记录至少为2的文档数。但我希望计数在最后一个字段:nbrJour
(天数计数)
查看实际结果。我哪里出错了
导出接口i应用程序{ 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[],
}
}
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
}"
接口应用程序文档扩展了iApplicationMongoose.Document{
}导出接口i应用程序{
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
}"
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[],
}
}
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
}"
接口应用程序文档扩展了iApplicationMongoose.Document{
}“导出类应用程序DAO扩展DAOImpl{
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
}"
构造函数(){
const auditUser={
id:String,
名称: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
}"
“导出类应用程序DAO扩展DAOImpl{
构造函数(){
const auditUser={
id:String,
名称: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
}"
你能从你的收藏中添加一些具有预期json输出的示例文档吗?输出已经存在,我想要的结果是历史处于特殊状态的时间。想知道持续了多少时间-1天,1天…n天。因此我的group by.会将模型发布为weel。你能从你的收藏中添加一些示例文档吗对于预期的json输出?输出已经存在,我想要的结果是历史处于特殊状态的时间。想知道持续了多少时间-1天,1天…n天。因此我的group by.将把模型发布为weel。