【发布时间】:2021-12-06 12:36:57
【问题描述】:
说明
我有这个,我需要将 ObjectId 转换为字符串,因为在模型集合中它是一个字符串。
match = {'$match': { ...simplematch}}
uidConvertion = {'$addFields': {
"uid": {"$convert": {"input": "$_id", "to": "string"}}}
}
join = {'$lookup': {
'from': 'model',
'localField': 'uid',
'foreignField': 'exampleCollection',
'as': 'models'
}}
res = db.exampleCollection.aggregate([
match,
uidConvertion,
join
])
但是……
.env/lib/python3.8/site-packages/pymongo/database.py", line 527, in aggregate
return self.client._retryable_read(
.env/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1525, in _retryable_read
return func(session, server, sock_info, secondary_ok)
.env/lib/python3.8/site-packages/pymongo/aggregation.py", line 137, in get_cursor
result = sock_info.command(
.env/lib/python3.8/site-packages/pymongo/pool.py", line 710, in command
return command(self, dbname, spec, secondary_ok,
.env/lib/python3.8/site-packages/pymongo/network.py", line 158, in command
helpers._check_command_response(
.env/lib/python3.8/site-packages/pymongo/helpers.py", line 167, in _check_command_response
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Unrecognized expression '$convert', full error: {'ok': 0.0, 'errmsg': "Unrecognized expression '$convert'", 'code': 168, 'codeName': 'InvalidPipelineOperator'}
我已连接到 ATLAS 中的数据库并检查我使用 db.version() 查看数据库的版本,结果发现它是 4.4.10。我查看了许多类似的错误,这是因为他们的 mongo 版本低于 4,我查看了 pymongo 版本试图找到错误,但它是 3.12。
$toString 也发生在我身上
我唯一要做的就是用 objectId 保存
【问题讨论】:
-
当我尝试聚合时,它起作用了。从 mongod 服务器返回错误 168。检查日志以确保它连接到您认为的位置。