我在c#中更新一条数据的时候,怎么更新一个json数组呢?比如我数据库中已经有一条数据了。{ "_id" : ObjectId("58183b67afa78926f9ea8513"), "name" : "joy", "age" : 23 },这样一条数据,我想要在插入一个字段,这个字段是一个数组,这样的:"add1":[{...},{....}]。。这样该怎么更新呢?我看c#驱动里面有个Builders<BsonDocument>.Update.Combine()这个方法,我这样写的:var up4 = Builders<BsonDocument>.Update.Set("address1.street4", "East 31st Street4");var up5 = Builders<BsonDocument>.Update.Set("address1.street5", "East 31st Street5");var up6 = Builders<BsonDocument>.Update.Set("address1.street6", "East 31st Street6");var update = Builders<BsonDocument>.Update.Combine(up4, up5, up6);但是我用这个方法只能更新成"add1":{... , .... , ....}这样欸。比如这种"address1" : { "street" : "East 31st Street", "street1" : "East 31st Street", "street2" : "East 31st Street2", "street3" : "East 31st Street3", "street4" : "East 31st Street4" }
2 回答
牧羊人nacy
TA贡献1862条经验 获得超7个赞
使用#set 修改器,拿走,不谢 MongoDB 数组
db.users.updateMany( {}, {$set: { followers:[ {name:"t5"},{name:"t7"} ] } } )
- 2 回答
- 0 关注
- 737 浏览
添加回答
举报
0/150
提交
取消