使用 MongoDB 中的聚合函数进行除法和乘法

Using Aggregate function in MongoDB to Divide and Mutliply

[{
  "_id": {
    "$oid": "5fb92c5d256cb4fd463f0944"
  },
  "ppCode": "A0007",
  "ppName": "ANGULLIA PARK OFF STREET",
  "parkingSystem": "Electronic Parking System",
  "vehCat": "Car",
  "availableLot": 210,
  "parkCapacity": 268,
  "startTime": 700,
  "endTime": 1100,
  "rates": [
    {
      "weekdayMin": 30,
      "weekdayRate": 1.3,
      "satdayMin": 30,
      "satdayRate": 1.3,
      "sunPHMin": 30,
      "sunPHRate": 0.7
    }
  ],
  "modified": "2333"
}, {
  "_id": {
    "$oid": "5fb92c65256cb4fd463f0ac3"
  },
  "ppCode": "Y0019",
  "ppName": "YAN KIT ROAD / CANTONMENT ROAD OFF ST",
  "parkingSystem": "Electronic Parking System",
  "vehCat": "Motorcycle",
  "availableLot": 0,
  "parkCapacity": 8,
  "startTime": 2230,
  "endTime": 700,
  "rates": [
    {
      "weekdayMin": 510,
      "weekdayRate": 0.65,
      "satdayMin": 510,
      "satdayRate": 0.65,
      "sunPHMin": 510,
      "sunPHRate": 0.65
    }
  ],
  "modified": "2333"
}]

以上是我的 MongoDB 文件。我正在尝试使用聚合复制我在 python 中所做的事情。以下是我尝试过的。但是由于它在数组对象中,当我使用 rates.0.weekdayRate 时乘法不起作用,当我使用下面的查询时,我得到一个空值 return。

Python代码:

result = carparkCollection.find_one({"ppName": ppname, "vehCat": mvtype})
        for r in result["rates"]:
                weekday = (parktime / r["weekdayMin"]) * r['weekdayRate']
db.carpark.aggregate(
    [
        {
            $project: 
                {
                    "_id": 1,
                    
                    "ppName": 1,
                    "weekdayMin": {
                        $multiply: [
                            { 
                                $divide: [
                                    { 
                                    $multiply: [9, 60]
                                    },
                                    "$rates[0].weekdayMin"
                                    ]
                                
                            },
                            "$rates[0].weekdayRate"]
                    }
                }
        }
    ]
)

请帮帮我!谢谢

如果您只对第一个数组元素感兴趣,请在聚合开始时使用$unwind将数组转换为对象;然后您可以使用点符号访问这些字段。

parktime = 10

results = db.carpark.aggregate([
    {'$unwind':'$rates'},
    {'$project': {
        '_id': 1,
        'ppName': 1,
        'weekdayMin': {
            '$multiply': [
                {'$divide': [
                    parktime,
                    '$rates.weekdayMin'
                ]},
                '$rates.weekdayRate'
            ]
        }
    }
}])

工作示例:

from pymongo import MongoClient

db = MongoClient()['mydatabase']

db.carpark.insert_many([{
  'ppCode': 'A0007',
  'ppName': 'ANGULLIA PARK OFF STREET',
  'parkingSystem': 'Electronic Parking System',
  'vehCat': 'Car',
  'availableLot': 210,
  'parkCapacity': 268,
  'startTime': 700,
  'endTime': 1100,
  'rates': [
    {
      'weekdayMin': 30,
      'weekdayRate': 1.3,
      'satdayMin': 30,
      'satdayRate': 1.3,
      'sunPHMin': 30,
      'sunPHRate': 0.7
    }
  ],
  'modified': '2333'
}, {
  'ppCode': 'Y0019',
  'ppName': 'YAN KIT ROAD / CANTONMENT ROAD OFF ST',
  'parkingSystem': 'Electronic Parking System',
  'vehCat': 'Motorcycle',
  'availableLot': 0,
  'parkCapacity': 8,
  'startTime': 2230,
  'endTime': 700,
  'rates': [
    {
      'weekdayMin': 510,
      'weekdayRate': 0.65,
      'satdayMin': 510,
      'satdayRate': 0.65,
      'sunPHMin': 510,
      'sunPHRate': 0.65
    }
  ],
  'modified': '2333'
}])

parktime = 10

results = db.carpark.aggregate([
    {'$unwind':'$rates'},
    {'$project': {
        '_id': 1,
        'ppName': 1,
        'weekday': {
            '$multiply': [
                {'$divide': [
                    parktime,
                    '$rates.weekdayMin'
                ]},
                '$rates.weekdayRate'
            ]
        }
    }
}])

print('Aggregate:')
for result in results:
   print(result['weekday'])

print('Python:')
results = db.carpark.find()

for result in results:
    for r in result["rates"]:
        weekday = (parktime / r["weekdayMin"]) * r['weekdayRate']
        print(weekday)

给出:

Aggregate:
0.43333333333333335
0.012745098039215686
Python:
0.43333333333333335
0.012745098039215686