我想要获得mongodb中的所有Carts对象,但是结果是缺少项列表。
示例数据mongodb:
{
"_id": "temp",
"total": "2300000",
"buyer": "test",
"items": [
{
"name": "IP-8",
"amount": "2",
"price": "20000"
},
{
"name": "IP-12",
"amount": "2",
"price": "20000"
}
]
}
结果:
[{"id":"temp","buyer":"test","total":2300000}]
这里有两个类:
public class Carts
{
[BsonId]
public string Id { get; set; }
public string buyer { get; set; }
public double total { get; set; }
[BsonElement("items")]
List<CartItem> items { get; set; }
}
public class CartItem
{
[DataMember]
public string name { get; set; }
[DataMember]
public int amount { get; set; }
[DataMember]
public double price { get; set; }
}
函数类:
public class TempCartService
{
private readonly IMongoCollection<Carts> _temp;
//private readonly IMongoCollection<CartItem> _items
public TempCartService(IOptions<ShopDbSetting> options)
{
var mongoClient = new MongoClient(options.Value.ConnectionString);
_temp = mongoClient.GetDatabase(options.Value.DatabaseName)
.GetCollection<Carts>("Carts");
}
public async Task<List<Carts>> FindAll() =>
await _temp.Find(_ => true).ToListAsync();
}
发布于 2022-09-30 15:18:05
请将可访问的修饰符更改为public
,请参阅下面的代码
public class Carts
{
[BsonId]
public string Id { get; set; }
public string buyer { get; set; }
public double total { get; set; }
[BsonElement("items")]
public List<CartItem> items { get; set; }
}
public class CartItem
{
[DataMember]
public string name { get; set; }
[DataMember]
public int amount { get; set; }
[DataMember]
public double price { get; set; }
}
https://stackoverflow.com/questions/73910093
复制相似问题