Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
防止mongodb c#驱动程序对嵌套对象进行id到#id序列化_C#_Mongodb_Serialization_Deserialization_Mongodb .net Driver - Fatal编程技术网

防止mongodb c#驱动程序对嵌套对象进行id到#id序列化

防止mongodb c#驱动程序对嵌套对象进行id到#id序列化,c#,mongodb,serialization,deserialization,mongodb-.net-driver,C#,Mongodb,Serialization,Deserialization,Mongodb .net Driver,我正在使用C#mongodb驱动程序更新mongodb中的记录。下面的代码对我来说很好,但它会自动将所有出现的“id”转换为“\u id” 更新记录后,我希望更新后的记录如下所示 { "_id": "kjsldfkjlsdkfjsd", "basicDetails": { "name": "test name", "description": "

我正在使用C#mongodb驱动程序更新mongodb中的记录。下面的代码对我来说很好,但它会自动将所有出现的“id”转换为“\u id”

更新记录后,我希望更新后的记录如下所示

{
  "_id": "kjsldfkjlsdkfjsd",
  "basicDetails": {
    "name": "test name",
    "description": "test desc",
    "status": {
      "id": 11,
      "name": "processing"
    },
    "createdBy": {
      "id": "123",
      "name": "some user"
    }
  }
}
但它保存了一个以下。它正在将所有出现的“id”转换为我不想要的“_id”

{
  "_id": "kjsldfkjlsdkfjsd", //This is ok
  "basicDetails": {
    "name": "test name",
    "description": "test desc",
    "status": {
      "_id": 11, //This should be "id"
      "name": "processing"
    },
    "createdBy": {
      "_id": "123",  //This should be "id"
      "name": "some user"
    }
  }
}

提前感谢…

在Mongo端,Id为
的属性名被锁定为
\u Id

你有两个选择。把它称为C端的其他东西,比如
IdStr
>

public class TextOption
{
    [BsonElement("id")]
    public string IdStr { get; set; } //don't want to convert to "_id"
    public string Name { get; set; }
}

public class Option
{
    [BsonElement("id")]
    public int IdStr { get; set; } //don't want to convert to "_id"
    public string Name { get; set; }
}
或使用基本贴图进行调整>

var client = new MongoClient();
var database = client.GetDatabase("test");
var collection = database.GetCollection<ProjectInfo>("projects");
BsonClassMap.RegisterClassMap<TextOption>(cm =>
{
    cm.AutoMap();
    cm.UnmapProperty(c => c.Id);
    cm.MapMember(c => c.Id)
        .SetElementName("id")
        .SetOrder(0) //specific to your needs
        .SetIsRequired(true); // again specific to your needs
});
BsonClassMap.RegisterClassMap<Option>(cm =>
{
    cm.AutoMap();
    cm.UnmapProperty(c => c.Id);
    cm.MapMember(c => c.Id)
        .SetElementName("id")
        .SetOrder(0) //specific to your needs
        .SetIsRequired(true); // again specific to your needs
});
using (var session = await client.StartSessionAsync())
{
    await collection.InsertOneAsync(session,
        new ProjectInfo
        {
            _Id = "604b5fa389ff6887d1b91a91",
            BasicDetails = new ProjectBasicDetail
            {
                CreatedBy = new TextOption { Id = "604b5fa389ff6887d1b91a93", Name = "a" },
                Description = "b",
                Name = "c",
                Status = new Option { Id = 123, Name = "status name" }
            }
        });
}
public class TextOption
{
    [BsonElement("id")]
    public string IdStr { get; set; } //don't want to convert to "_id"
    public string Name { get; set; }
}

public class Option
{
    [BsonElement("id")]
    public int IdStr { get; set; } //don't want to convert to "_id"
    public string Name { get; set; }
}
var client = new MongoClient();
var database = client.GetDatabase("test");
var collection = database.GetCollection<ProjectInfo>("projects");
BsonClassMap.RegisterClassMap<TextOption>(cm =>
{
    cm.AutoMap();
    cm.UnmapProperty(c => c.Id);
    cm.MapMember(c => c.Id)
        .SetElementName("id")
        .SetOrder(0) //specific to your needs
        .SetIsRequired(true); // again specific to your needs
});
BsonClassMap.RegisterClassMap<Option>(cm =>
{
    cm.AutoMap();
    cm.UnmapProperty(c => c.Id);
    cm.MapMember(c => c.Id)
        .SetElementName("id")
        .SetOrder(0) //specific to your needs
        .SetIsRequired(true); // again specific to your needs
});
using (var session = await client.StartSessionAsync())
{
    await collection.InsertOneAsync(session,
        new ProjectInfo
        {
            _Id = "604b5fa389ff6887d1b91a91",
            BasicDetails = new ProjectBasicDetail
            {
                CreatedBy = new TextOption { Id = "604b5fa389ff6887d1b91a93", Name = "a" },
                Description = "b",
                Name = "c",
                Status = new Option { Id = 123, Name = "status name" }
            }
        });
}
public class ProjectInfo
{
    [BsonId]
    public string _Id { get; set; } //This is primary key which unique for project info
    public ProjectBasicDetail BasicDetails { get; set; }
}