dongzhenqi2015 2016-05-09 07:57
浏览 333
已采纳

如何在Golang中的mgo查询中运行$ and运算符

I want to execute following query in MongoDB in Golang

check_select = bson.M{
            "$and": []interface{}{
                "shr_key": user_shr_key,
                "id": uid,
                "user_history": bson.M{"$elemMatch": bson.M{"action": "STOP", "message_id": mid}},
            },
        }

Please help... I am getting following error "index must be non-negative integer constant".

  • 写回答

1条回答 默认 最新

  • donglinli2027 2016-05-09 08:09
    关注

    The error is from the way you initialize an array in go:

    ....
    "$and": []interface{}{
        "shr_key": user_shr_key,
    ....
    

    go array doesn't accept string as an index.

    Anyway, in order to solve your problem, remove index from array initialization and wrap the key-value pair in bson.M will do, eg:

    bson.M{
                "$and": []bson.M{ // you can try this in []interface
                    bson.M{"shr_key": user_shr_key},
                    bson.M{"id": uid},
                    bson.M{"user_history": bson.M{"$elemMatch": bson.M{"action": "STOP", "message_id": mid}}},
                },
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥20 unity内置语言切换的按钮设置
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码