[英]Problem to create a new key-value with conidition using $cond in "pymongo (Python 3.7)" and "mongoDB"
I have a collection with the following document format:我有一个具有以下文档格式的集合:
{
{ "_id": 1234,
"processes": [
"0": { "0_0": aaaa , "0_1": bbbb },
"1": { "1_0": cccc, "1_1": dddd },
"2": { "2_0": eeee, "2_1": ffff},
]},
{ "_id": 5678,
"processes": [
"0": { "0_0": gggg, "0_1": hhhh},
"1": { "1_0": iiii, "1_1": jjjj},
"2": { "2_0": kkkk, "2_1": mmmm},
]}
}
In another query I made about the same DB, my colleague @hhharsha36 helped me with the problem I had:在我对同一个数据库进行的另一个查询中,我的同事 @hhharsha36 帮助我解决了我遇到的问题:
Update with Pymongo boolean field in a subdocument within a list field of a document in a MongoDB collection 使用 MongoDB 集合中文档的列表字段内的子文档中的 Pymongo boolean 字段进行更新
Thanks to that my current aggregate query looks like this:多亏了我当前的聚合查询如下所示:
cursor_processes = collection.aggregate([
{
"$unwind": "$processes"
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
{
"_id": "$_id"
},
"$processes"
]
}
}
}
])
At this moment, I want to create a key-value that depends on whether or not a key named motive exists previously.此时,我想创建一个键值对,该键值取决于先前是否存在名为 motive 的键。 Then:然后:
cursor_processes = collection.aggregate([
{
"$unwind": "$processes"
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
{
"_id": "$_id"
},
"$processes",
{
"code": {
'$cond': [
{"processes.0": {'$exists': True}},
{'$concat': ["$processes.0_0", "$processes.0_1", {'$substr': ["$_id", 0, -1]}, "si_00"]}
{'$concat': ["$processes.1_0", "$processes.2_0", {'$substr': ["$_id", 0, -1]}, "no_00"]}
]
}
}
]
}
}
}
])
list_proc = [i for i in cursor_processes] #Create a list
When I debug, I get the following error message:调试时,我收到以下错误消息:
pymongo.errors.OperationFailure: Unrecognized expression '$exists', full error: {'ok': 0.0, 'errmsg': "Unrecognized expression '$exists'", 'code': 168, 'codeName': 'InvalidPipelineOperator'} pymongo.errors.OperationFailure:无法识别的表达式'$exists',完全错误:{'ok':0.0,'errmsg':“无法识别的表达式'$exists'”,'code':168,'codeName':'InvalidPipelineOperator'}
Please help me.请帮我。 Thanks.谢谢。
$exists is a query operator. $exists是一个查询运算符。
$cond is expecting that first argument to be a boolean expression, not a filter document. $cond期望第一个参数是 boolean 表达式,而不是过滤器文档。
You might try the $ifNull pipeline operator, or perhaps a combination of $eq and $not pipeline operators您可以尝试$ifNull管道运算符,或者$eq和$not管道运算符的组合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.