MongoDb c# bad unknown operator exception -


i execute following query :

{     $query : {          "userid" : 11851, "p2l.listid" : 38882, "isdeleted" : false      },            $orderby: { email: 1},       $skip: 0,      $limit:100  } 

via following code :

bsondocument document = bsonserializer.deserialize<bsondocument>(querystring); querydocument querydoc = new querydocument(document); var toreturn = collection.find(querydoc); return toreturn.tolist(); 

and following exception:

[mongodb.driver.mongoqueryexception] = {"queryfailure flag true (response { \"$err\" : \"can't canonicalize query: badvalue unknown top level operator: $query\", \"code\" : 17287 })."}

i use mongodb 3.0 , c# driver 2.0.

is there other way execute query ? need keep in string format in sql database, need serialize/deserialize it.

that isn't valid query. while can add $orderby way (but it's not recommended), skip , limit not part of document. best thing not try build way , instead let driver build you. make application future proof when server changes how queries issued (https://jira.mongodb.org/browse/server-15176).

bsondocument document = bsondocument.parse(querystring); querydocument querydoc = new querydocument((bsondocument)document["$query"]);  return collection.find(querydoc)     .setskip((int)document["$skip"])     .setlimit((int)document["$limit"))     .setsort(new sortdocument((bsondocument)document["$orderby"]))     .tolist(); 

obviously, if of these conditional, you'll need handle too.

on final note, if querystring querystring, think you're going find problematic. impossible use indexes correctly because "users" can absolutely want. plus, requiring them understand mongodb query language. better model allowed explicitly.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -