将字段添加到 MongoDB 内部对象
Add Fields to MongoDB Inner Object
我正在尝试将字段附加到我的 mongodb 集合中的对象。到目前为止,这就是我在 MongoDB 中的文档的样子。
我的用户可以拥有多个设备,因此我尝试将更多字段附加到设备对象。我曾尝试将 $push 用于数组而不是对象,但我不喜欢稍后从数据库中检索数据时必须访问数据的方式。
于是我开始使用$set。 $set 很好用,因为它为我提供了我希望我的数据保存在数据库中的格式,但它每次都会不断覆盖设备对象中的一个键值对,我不希望这种情况发生。
db.go
func AddDeviceToProfile(uid string, deviceId int, deviceName string) {
client := ConnectClient()
col := client.Database(uid).Collection("User")
idString := strconv.Itoa(deviceId)
filter := bson.M{"uid": uid}
update := bson.M{
"$set": bson.M{"devices": bson.M{idString: deviceName}}, <------ Need to fix this
}
option := options.FindOneAndUpdate()
_ = col.FindOneAndUpdate(context.TODO(), filter, update, option)
log.Print("Device Added")
_ = client.Disconnect(context.TODO())
}
我研究过使用 $addFields,但我不知道我是否正确地使用了它,我只是替换了上面的 $set 并添加了 $addFields,我也尝试过这种方式
update := bson.M{
"devices": bson.M{"$addFields": bson.M{idString: deviceName}},
}
我希望我的文档看起来像什么
您需要的是 $set 指令,而不是使用 $push 或 $addFields。
要在嵌入文档中指定字段,请使用圆点表示法。
对于匹配条件 _id 等于 100 的文档,以下操作更新设备文档中的 make 字段:
db.products.update(
{ _id: 100 },
{ $set: { "devices.make": "zzz" } }
)
将它们转换为 Go 语法也很容易。你在做什么是正确的。以下应该有效或可能需要一些调整。
func AddDeviceToProfile(uid string, deviceId int, deviceName string) {
client := ConnectClient()
col := client.Database(uid).Collection("User")
idString := strconv.Itoa(deviceId)
filter := bson.M{"uid": uid}
update := bson.M{"$set": bson.M{"devices." + idString: deviceName}}
option := options.FindOneAndUpdate()
_ = col.FindOneAndUpdate(context.TODO(), filter, update, option)
log.Print("Device Added")
_ = client.Disconnect(context.TODO())
}
我正在尝试将字段附加到我的 mongodb 集合中的对象。到目前为止,这就是我在 MongoDB 中的文档的样子。
我的用户可以拥有多个设备,因此我尝试将更多字段附加到设备对象。我曾尝试将 $push 用于数组而不是对象,但我不喜欢稍后从数据库中检索数据时必须访问数据的方式。
于是我开始使用$set。 $set 很好用,因为它为我提供了我希望我的数据保存在数据库中的格式,但它每次都会不断覆盖设备对象中的一个键值对,我不希望这种情况发生。
db.go
func AddDeviceToProfile(uid string, deviceId int, deviceName string) {
client := ConnectClient()
col := client.Database(uid).Collection("User")
idString := strconv.Itoa(deviceId)
filter := bson.M{"uid": uid}
update := bson.M{
"$set": bson.M{"devices": bson.M{idString: deviceName}}, <------ Need to fix this
}
option := options.FindOneAndUpdate()
_ = col.FindOneAndUpdate(context.TODO(), filter, update, option)
log.Print("Device Added")
_ = client.Disconnect(context.TODO())
}
我研究过使用 $addFields,但我不知道我是否正确地使用了它,我只是替换了上面的 $set 并添加了 $addFields,我也尝试过这种方式
update := bson.M{
"devices": bson.M{"$addFields": bson.M{idString: deviceName}},
}
我希望我的文档看起来像什么
您需要的是 $set 指令,而不是使用 $push 或 $addFields。
要在嵌入文档中指定字段,请使用圆点表示法。
对于匹配条件 _id 等于 100 的文档,以下操作更新设备文档中的 make 字段:
db.products.update(
{ _id: 100 },
{ $set: { "devices.make": "zzz" } }
)
将它们转换为 Go 语法也很容易。你在做什么是正确的。以下应该有效或可能需要一些调整。
func AddDeviceToProfile(uid string, deviceId int, deviceName string) {
client := ConnectClient()
col := client.Database(uid).Collection("User")
idString := strconv.Itoa(deviceId)
filter := bson.M{"uid": uid}
update := bson.M{"$set": bson.M{"devices." + idString: deviceName}}
option := options.FindOneAndUpdate()
_ = col.FindOneAndUpdate(context.TODO(), filter, update, option)
log.Print("Device Added")
_ = client.Disconnect(context.TODO())
}