Getting the documents in the specified order from MongoDB using C# driver -


this question has answer here:

i new mongodb , wanted know whether possible fetch documents id in order have specified in array. using official c# driver.

problem description

i have documents in collection this

{     "_id" : "1",     "name" : "assignment 1" } 

to

{     "_id" : "100",     "name" : "assignment 100" } 

i have array of ids { "4", "19", "10", "6" } , need fetch documents specified in same order in array.

these queries tried

{ "_id" : { "$in" : ["4", "19", "10", "6"] } }

{ "$or" : [{ "_id" : "4" }, { "_id" : "19" }, { "_id" : "10" }, { "_id" : "6" }] } 

but both of these queries returning documents in following order enter image description here

but expected result is

enter image description here

the option in front of me right make separate db requests each elements in array think bad option.

can of please steer me on fetching documents in order have specified in array. using mongodb c# driver , expecting answer based on c#.

thanks,
kapil

well have managed use aggregation framework make work.

this way have achieved it. happy if can suggest better way this.

string[] fetchingids = { "4", "19", "10", "6" };  imongoquery fetchquery = query<assignment>.in(x => x.id, fetchingids); bsondocument match = new bsondocument { { "$match", fetchquery.tobsondocument() } };  bsondocument currentdocument = null; bsondocument finaldocument = null; bsonvalue processingdocument = null; (int = 0; < fetchingids.length; i++) {     bsonelement ifstatement = new bsonelement("$eq", new bsonarray(new[] { "$_id", fetchingids[i] }));     bsonarray conditionstatement = new bsonarray(new[] { new bsondocument(ifstatement), bsonvalue.create(i + 1), -1 });     currentdocument = new bsondocument("$cond", conditionstatement);      if (finaldocument == null)     {         finaldocument = currentdocument;     }     else     {         processingdocument = null;         bsonvalue tempdoc = finaldocument["$cond"][2] bsondocument;          if (tempdoc == null)         {             processingdocument = finaldocument["$cond"];         }          while (tempdoc != null)         {             if ((tempdoc["$cond"][2] bsondocument) == null)             {                 processingdocument = tempdoc["$cond"];                 tempdoc = null;             }             else             {                 tempdoc = tempdoc["$cond"][2];             }         }          processingdocument[2] = currentdocument;     } }   bsondocument project = new bsondocument { { "$project", new bsondocument                                                          {                                                             // return document                                                             {"obj","$$root"},                                                             {"weight",finaldocument},                                                         }                                           } };  bsondocument sort = new bsondocument { { "$sort", new bsondocument { { "weight", 1 } } } };  var result = assignmentcollection.aggregate(new aggregateargs {     pipeline = new[] { match, project, sort }, }).tolist(); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -