Search code examples
gomgo

How to write these mongo code in go


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


Solution

  • 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> }, ... ] )