英文:
How to push object to an existing array in MongoDB
问题
我正在尝试弄清楚如何在Go中将新对象推送到数组中。
我需要将一个新对象推送到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.
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论