Simple-MongoDB – Implicit support for Cursors

Minor update just checked in. The Source-code as well as binaries can be downloaded here.

I have reworked the underlying command-objects that are used to communicate with the MongoDb-server (Note! These are consumed by the EntityStore and Session API, hence you as an end user doesn’t have to use these commands). I have now implemented implicit-support for cursors. So if a query of yours gives more result then the query could return per request, additional request will be sent until the cursor has been emptied. Each resultset is appended to the Response-object of your query, so you don’t have to take care of this.

Example
Five persons exists in the db, and the QueryCommand doesn’t specify a Selector, hence all five will be returned. But since the NumberOfDocumentsToReturn is explicitly set to two, there will be three requests performed.

[TestMethod]
public void QueryManyPersonsUsingCursor_SpecificNumberOfDocumentsToReturnGivesCursor_AllPersonsAreReturned()
{
    var person = new[]
                 {
                     new Person { Name = "Daniel1", Age = 28 },
                     new Person { Name = "Daniel2", Age = 29 },
                     new Person { Name = "Daniel3", Age = 30 },
                     new Person { Name = "Daniel4", Age = 31},
                     new Person { Name = "Daniel5", Age = 32}
                 };
    InsertDocuments(Constants.Collections.PersonsFullCollectionName, person);

    using (var cn = CreateConnection())
    {
        var queryCommand = new QueryDocumentsCommand<Person>(cn)
        {
            NodeName = Constants.Collections.PersonsFullCollectionName,
            NumberOfDocumentsToReturn = 2
        };
        queryCommand.Execute();

        Assert.AreEqual(5, queryCommand.Response.NumberOfDocuments);
    }
}

When you are using the Session-API or the EntityStore-API, the NumberOfDocumentsToReturn is sat to zero, which means that the default value specified by the db will be used. Neverthless, you don’t have to think about the cursor.

//Daniel

15 thoughts on “Simple-MongoDB – Implicit support for Cursors

    • Hi,
      Sorry for the late answer. The example below does what you want. Note that I have used anonymous types, but you could of course use specific C#-classes or strings.

      //Daniel

      [TestMethod]
      public void Find_MatchingValueInArray_ReturnsMatchingPost()
      {
          var collectionName = "BlogEntries";
          var documents = new[]
          {
              new { Title = "MongoDb and testing", Tags = new[] { "MongoDb", "Testing" } }, 
              new { Title = "MongoDb tutorial", Tags = new[] { "MongoDb", "Tutorial" } }
          };
          TestHelper.InsertDocuments(collectionName, documents);
      
          using(var session = new SimoSession(TestHelper.CreateConnection()))
          {
              var tutorial = session[DbName][collectionName].FindOneInfered(new {Title = ""}, @"{Tags : 'Tutorial'}");
      
              Assert.AreEqual("MongoDb tutorial", tutorial.Title);
          }
      }
      
      • Will that work for such an example :
        There are such an objects in MongoDB collection :
        {_id:123123,n=[tag1,tag2,tag3,tag5],name:”sdfasfs”}
        {_id:123123,n=[tag1,tag22,tag3,tag4],name = “tuyfgdfd”}
        And I need to get both of them with query like :
        {n:{$all:[tag1,tag2]}}

  1. Just confirmed:

    Adding posts:

    new { Title = "MongoDb other", Tags = new[] { "MongoDb", "Misc" } }, 
    new { Title = "MongoDb and testing tutorial", Tags = new[] { "MongoDb", "Testing", "Tutorial" } }, 
    new { Title = "MongoDb tutorial", Tags = new[] { "MongoDb", "Tutorial" } }
    

    Can be queries using either:

    var tutorials = session[DbName][collectionName].FindInfered(new { Title = "" }, @"{Tags : {$all :['Tutorial']}}");
    

    or

    var tutorials = session[DbName][collectionName].FindInfered(new { Title = "" }, @"{Tags : 'Tutorial'}");
    

    Both will return two posts.

    //Daniel

    • Hello, I’ve new feature request ^_^ : I need to limit results ? Should I use FindIfered method ?

      • Hi,
        I saw this last night but didn’t get the time to check it in before work today.

        There will be limit and skip arguments to the Find functions.

        Basically, it’s about providing these two values to the QueryDocumentsCommand that is executed:

        cmd.NumberOfDocumentsToReturn
        cmd.NumberOfDocumentsToSkip

        So you could add a “hack” there until I’m finished.

        //Daniel

      • Hi,

        Fixed! In the trunk, revision r61.

        If you provide a value for Limit only that number of docs are returned and the cursor is automatically closed.

        I will provide zip’s during the day (I really got to get that buildserver up).

        //Daniel

  2. Hello, I have a problem with utf-8 encoding (Cyrillic text ) there are no such a problem in Newton.Json and mongo shell can’t work with Cyrillic(Russian) and incorrectly works with Polish. If I’m right you are from Germany and there are some special letters in German, can’t you test how mongo works with your awesome driver and German ?

    • Hi,
      I can test to store some strings containing Swedish chars. I can’t do this until tonight (like in 7hrs). I’m currently at work so….

      //Daniel

    • Hi,

      I can insert and read data using the provider. If I try to insert a document directly using the mong.exe-client I get an error.

      Error in mongo.exe

      //Daniel

      • Hmmm.

        I will try to test this tomorrow. Can you send me some code? I have sent you an email so that you can reply to that.

        //Daniel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s