du532861657 2016-05-11 05:08
浏览 47
已采纳

如何在go中编写这些mongo代码

My mongo File is here

 db.aaa.aggregate([{
                $match : {
                    status : "Active"
                }
            }, {
                $project : {
                    _id : 1,
                    title : 1,
                    postdatetime : 1,
                    status : 1,
                    answer : {
                        $filter : {
                            input : "$answer",
                            as : "answe",
                            cond : {
                                $eq : ["$$answe.status", "Active"]
                            }
                        }
                    }
                }
            }
        ])

I try this one to write in go

o2 := bson.M{
    "$project":bson.M{
        "$answer":bson.M{
                "_id":1,"title":1,"postdatetime":1,"status":1,"answer":bson.M{
                            "$filter":bson.M{"input":"$answer","as":"ans","cond":bson.M{"$eq":bson.M["$$ans.status","Active",],},}
                        },

            },
        },
}

But it show this on err syntax error: unexpected comma, expecting ]

Please Help me Out.

After the answer i try this one
   o2: = bson.M {
    "$project": bson.M {
        "$answer": bson.M {
            "_id": 1,
            "title": 1,
            "revision": 1,
            "bgimageurl": 1,
            "author": 1,
            "postdatetime": 1,
            "status": 1,
            "qatags": 1,
            "followers": 1,
            "qaviews": 1,
            "answer": bson.M {
                "$filter": bson.M {
                    "input": "$answer",
                    "as": "ans",
                    "cond": bson.M {
                        "$eq": "[$$ans.status][,Active]",
                    },
                },
            },

        },
    },
}

This time compile successfully but after running the go file send me this err msg 2016/05/11 11:04:17 $expressions are not allowed at the top-level of $project exit status 1

  • 写回答

2条回答 默认 最新

  • dsgdhtr_43654 2016-05-11 05:39
    关注

    There are two errors in your code:

    1. Array in bson.M

    bson.M["$$ans.status","Active",] will give you error like Type bson.M is not an expression, and since you are not required key-value pair in this case, an array of string would be a preferable case.

    Try:

    []string{"$$ans.status","Active",}
    

    2. The comma

    According to https://golang.org/doc/effective_go.html:

    the lexer always inserts a semicolon after the token. This could be summarized as, “if the newline comes after a token that could end a statement, insert a semicolon”.

    If you are not appended a comma after }, lexer will insert a ; for you, which will cause your current problem.

    "$filter":bson.M{"input":"$answer","as":"ans","cond":bson.M{"$eq":bson.M{"$$ans.status","Active",},},} // should add a comma at the end of the line
    

    After modified:

    "$filter":bson.M{"input":"$answer","as":"ans","cond":bson.M{"$eq":[]string{"$$ans.status","Active",},},},
    

    Update

    For your second question, $expressions are not allowed at the top-level of $project, please note that aggregation in mongodb only accept array, you can refer to this link https://docs.mongodb.com/v3.0/reference/operator/aggregation/:

    db.collection.aggregate( [ { <stage> }, ... ] )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上