MongoDB 文档未正确映射到 C# 对象

问题描述 投票:0回答:1

问题

我的演示代码(如下)遇到以下问题:

执行下面的行时...

var customer = collection.Find(filter).FirstOrDefault();

...以下错误...

System.FormatException: An error occurred while deserializing the Header property of class Customer: Element 'id' does not match any field or property of class Header.
 ---> System.FormatException: Element 'id' does not match any field or property of class Header.

...发生。

加:

如果我将“[BsonIgnoreExtraElements]”装饰器添加到“Customer”和“Header”类,则查询运行不会出现错误,但“header.id”的值为“null”...

[Customer]
    Id [string] = "65d5fa85e4eb515ee6f6301a"
    GivenName [string] = "Renata"
    [Header]
        Id [string] = null

MONGODB 文件(记录)

{
  "_id": {
    "$oid": "65d5fa85e4eb515ee6f6301a"
  },
  "given_name": "Renata",
  "address_filled": false,
  "header": {
    "id": "0945f7cd-16a8-4ea6-b87f-c24e90dcfbc6"
  }
}

演示代码

以下代码的目的是使用

id
包通过其
header
子文档的
MongoDB.Driver
查询 MongoDB 文档,并将返回结果映射到 C# 类(使用
MongoDB.Bson
)。

映射 MONGODB 文档的 C# 类(记录)

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

public class Header
{
    [BsonElement("id")]
    public string Id { get; set; }
}

public class Customer
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId Id { get; set; }

    [BsonElement("given_name")]
    public string GivenName { get; set; }

    [BsonElement("address_filled")]
    public bool AddressFilled { get; set; }

    [BsonElement("header")]
    public Header Header { get; set; }
}

执行 MONGODB 查询的代码

使用

MongoDB.Driver
通过
id
子文档中的
header
查询文档:

using MongoDB.Driver;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Connection string and database setup
        var connectionString = "your_connection_string";
        var client = new MongoClient(connectionString);
        var database = client.GetDatabase("customers");
        var collection = database.GetCollection<Customer>("customers");

        // Query to find the document by header id
        var filter = Builders<Customer>.Filter.Eq("header.id", "0945f7cd-16a8-4ea6-b87f-c24e90dcfbc6");

        // Execute the query
        var customer = collection.Find(filter).FirstOrDefault();

        // Display the result
        if (customer != null)
        {
            Console.WriteLine($"Customer Name: {customer.GivenName}");
            Console.WriteLine($"Header ID: {customer.Header.Id}");
        }
        else
        {
            Console.WriteLine("Customer not found.");
        }
    }
}

说明

  • MongoClient:创建与 MongoDB 实例的连接。
  • GetDatabase:按名称检索数据库。
  • GetCollection:获取名为
    customers
    的集合。
  • Builders.Filter.Eq:创建一个过滤器来查找
    header.id
    等于指定值的文档。
  • Find:执行查询,
    FirstOrDefault
    返回第一个匹配的文档,如果没有找到匹配则返回null。
  • BsonElement:将属性映射到MongoDB文档中的相应字段。

此示例演示通过

header.id
查询 MongoDB 集合,并使用 MongoDB.Driver 和 MongoDB.Bson 包将结果映射到 C# 对象。


谢谢!🤗

c# mongodb mongodb-query
1个回答
0
投票

在 MongoDB .NET 驱动程序中,

Id
属性将自动映射到
_id
字段。您可以使用
[BsonNoId]
属性来防止默认行为。

[BsonNoId]
public class Header
{
    [BsonElement("id")]
    public string Id { get; set; }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.