Skip to content

[BUG] Find all related orders of a customer returns empty #1651

@kfirpeled

Description

@kfirpeled

Version
LiteDb 5.0.7
OSX Catalina 10.15.4
dotnet 3.1.102

Describe the bug
Finding customer's order returns zero results when using lambda expression.
Digging into this deep I could see that the BsonMapper is not resolving the expression correctly

Code to Reproduce


    public class Order : BaseEntity
    {
        public Customer? Customer { get; set; }
    }

    public class Customer : BaseEntity
    {
        public string? Name { get; set; }
    }

    public class BaseEntity
    {
        public Guid Id { get; set; }
    }
    public class CustomerRelationshipsTests : IDisposable
    {
        private LiteDatabase _database;
        private ILiteCollection<Customer> _customerCollection;
        private ILiteCollection<Order> _orderCollection;

        public CustomerRelationshipsTests()
        {
            BsonMapper.Global.Entity<Order>().DbRef(order => order.Customer);

            _database = new LiteDatabase("customer1.db");

            _orderCollection = _database.GetCollection<Order>();
            _customerCollection = _database.GetCollection<Customer>();
        }


        [Fact]
        public void Find_ByRelationId_Success()
        {
            var customer = new Customer() {Name = "John",};

            Assert.True(_customerCollection.Upsert(customer));
            Assert.True(_customerCollection.Upsert(new Customer() {Name = "Anonymous"}));

            Assert.NotEqual(Guid.Empty, customer.Id);

            var order = new Order()
            {
                Customer = customer,
            };
            var order2 = new Order()
            {
                Customer = new Customer() {Id = customer.Id},
            };
            var orphanOrder = new Order();

            Assert.True(_orderCollection.Upsert(orphanOrder));
            Assert.True(_orderCollection.Upsert(order));
            Assert.True(_orderCollection.Upsert(order2));

            customer.Name = "Josh";
            Assert.True(_customerCollection.Update(customer));


            var actualOrders = _orderCollection
                .Include(orderEntity => orderEntity.Customer)
                // NOT WORKING (BsonExpression generated from the lambda is $.Customer._id instead of $.Customer.$id)
                .Find(orderEntity => orderEntity.Customer.Id == customer.Id)
                // .Find(Query.EQ("$.Customer.$id", customer.Id)) WORKS - returning results correctly
                // .Find(Query.EQ("$.Customer._id", customer.Id)) NOT WORKING
                .ToList();

            Assert.Equal(2, actualOrders.Count); // Fails here, actual result is zero
            Assert.Equal(new[] {customer.Name, customer.Name},
                actualOrders.Select(actualOrder => actualOrder.Customer.Name));
            Assert.Equal(2, (_customerCollection.FindAll().ToList()).Count);
            Assert.Equal(3, (_orderCollection.FindAll().ToList()).Count);
        }

        public void Dispose()
        {
            _database.DropCollection(_orderCollection.Name);
            _database.DropCollection(_customerCollection.Name);
            _database.Dispose();
        }
    }


**Is it related to BsonMapper ? **

var expr = BsonMapper.Global.GetExpression<Order, bool>(orderEntity => orderEntity.Customer.Id == customer.Id);

Returns ($.Customer._id=@p0)
Should it return ($.Customer.$id=@p0) ? Or the issue is somewhere else?

Expected behavior
I expect to use find with lambda to select customer's order by the customer's id.
A workaround for me is to build the query myself (which is not ideal)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions