0

i want to get all Carts object in mongodb but the result is missing the list of items

sample data mongodb:

{
  "_id": "temp",
  "total": "2300000",
  "buyer": "test",
  "items": [
    {
      "name": "IP-8",
      "amount": "2",
      "price": "20000"
    },
    {
      "name": "IP-12",
      "amount": "2",
      "price": "20000"
    }
  ]
}

Result:

[{"id":"temp","buyer":"test","total":2300000}]

here are 2 class:

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; }
}

function class:

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();
}
1
  • This works as expected in a small sample; the list is filled. Are there any changes in the sample in comparison to the real-world-code/-documents? What could be possible differences? Commented Sep 30, 2022 at 14:27

1 Answer 1

1

please change your accessible modifier to public see the below code

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.