[CSHARP-1680] How use driver with command like shell? Created: 30/May/16  Updated: 05/Apr/19  Resolved: 01/Jun/16

Status: Closed
Project: C# Driver
Component/s: API
Affects Version/s: 2.2.3
Fix Version/s: None

Type: Task Priority: Trivial - P5
Reporter: Andrzej Kaminsky Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, FW 4.5, MS VS 2015 Update 2



 Description   

Good day! Sorry for my bad English.
I have a problem. My program must recive out command in style shell MongoDB and execute, but i not understand how make it.
I find old example http://stackoverflow.com/questions/6120629/can-i-do-a-text-query-with-the-mongodb-c-sharp-driver, but he not compiled with current version driver. I modify example, but he looping in point return cursor.ToList();. Database have 33 document, in console all works. How can i resolve my problem?
Modify code:

MongoServerSettings mss = new MongoServerSettings();
mss.ConnectionMode = ConnectionMode.Automatic;
mss.Server = new MongoServerAddress("localhost", 27020);
MongoServer ms = new MongoServer(mss);
var collection = ms.GetDatabase("local").GetCollection("title");
var items = collection.GetItems<BsonDocument>("{ val : 'А'}", "{val:1}");
 
    public static class MongoDbExt
    {
        public static List<T> GetItems<T>(this MongoCollection collection,
                string queryString, string orderString) where T : class
        {
            MongoCursor<T> cursor;
            SortByWrapper order = null;
            QueryDocument query = null;
            if ((queryString != "") && (queryString != null))
            {
                var queryDoc = BsonSerializer.Deserialize<BsonDocument>(queryString);
                query = new QueryDocument(queryDoc);
            }
            cursor = collection.FindAs<T>(query);
            //cursor = collection.FindAs<T>(null);
            if ((orderString != "") && (orderString != null))
            {
                var orderDoc = BsonSerializer.Deserialize<BsonDocument>(orderString);
                order = new SortByWrapper(orderDoc);
            }
            cursor.SetSortOrder(order);
            return cursor.ToList();
        }
    }



 Comments   
Comment by Andrzej Kaminsky [ 02/Jun/16 ]

Thanks, Craig

Comment by Craig Wilson [ 01/Jun/16 ]

Hi Andrzej,

This compiles for me without a problem. If the ToList method is a problem, make sure you have a using statement for System.Linq.

Also, in the future, it's better to ask questions like this either on our user group or on stackoverflow.com. We generally use JIRA for tracking bug tickets and feature requests.

Craig

Comment by Andrzej Kaminsky [ 30/May/16 ]

Oh, it is important: 2.2.3 is version Driver, 3.2 - version server

Generated at Wed Feb 07 21:40:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.