mongodb - Convert string to date in pymongo
问题描述
Using Community MongoDB 4.4.3, I have a sample test collection:
client = pymongo.MongoClient('localhost', 27017)
db = client.test
collection = db.test_collection
print(collection.find_one())
>>> {'_id': ObjectId('600d8e2f4cf39c04cdb86ea0'),
'id': '0706.1314',
'update_date': '2008-12-18'}
I'm trying to convert string to date for the field update_date
with $toDate operator:
collection.aggregate([{"$toDate": "update_date"}])
But get the following error:
OperationFailure: Unrecognized pipeline stage name: '$toDate', full error: {'ok': 0.0, 'errmsg': "Unrecognized pipeline stage name: '$toDate'", 'code': 40324, 'codeName': 'Location40324'}
Why does this happen?
解决方案
您需要指定输入字段,如下所示:
collection.aggregate([ {$project:{ newField:{"$toDate": "$update_date"}}} ])
推荐阅读
- scala - Scala 2.13 对 Play Framework 的 SASS 支持
- python - 无法连接到 Visual Studio Code 上的 Jupyter 服务器(Python Anaconda)
- embedding - 句子嵌入 BERT
- python - 根据输出条件运行 github 操作步骤
- esp8266 - 如何从 Blynk HTTP RESTful API 读取模拟引脚数据?
- java - Jhipster:上下文路径更改在管理部分显示错误
- php - 使用 imagick 更好的背景遮罩
- r - R中的时间假人是什么样的?
- python - 如果列包含列表,如何有条件地替换 Pandas Dataframe 列中的值?
- php - HTML5Blank 将摘录保存到数组中