C# Mongo Driver AsQueryable to Builder Filter

How to i am change this

var pp = from doc in collection.AsQueryable()
                                         select doc.mainarray into nested
                                         from nestedDoc in nested
                                         where nestedDoc.sID == ssID
                                         from dprg in nestedDoc.d.DArray
                                         where dprg.ID == time
                                         select dprg;

to samiliar to this

Builders<farray>.Filter.Eq(...............

My full code

var collection = MongoDB.GetCollection<farray>(MAINDB);
                                var pl = Builders<farray>.Update.Push("f1.$.f2.$.f3.$.f4", new Puts() { ID = new STX() {ID=people}, Type = type });
                                //var qq = from collection.AsQueryable();
                                
                                var pp = from doc in collection.AsQueryable()
                                         select doc.mainarray into nested
                                         from nestedDoc in nested
                                         where nestedDoc.sID == ssID
                                         from dprg in nestedDoc.d.DArray
                                         where dprg.ID == time
                                         select dprg;
                                       /*  
                                var pupdate = from doc in collection.AsQueryable()
                                              select doc.mainarray into nested
                                              from nestedDoc in nested
                                              where nestedDoc.sID == ssID
                                              from dprg in nestedDoc.d.DArray
                                              where dprg.ID == time
                                              select dprg;
                                              */
                                collection.FindOneAndUpdate(pp,pl);

MongoDB Driver Version: v2.14.1
Coding Language: C#
I getting error on collection.FindOneAndUpdate(pp,pl);

CS0411  IMongoCollection<Program.farray>.FindOneAndUpdate<TProjection>(FilterDefinition<Program.farray>, UpdateDefinition<Program.farray>, FindOneAndUpdateOptions<Program.farray, TProjection>, CancellationToken)'...

I try this collection.FindOneAndUpdate(filter:pp,update:pl); but nothing changed

farray is a class
pp variable is a IQueryable<DVMClass>
pl variable is a UpdateDefinition<farray>

I think i getting error because pp variable is not a FilterDefinition
If this error because this how to i change pp variable to a FilterDefinition

Hi, @Kaan_Faruk_Kinali,

Welcome to the MongoDB Community Forums.

I see that you are attempting to execute a FindAndUpdateOne operation, which takes a FilterDefinition and an UpdateDefinition. You can specify the FilterDefinition in a variety of ways, including Builders<T>.Filter, a JSON string, or an expression. Notably a MongoQuery is not convertible to a FilterDefinition. You will have to express your FilterDefinition using one of the supported FilterDefinition conversions to call FindAndUpdateOne.

Sincerely,
James

1 Like

How to I change this MongoQuery to FilterDefinition ?
Please help me on this

Can this work ?

var ppp = Builders<farray>.Filter.ElemMatch(x => x.mainarray,
                                        Builders<fsingle>.Filter.Eq(y => y.sID, ssID) &
                                        Builders<fsingle>.Filter.ElemMatch(y => y.d.DArray, p => p.ID == time));

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.