简体   繁体   中英

MongoDB Driver C#, Search by nested property using filter definition

I need help with building filter for MongoCollection of class A when I have filters for class B

    public class A
    {
        public string ExampleAProperty { get; set; }

        public B NestedB { get; set; }
    }

    public class B
    {
        public string ExampleBProperty { get; set; }
    }

    public class SearchClass
    {
        public async Task<ICollection<A>> SearchAsync(IMongoCollection<A> collection)
        {
            // this is just simple example of possible dozens predefined filters for B class.
            var bFilter = Builders<B>.Filter.Eq(x => x.ExampleBProperty, "Example");

            // in need filter A where B meets provided filters 

            var cursor = await collection.FindAsync(
                Builders<A>.Filter.Eq(x => x.NestedB, bFilter); // how?
            );
            return await cursor.ToListAsync();
        }
    }

Please don't use IMongoQueryable. Thank you

You can easily do this:

var aFilter = Builders<A>.Filter.Eq(a => a.NestedB.ExampleBProperty, "Example");
var cursor = await collection.FindAsync(aFilter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM