gpt4 book ai didi

c# - MongoDB - 在单个集合中插入多种类型的对象

转载 作者:行者123 更新时间:2023-12-05 03:01:27 25 4
gpt4 key购买 nike

我有不同类的对象,想将它们存储在 MongoDB 中的单个集合中。

我将 asp.net core 2.2 与 MongoDB.Driver 2.8.0 和 MongoDB 4.0.5 一起使用。我做了一些尝试。

想法:我可以将以下所有对象存储在一个集合中。目前我可以保存它们,但我无法阅读它们 -> 获取“FormatException:元素‘_id’不匹配类 Mongo2.Models.MongoEntity 的任何字段或属性。” 但仅在重新运行应用程序之后。在空数据库上,我可以保存对象并检索它们。当我关闭应用程序并且不删除数据库时, collection.Find(book => true).ToList(); 出现异常
}

我想我遗漏了一些步骤或者我的方法无效 - 在互联网上找不到任何可行的示例。

任何帮助将不胜感激。通过共享工作解决方案/示例的链接或在此处。

型号:

public class MongoEntity
{
}

[BsonIgnoreExtraElements]
public class BookLibrary : MongoEntity
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string BookLibraryId { get; set; }
public List<Book> Library { get; set; }
}

[BsonIgnoreExtraElements]
public class Book : MongoEntity
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string BookId { get; set; }

[BsonElement("Name")]
public string BookName { get; set; }

[BsonElement("Price")]
public decimal Price { get; set; }

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

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

[BsonElement("Date")]
public DateTime Added { get; set; }
}

[BsonIgnoreExtraElements]
public class Author : MongoEntity
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string AuthorId { get; set; }
[BsonElement("Author name")]
public string Name { get; set; }
[BsonElement("Author last name")]
public string LastName { get; set; }
}

[BsonIgnoreExtraElements]
public class Category : MongoEntity
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string CategoryId { get; set; }
[BsonElement("Category name")]
public string CategoryName { get; set; }
}

数据上下文:

public class BookContext
{
private readonly IMongoDatabase _database;
private readonly IMongoClient _client;

public BookContext(IConfiguration configuration)
{
_client = new MongoClient(configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>().ConnectionString);
_database = _client.GetDatabase(configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>().DatabaseName);

}

public List<MongoEntity> Get()
{
var collection = _database.GetCollection<MongoEntity>("Books");
return collection.Find(book => true).ToList();
}

public void AddBsonDocument()
{
var author = new Author()
{
Name = "Name",
LastName = "Lastname",
AuthorId = ObjectId.GenerateNewId().ToString()
};

var category = new Category()
{
CategoryId = ObjectId.GenerateNewId().ToString(),
CategoryName = "Non-fiction"
};

var book = new Book()
{
BookName = "Random title",
Category = category,
Price = 54.93m,
Added = DateTime.Now,
BookId = ObjectId.GenerateNewId().ToString(),
Author = author
};

var lib = new BookLibrary(){BookLibraryId = ObjectId.GenerateNewId().ToString(), Library = new List<Book>()};
lib.Library.Add(book);



var collection = _database.GetCollection<MongoEntity>("Books");

var list = new List<MongoEntity> {lib, book, author, category};

collection.InsertMany(list);

}
}

最佳答案

结果证明答案非常简单明了,但不知何故我错过了。

对象的类型与集合名称没有很强的相关性,所以我可以使用:

var books = _database.GetCollection<Books>("Books");
var bookLibraries = _database.GetCollection<BookLibrary>("Books");
var authors = _database.GetCollection<Author>("Books");
var categories = _database.GetCollection<Category>("Books");

另一种解决方案是使用 BsonDocument 类型:

var books = _database.GetCollection<BsonDocument>("Books");

就是这样。

关于c# - MongoDB - 在单个集合中插入多种类型的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com