Pythonを使用してドキュメントの集約関数をクエリした後のMongoDBからの私の応答は、有効な応答を返し、それを印刷することはできますが、返すことはできません。
エラー:
TypeError: ObjectId('51948e86c25f4b1d1c0d303c') is not JSON serializable印刷:
{'result': [{'_id': ObjectId('51948e86c25f4b1d1c0d303c'), 'api_calls_with_key': 4, 'api_calls_per_day': 0.375, 'api_calls_total': 6, 'api_calls_without_key': 2}], 'ok': 1.0}しかし、私が戻ろうとすると:
TypeError: ObjectId('51948e86c25f4b1d1c0d303c') is not JSON serializableそれはRESTfull呼び出しです:
@appv1.route('/v1/analytics')
def get_api_analytics():
    # get handle to collections in MongoDB
    statistics = sldb.statistics
    objectid = ObjectId("51948e86c25f4b1d1c0d303c")
    analytics = statistics.aggregate([
    {'$match': {'owner': objectid}},
    {'$project': {'owner': "$owner",
    'api_calls_with_key': {'$cond': [{'$eq': ["$apikey", None]}, 0, 1]},
    'api_calls_without_key': {'$cond': [{'$ne': ["$apikey", None]}, 0, 1]}
    }},
    {'$group': {'_id': "$owner",
    'api_calls_with_key': {'$sum': "$api_calls_with_key"},
    'api_calls_without_key': {'$sum': "$api_calls_without_key"}
    }},
    {'$project': {'api_calls_with_key': "$api_calls_with_key",
    'api_calls_without_key': "$api_calls_without_key",
    'api_calls_total': {'$add': ["$api_calls_with_key", "$api_calls_without_key"]},
    'api_calls_per_day': {'$divide': [{'$add': ["$api_calls_with_key", "$api_calls_without_key"]}, {'$dayOfMonth': datetime.now()}]},
    }}
    ])
    print(analytics)
    return analyticsdbは適切に接続されており、コレクションもあり、有効な期待される結果が返されましたが、返そうとすると、Jsonエラーが表示されます。応答をJSONに戻す方法についてのアイデア。ありがとう