如何在mgo中进行多级推送数组元素的操作?

huangapple go评论89阅读模式
英文:

How to do multi-level push of array element in mgo?

问题

我想对下面的结构进行多级推送:

type Inspector_Pool struct{
    Unique_Id string `json:"unique_id" form:"unique_id" query:"unique_id"`
    Email string `json:"email" form:"email" query:"email"`
    Branch []string `json:"branch" query:"branch"`
    Date []Date `json:"date" query:"date"`
}

type Date struct {
    Event_Timestamp string `json:"event_timestamp" query:"event_timestamp"`
    Event []Event `json:"event" query:"event"`
}

type Event struct {
    Event_Name string `json:"event_name" form:"event_name" query:"event_name"`
    Event_Id string `json:"event_id" form:"event_id" query:"event_id"`
    Status string `json:"status" query:"status"`
}

所以我想要在Event数组中推送数据,我需要实现以下代码:

// 在date数组中推送一个新节点
query := bson.M{"branch": "400612", "unique_id": c.Unique_Id}
update := bson.M{"$push": bson.M{"date": bson.M{"event_timestamp": t, "event": []models.Event{
        {
            Event: models.INSPECTION,
            Event_Id: "123456789",
            Status: models.PENDING,
        },
}}}}

err = look_up.Update(query, update)
if err != nil {
    panic(err)
}

// 在嵌套的event中推送数据
pushQuery := bson.M{"date.event": bson.M{"event": []models.Event{
    {
        Event_Name: models.INSPECTION,
        Event_Id: "4984984198",
        Status: models.PENDING,
    },
}}}

err = look_up.Update(bson.M{"unique_id": "2549090", "date.event_timestamp": "08-05-2017"}, bson.M{"$push": pushQuery})
if err != nil {
    //panic(err)
    fmt.Print("error_2", err)
}

但是它没有将数据推送到event对象中,而是在date对象中创建了一个新条目。以下是快照:如何在mgo中进行多级推送数组元素的操作?

英文:

So I want to do a multi level push for the struct below :

type Inspector_Pool struct{
    Unique_Id string `json:"unique_id" form:"unique_id" query:"unique_id"`
    Email string `json:"email" form:"email" query:"email"`
    Branch []string `json:"branch" query:"branch"`
    Date []Date `json:"date" query:"date"`
}

type Date struct {
	Event_Timestamp string `json:"event_timestamp" query:"event_timestamp"`
	Event []Event `json:"event" query:"event"`
}

type Event struct {
	Event_Name string `json:"event_name" form:"event_name" query:"event_name"`
	Event_Id string `json:"event_id" form:"event_id" query:"event_id"`
	Status string `json:"status" query:"status"`
}

So what i want is to push data in Event array so what I have to achieve code below:

//push a new node in date array
query := bson.M{"branch":"400612", "unique_id":c.Unique_Id}
update := bson.M{"$push": bson.M{"date": bson.M{"event_timestamp": t, "event": []models.Event{
		{
			Event:models.INSPECTION,
			Event_Id:"123456789",
			Status:models.PENDING,
		},

}}}}

err = look_up.Update(query, update)
if err != nil {
	panic(err)
}

//push the data in the nested event 
pushQuery := bson.M{"date.event": bson.M{"event": []models.Event{
	{
		Event_Name:models.INSPECTION,
		Event_Id:"4984984198",
		Status:models.PENDING,
	},
}}}

err = look_up.Update(bson.M{"unique_id": "2549090", "date.event_timestamp":"08-05-2017"}, bson.M{"$push": pushQuery})
if err != nil {
	//panic(err)
	fmt.Print("error_2",err)
}

but it doesn'tpush it in the event object but instead create a new entry in date object heres the snapshot如何在mgo中进行多级推送数组元素的操作?

答案1

得分: 1

您可以使用$ positional 运算符进行更新。该运算符可以在不明确指定数组中元素位置的情况下,标识要更新的数组元素。

将您的第二个push语句修改如下:

pushQuery := bson.M{"date.$.event": Event{    
        Event_Name: "foobar",
        Event_Id: "4984984198",   
}}
err = collection.Update(bson.M{"unique_id": "2549090", 
                               "date.event_timestamp": "08-05-2017"}, 
                        bson.M{"$push": pushQuery})

上述代码将事件'foobar'推送到与event_timestamp为'08-05-2017'匹配的同一日期数组中,即:

{
  "_id": ObjectId("5909287934cb838fe8f89b6e"),
  "unique_id": "2549090",
  "date": [
    {
      "event_timestamp": "08-05-2017",
      "event": [
        {
          "event_name": "baz",
          "event_id": "123456789"
        },
        {
          "event_name": "foobar",
          "event_id": "4984984198"
        }
      ]
    }
  ]
}

通常情况下,数组中嵌套数组会使得后续查询或提取数据变得困难/复杂。根据您的应用程序使用情况,我还建议重新考虑您的文档数据模型

英文:

You can utilise the $ positional operator to update. Which basically identifies an element in an array to update without explicitly specifying the position of the element in the array.

Alter your second push statement as below:

pushQuery := bson.M{"date.$.event": Event{    
        Event_Name:"foobar",
        Event_Id:"4984984198",   
}}
err = collection.Update(bson.M{"unique_id":"2549090", 
                               "date.event_timestamp":"08-05-2017"}, 
                        bson.M{"$push": pushQuery})

The above will push event 'foobar' into the same date array matching event_timestamp '08-05-2017' i.e.

{"_id": ObjectId("5909287934cb838fe8f89b6e"),
  "unique_id": "2549090",
  "date": [
    {
      "event_timestamp": "08-05-2017",
      "event": [
        {
          "event_name": "baz",
          "event_id": "123456789"
        },
        {
          "event_name": "foobar",
          "event_id": "4984984198"
        }
      ]
    }
  ]}

Generally having an array of array makes querying or extracting data difficult/complex later on. Depending on your application's use case, I would also suggest to re-consider your document Data Model.

huangapple
  • 本文由 发表于 2017年5月2日 20:25:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/43737858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定