的第二个参数
find
是一个投影,因此它必须在顶级具有字段名。你把
$expr
但你可能想要
ID
。因此,要用最少的更改修复现有查询,请执行以下操作:
list(mycol.find({},{
"_id": 0,
"ID": {
"$cond": {
"if": { "$lt": ["$test_results.test2", 1] },
"then": "$identifier",
"else": None
}
}
}))
JS等效于
Mongo Plaground
.
但有了这个查询,你会得到
None
对于它经过的那些ID;这可能是没有用的。请参阅操场上的第二份文件
test2: 1
所以结果是
ID: null
.
然而,你说
“给我测试2导致0的文档的标识符”
,所以这应该是你的一部分
发现
标准
list(mycol.find(
{ "test_results.test2": 0 }, # the query
# the projection
{
"_id": 0,
"ID": "$identifier"
}
))
正如您所看到的,这是一个简单得多的查询&预测
Mongo Playground
除非你有负值,否则应该像对待
0
,检查是否与相等
0
而不是“小于1”。如果您确实需要“小于1”的检查,请执行以下操作:
list(mycol.find(
{ "test_results.test2": {"$lt": 1} },
# the projection
{
"_id": 0,
"ID": "$identifier"
}
))