$divide(聚合)
定义
示例
请考虑包含以下文档的 conferencePlanning
集合:
db.conferencePlanning.insertMany( [ { "_id" : 1, "city" : "New York", "hours" : 80, "tasks" : 7 }, { "_id" : 2, "city" : "Singapore", "hours" : 40, "tasks" : 4 } ] )
以下聚合使用 $divide
表达式将 hours
字段除以文字 8
,从而计算工作日数:
db.planning.aggregate( [ { $project: { city: 1, workdays: { $divide: [ "$hours", 8 ] } } } ] )
操作返回以下结果:
{ "_id" : 1, "city" : "New York", "workdays" : 10 } { "_id" : 2, "city" : "Singapore", "workdays" : 5 }