Sunday 15 March 2015

.net - C# mongo queries with json strings -


It looks so basic that I'm convinced that I'm ignoring an orbit or a method, but For me, I can not find it

I've got a Jason string like this:

  {SendId: 4, "Events.Code": {$ all: [2], $ nin: [3 ]}}   

I can run this in a mongo shell against a find () or count () and what I find I am getting it. What is the easiest way to deal with this in C #?

  • BsonDocument has a good parsing method, but it is IMongoQuery
  • does not implement BsonDocument code>, and it implements IMongoQuery , but in it It does not have its own parsing method, and I take a BsonDocument [] from QueryDocument to BsonDocument
  • the aggregation framework, but Sometimes I just want to do a simple search or calculation work
  • Some of these questions are big and Gross, and I do not want to make them a line one line with query builder class

    , if the database works with json documents, and I get this stuff Can not run in the shell, is there any way to run it with the driver?

    It's ugly, but you can do this by doing this to BsonDocument and then wrapping in QueryDocument

      BsonDocument Query = MongoDB.Bson.Serialization.BsonSerializer. . Direct & lt; BsonDocument & gt; ("{SendId: 4, 'Events.Code': {$ all: [2], $ nin: [3]}}"); QueryDocument queryDoc = new query document (query); Var Results = Archive. FDAS & lt; Typeofferscent estimated & gt; (QueryDoc); // Simply use the search   

    If there is anything that you plan to do often, you can always wrap it in a method, or a like the following JSQueryDocument can create class:

      public class JSQueryDocument: QueryDocument {public JSQueryDocument (string query): base (MongoDB.Bson.Serialization.BsonSerializer.Deserialize.Deserialize & lt; BsonDocument & gt ; {Query}) {// Perhaps it is better to do this as a method instead of constructor / / debugging it queries Programming can not be formatted correctly}} /// ... var results = collection. Find (New JSQueryDocument ("{SendId: 4, 'Events.Code': {$ All: [2], $ nin: [3]}}"));    

  • No comments:

    Post a Comment