Golang在json响应中获取数组索引值

所以我对数据库(mongodb)有一些查询,它将按值字段对结果进行排序。


all := EValues{}

err := con.Find(bson.M{"name": "somename}).Sort("-value").All(&all)

这个 Json 输出看起来像:


 "values": [

    {

      "user_name": "guest7485",

      "value": 8911,

      "value_date": "2016-03-09T14:40:34.512Z"

    },

    {

      "user_name": "guest7485",

      "value": 539,

      "value_date": "2016-03-07T14:11:05.217Z"

    },

    {

      "user_name": "guest7485",

      "value": 221,

      "value_date": "2016-03-07T14:11:08.853Z"

    },

    {

      "user_name": "guest7485",

      "value": 77,

      "value_date": "2016-03-07T14:11:12.377Z"

    }

  ]

在我的 json 响应中,我需要为所有结果添加参数“位置”,它应该基本上等于 1 - 第一个结果,2 - 第二个结果等等。所以我的最终输出应该是:


 "values": [

    {

      "position": 1,

      "user_name": "guest7485",

      "value": 8911,

      "value_date": "2016-03-09T14:40:34.512Z"

    },

    {

      "position": 2,

      "user_name": "guest7485",

      "value": 539,

      "value_date": "2016-03-07T14:11:05.217Z"

    },

    {

      "position": 3,

      "user_name": "guest7485",

      "value": 221,

      "value_date": "2016-03-07T14:11:08.853Z"

    },

    {

      "position": 4,

      "user_name": "guest7485",

      "value": 77,

      "value_date": "2016-03-07T14:11:12.377Z"

    }

  ]

我想知道如何用 mgo 来解决这个问题,并且一般来说,如果有人能给我最有效的方法来解决这个问题,我会非常高兴。


更新:


Evalues 的定义如下:


type EValue struct {

    ID bson.ObjectId `json:"-" bson:"_id,omitempty"`

    Name string             `json:"-" bson:"name"`

    UserId    bson.ObjectId `json:"-" bson:"userId"`

    UserName  string        `json:"user_name" bson:"userName"`

    Value     int64         `json:"value" bson:"value"`

    AddedTime time.Time     `json:"value_date" bson:"addedTime"`

}


type EValues []EValue


MMTTMM
浏览 186回答 2
2回答

蝴蝶不菲

在 MongDB 3.2 中,这可以使用$unwind运算符来完成,您可以在其中传递带有字段path和includeArrayIndex将保存数组索引的字段的对象:pipeline = [&nbsp; &nbsp; { "$match": {"name": "somename"} },&nbsp; &nbsp; { "$unwind": { "path": "$values", "includeArrayIndex": "position" } },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "$project": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "newarray.position": "$position",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "newarray.user_name": "$values.user_name",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "newarray.value_date": "$values.value_date",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "newarray.value": "$values.value",&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "$group": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "_id": "$name",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "values": { "$push": "$newarray" }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;]db.test.aggregate(pipeline);输出> db.test.aggregate(pipeline).pretty();{&nbsp; &nbsp; &nbsp; &nbsp; "_id" : "somename",&nbsp; &nbsp; &nbsp; &nbsp; "values" : [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position" : NumberLong(0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name" : "guest8911",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date" : "2016-03-09T14:40:34.512Z",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 8911&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position" : NumberLong(1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name" : "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date" : "2016-03-07T14:11:05.217Z",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 539&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position" : NumberLong(2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name" : "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date" : "2016-03-07T14:11:08.853Z",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 221&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position" : NumberLong(3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name" : "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date" : "2016-03-07T14:11:12.377Z",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 77&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]}>如果 mgo 驱动程序不支持此功能,那么为此使用 Map-Reduce 是一种不太有效的方法。以下 mongo shell 示例演示了如何运行该操作:填充测试集合:db.test.insert({&nbsp;&nbsp; &nbsp; "name": "somename",&nbsp;&nbsp; &nbsp; "values": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name": "guest8911",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value": 8911,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date": "2016-03-09T14:40:34.512Z"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name": "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value": 539,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date": "2016-03-07T14:11:05.217Z"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name": "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value": 221,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date": "2016-03-07T14:11:08.853Z"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name": "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value": 77,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date": "2016-03-07T14:11:12.377Z"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]})运行以下 map-reduce 操作:> mr = db.runCommand({&nbsp; &nbsp; "mapreduce": "test",&nbsp; &nbsp; "map": function() {&nbsp; &nbsp; &nbsp; &nbsp; var arr = []&nbsp; &nbsp; &nbsp; &nbsp; for(var i=0; i < this.values.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var val = this.values[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val["position"] = i+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.push(val);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; emit(this._id, arr);&nbsp; &nbsp; },&nbsp; &nbsp; "reduce" : function() {},&nbsp;&nbsp; &nbsp; "out": "test_keys"})查询结果集合:> db[mr.result].find().pretty(){&nbsp; &nbsp; &nbsp; &nbsp; "_id" : ObjectId("56e18ab84b9018ec86d2a6bd"),&nbsp; &nbsp; &nbsp; &nbsp; "value" : [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name" : "guest8911",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 8911,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date" : "2016-03-09T14:40:34.512Z",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position" : 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name" : "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 539,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date" : "2016-03-07T14:11:05.217Z",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position" : 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name" : "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 221,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date" : "2016-03-07T14:11:08.853Z",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position" : 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user_name" : "guest7485",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 77,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value_date" : "2016-03-07T14:11:12.377Z",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "position" : 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]}>现在给出上面的清单,您可以使用 MapReduce 在 mgo 中组装您的查询job := mgo.MapReduce{&nbsp; &nbsp; &nbsp; Map:&nbsp; &nbsp; "function(){var arr=[];for(var i=0;i<this.values.length; i++){var val=this.values[i];val['position']=i+1;arr.push(val);};emit(this._id,arr);}",&nbsp; &nbsp; &nbsp; Reduce: "function() { }",&nbsp; }&nbsp; var result []struct { Id int "_id"; Value []EValue }&nbsp; _, err := collection.Find(nil).MapReduce(job, &result)&nbsp; if err != nil {&nbsp; &nbsp; &nbsp; panic(err)&nbsp; }&nbsp; for _, item := range result {&nbsp; &nbsp; &nbsp; fmt.Println(item.Value)&nbsp; }有关更多详细信息,请查看文档:https : //godoc.org/labix.org/v1/mgo#MapReduce:

一只萌萌小番薯

向 EValue 添加位置字段:type EValue struct {&nbsp; &nbsp; ... other fields here&nbsp; &nbsp; Position int `json:"position" bson:"-"`}循环遍历数据库结果并设置字段:for i := range all {&nbsp; &nbsp; all[i].Position = i + 1}将结果编组为 JSON。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go