Is there a way to submit a query that is expressed in the shell query syntax to the mongo c# driver
For example Something like
Coll.find { "myrecs","$query : { x : 3, y : "abc" }, $orderby : { x : 1 } } ");
To take an example from the shell guide
In MongoDB, we can perform text search using text index and $text operator. Text index: MongoDB proved text indexes that are used to find the specified text from the string content. Text indexes should be either a string or an array of string elements.
The $in operator allows you to write queries that will return documents with values matching one of multiple values held in an array. The following example query includes the $in operator, and will return documents whose name value matches either Everest or K2 : db.
MongoDB text search uses the Snowball stemming library to reduce words to an expected root form (or stem) based on common language rules. Algorithmic stemming provides a quick reduction, but languages have exceptions (such as irregular or contradicting verb conjugation patterns) that can affect accuracy.
In MongoDB, we can create text indexes using db. collectionName. createIndex() method. So, to index a field that contains either string or an array of string elements, pass a document in the createIndex() method that contains the field and the string literal(i.e., “text”).
There is no exact same functionality you want.
But you can create BsonDocument from json for query:
var jsonQuery = "{ x : 3, y : 'abc' }"; BsonDocument doc = MongoDB.Bson.Serialization .BsonSerializer.Deserialize<BsonDocument>(jsonQuery);
And after that you can create query from BsonDocument:
var query = new QueryComplete(doc); // or probably Query.Wrap(doc);
The same you can do for the sort expression:
var jsonOrder = "{ x : 1 }"; BsonDocument orderDoc = BsonSerializer.Deserialize<BsonDocument>(jsonQuery); var sortExpr = new SortByWrapper(orderDoc);
Also you can create extension method for the MongoCollection like this:
public static List<T> GetItems<T>(this MongoCollection collection, string queryString, string orderString) where T : class { var queryDoc = BsonSerializer.Deserialize<BsonDocument>(queryString); var orderDoc = BsonSerializer.Deserialize<BsonDocument>(orderString); //as of version 1.8 you should use MongoDB.Driver.QueryDocument instead (thanks to @Erik Hunter) var query = new QueryComplete(queryDoc); var order = new SortByWrapper(orderDoc); var cursor = collection.FindAs<T>(query); cursor.SetSortOrder(order); return cursor.ToList(); }
I didn't test the code above. Will do it later if need.
Update:
Just tested the code above, it's working!
You can use it like this:
var server = MongoServer.Create("mongodb://localhost:27020"); var collection= server.GetDatabase("examples").GetCollection("SO"); var items = collection.GetItems<DocType>("{ x : 3, y : 'abc' }", "{ x : 1 }");
The QueryComplete class seems to have been deprecated. Use MongoDB.Driver.QueryDocument
instead. As below:
BsonDocument document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>("{ name : value }"); QueryDocument queryDoc = new QueryDocument(document); MongoCursor toReturn = collection.Find(queryDoc);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With