未知运算符:mgo中的“ $ or”

I'm getting unknown operator: $or error with code below and couldn't figure out why.

vc := &storers.VCategory{}

err = mndb.
    C("virtualcategoryprototypes").
    Find(bson.M{
        "children":   nil,
        "deleted_by": nil,
        "indexCycleNo": bson.M{"$or": []bson.M{
            bson.M{"$exists": false},
            bson.M{"$lt": cycle}}}}).
    One(vc)

1个回答



$ or </ code>表达式是其他条件的一系列(数组),这些条件自身有效(并且 (将使用逻辑OR连接进行评估),并且 $ or </ code>表达式的结果将与处于同一级别的其他条件进行逻辑AND连接。</ p>

因此,您必须将 $ or </ code>运算符上移一个级别,其值必须为切片或数组,而 $ or </ code>的元素在您的情况下为 indexCycleNo:{$ exists:false} </ code>和 indexCycleNo:{$ lt:cycle} </ code>:</ p>

  err = mndb。
C (“虚拟类别原型”。)。
Find(bson.M {
“ children”:nil,
“ deleted_by”:nil,
“ $ or”:[] bson.M {
{“ indexCycleNo”: bson.M {“ $ exists”:false}},
{“ indexCycleNo”:bson.M {“ $ lt”:cycle}},
},
})。
一个(vc)
</ code> </ pre>
</ div>

展开原文

原文

An $or expression is a series (array) of other conditions which are valid on their own (and which will be evaluated using logical OR connection), and the result of the $or expression will be in logical AND connection with other conditions at the same level.

So you have to move the $or operator up one level, its value must be a slice or array, and the elements of $or will be in your case indexCycleNo: {$exists: false} and indexCycleNo: {$lt: cycle}:

err = mndb.
    C("virtualcategoryprototypes").
    Find(bson.M{
        "children":   nil,
        "deleted_by": nil,
        "$or": []bson.M{
            {"indexCycleNo": bson.M{"$exists": false}},
            {"indexCycleNo": bson.M{"$lt": cycle}},
        },
    }).
    One(vc)

dounei9043
dounei9043 如此简单的错误,我在这里尝试找出3个多小时。 非常感谢你。
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐