普通网友 2016-05-16 11:57
浏览 272
已采纳

如何用golang编写MongoDB $ slice

How do I write $slice MongoDB code in golang? Here is my MongoDB code:

db.userdetail.aggregate([{"$project":{"user":{"$slice":["$user",2,1]}}}]).pretty()

I try to write this code in golang:

o1 := bson.M{
        "$project":bson.M{"user":bson.M{"$slice":bson.M["$user",2,1,],},},
    }

It gives me the following err: syntax error: unexpected comma, expecting ]

  • 写回答

1条回答 默认 最新

  • duanhui4160 2016-05-16 16:18
    关注

    bson.M is map[string]interface not slice. And you need []interface for "$slice".

    o1 := bson.M{"$project": bson.M{"user": bson.M{"$slice": []interface{}{"$user", 2, 1}}}}
    

    If you share some dummy data, input and output, I could help you further.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?