Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do a text query with the mongodb c# driver

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

like image 489
pm100 Avatar asked May 25 '11 06:05

pm100


People also ask

Does MongoDB do text search?

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.

Can we write queries in MongoDB?

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.

How does MongoDB text search work?

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.

How do you create a text index MongoDB?

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”).


2 Answers

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 }"); 
like image 55
Andrew Orsich Avatar answered Sep 24 '22 02:09

Andrew Orsich


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); 
like image 29
Erik Hunter Avatar answered Sep 26 '22 02:09

Erik Hunter