Adding a new property to Cosmos DB

旧城冷巷雨未停 提交于 2019-12-11 16:49:12

问题


I've actually had quite a bit of trouble finding an answer to this question.

If I create a new document in a collection, I can give it whatever properties I want.

Is there a way to add a new property to all my documents? Cosmos DB seems to be resisting everything I've tried. Do I have to delete the existing documents and make new ones?

It would be nice to know how to do this in C# code, if possible. Javascript is fine too, though.


回答1:


CosmosDB does not support partial document updates so you would have to Update all of your existing documents with the new property if you really need it there. There are many ways you can do that including stored procedures.

If you are ok to use C# then you can do that easily with Cosmonaut

var items = await cosmoStore.Query().ToListAsync();
foreach(var item in items){
   item.YourProperty = "YourValue";
}
await cosmosStore.UpdateRangeAsync(items);

When selecting the items your value will default to the default property value if it's not present and by updating it, it will be added.



来源:https://stackoverflow.com/questions/50280374/adding-a-new-property-to-cosmos-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!