RavenDB:有没有办法从DB中删除具体字段?

时间:2016-02-03 09:33:29

标签: c# json database ravendb

我有这样一个Raven db文档结构:

{"Title":"abc",
"User":{"$type": "IOM.Server.Data", "Mail":...etc},
"Content": [
   {
      "Additional": {
         "Title": "abc",
         "$type": "IOM.Server.Data"
      }
   }]
}

在Visual Studio(C#)中,我收到错误“无法加载程序集'IOM.Server.Data'”。

我无法将此值读取为String,因为我无法使用名为“$ type”的变量。

现在我必须将User和Content作为Object类型读取,然后转换为Json,然后从Json Objects获取信息。如果我可以从db中删除所有这些字段(用户,内容 - 附加),因为我不需要它们会更容易。有没有办法做到这一点?解决这个问题是对的吗?

1 个答案:

答案 0 :(得分:1)

如果你在工作室看到$ type,那就意味着你可能已经实例化了用户'和'其他'一个扩展道具基类的类' User'和'附加'。 顺便说一下,如果要删除$ type,可以使用patch:

documentStore.DatabaseCommands.Patch("yourDocId/1",
                    new[] 
                    { 
                        new PatchRequest
                        {
                            Type = PatchCommandType.Modify,
                            Name = "User",
                            AllPositions = true,
                            Nested = new []
                            {                                   
                                 new PatchRequest
                                 {
                                      Type = PatchCommandType.Unset,
                                      Name = "$type"
                                 }                                    
                            }
                        },
                        new PatchRequest
                        {
                            Type = PatchCommandType.Modify,
                            Name = "Content",
                            AllPositions = true,
                            Nested = new []
                            {
                                new PatchRequest
                                {
                                    Type = PatchCommandType.Modify,
                                    Name = "Additional",
                                    AllPositions = true,
                                    Nested = new []
                                    {
                                        new PatchRequest
                                        {
                                            Type = PatchCommandType.Unset,
                                            Name = "$type"
                                        }
                                    }
                                }
                            }
                        }
                    });

希望这会有所帮助