C# 2.0 Driver Searching a Text Index

43 views
Skip to first unread message

Dan Dewar

unread,
Apr 29, 2015, 10:50:01 PM4/29/15
to mongod...@googlegroups.com
Hello,

I was curious if anyone has an example of how to search in a text index with the new 2.0 C# driver. 

Here is the command I am running in mongo (which returns results using the index) that I would like to replicate in the c# driver: 

db.Product.find({ $text: {$search: "coffee"}})

I have tried using various techniques but cannot get the driver to work.

Here is my latest attempt using the .find command: 

var Products = await Context.Products.Find("{ \"$text\": {\"$search\", \"coffee\"}").ToListAsync();

Any advice would be great!

Thanks,
Dan

Craig Wilson

unread,
Apr 30, 2015, 12:18:17 AM4/30/15
to mongod...@googlegroups.com
Hi Dan, 

What you are doing is accurate, except your json string is improperly formatted. You have a  , after $search instead of a :. However, an easier way would be to use the builders (http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/definitions/#filters).

...Find(Builders<Product>.Filter.Text("coffee")).ToListAsync();

Craig

Dan Dewar

unread,
Apr 30, 2015, 11:13:24 AM4/30/15
to mongod...@googlegroups.com
Worked Perfectly.

Thanks!
Reply all
Reply to author
Forward
0 new messages