How to push object to an existing array in MongoDB

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

How to push object to an existing array in MongoDB

问题

我正在尝试弄清楚如何在Go中将新对象推送到数组中。

我的数据库截图:
How to push object to an existing array in MongoDB

我需要将一个新对象推送到actors数组中(该数组的最大大小为20个项目)。

在Node.js中,我会运行{ $push: { "actors": {$sort: {_id: 1}, $each: [{"name": "test"}], $slice: -20}} }

但是在Go中,我不确定正确的语法是什么。

这是我定义的集合结构:

type General struct {
	ID           	primitive.ObjectID 	`bson:"_id"`
	general 		string 				`bson:"general,omitempty"`
	Actors []struct{
		ID			primitive.ObjectID 	`bson:"_id"`
		name 		string 				`bson:"name,omitempty"`
	}
}

**** 编辑 ****

关于生成ObjectId:

根据您的答案,我已根据您的答案更新了我的代码:

update := bson.D{{"$push", bson.D{{"actors", bson.D{{"$sort", bson.D{{"_id", 1}}}, {"$each", bson.A{bson.D{{"name", "test"}, {"_id", primitive.NewObjectId()}}}}, {"$slice", -20}}}}}}

但是当我运行代码时,我得到以下错误:undefined: primitive.NewObjectId (exit status 2)

如果我只运行fmt.Println(primitive.NewObjectID()),我可以看到打印出一个新的ObjectId...所以我正在努力弄清楚为什么它在更新查询中不起作用。

(我已经导入了"go.mongodb.org/mongo-driver/bson/primitive"

英文:

I'm trying to figure out how to push a new object to an array in Go.

Screenshot of my DB:
How to push object to an existing array in MongoDB

I need to push a new object under actors array (where maximum size is 20 items in this array).

In Node.js I would have run { $push: { "actors": {$sort: {_id: 1}, $each: [{"name": "test"}], $slice: -20}} }

But in Go I'm not sure what is the correct syntax for it.

This is how my collection struct is defined:

type General struct {
	ID           	primitive.ObjectID 	`bson:"_id"`
	general 		string 				`bson:"general,omitempty"`
	Actors []struct{
		ID			primitive.ObjectID 	`bson:"_id"`
		name 		string 				`bson:"name,omitempty"`
	}
}

**** EDIT ****

Regrading the generation of an ObjectId:

I've updated my code according to your answer:

update := bson.D{{"$push", bson.D{{"actors", bson.D{{"$sort", bson.D{{"_id", 1}}}, {"$each", bson.A{bson.D{{"name", "test"}, {"_id", primitive.NewObjectId()}}}}, {"$slice", -20}}}}}}

But when I run the code then I get the following error: undefined: primitive.NewObjectId (exit status 2)

If I just run fmt.Println(primitive.NewObjectID()) then I can see a new ObjectId is printed... so i'm trying to figure out why it is not working in the update query.

(I've imported "go.mongodb.org/mongo-driver/bson/primitive")

答案1

得分: 2

你可以使用"go.mongodb.org/mongo-driver/bson"包中的"bson primitives"来构建更新文档或管道

使用原始包中的NewObjectID函数生成一个ObjectId。

import (
	"fmt"
    "context"
	"go.mongodb.org/mongo-driver/bson/primitive"
)
//...

update := bson.D{{"$push", bson.D{{"actors", bson.D{{"$sort", bson.D{{"_id", 1}}}, {"$each", bson.A{bson.D{{"name", "test"}, {"_id", primitive.NewObjectID()}}}}, {"$slice", -20}}}}}}

然后对你的集合运行更新管道。

import (
	"fmt"
    "context"
	"go.mongodb.org/mongo-driver/bson"
)

//...
filter := bson.D{{"_id", 12333}}
result, err := collection.UpdateOne(context.Background(), filter, update)
英文:

You can build the update document or pipeline using the bson primitives in "go.mongodb.org/mongo-driver/bson" package.

Generate a ObjectId with the NewObjectID function in the primitive package.

import (
	"fmt"
    "context"
	"go.mongodb.org/mongo-driver/bson/primitive"
)
//...

update := bson.D{{"$push", bson.D{{"actors", bson.D{{"$sort", bson.D{{"_id", 1}}}, {"$each", bson.A{bson.D{{"name", "test"}, {"_id", primitive.NewObjectID()}}}}, {"$slice", -20}}}}}}

Then run the update pipeline against your collection.

import (
	"fmt"
    "context"
	"go.mongodb.org/mongo-driver/bson"
)

//...
filter := bson.D{{"_id", 12333}}
result, err := collection.UpdateOne(context.Background(), filter, update)

huangapple
  • 本文由 发表于 2023年1月17日 19:26:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75145731.html
匿名

发表评论

匿名网友

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

确定