Search code examples
c#mongodbmongodb-.net-driver

MongoDB C# Driver: $where with javascript function


Using the MongoDB C# driver (http://mongodb.github.io/mongo-csharp-driver), how can I create a query which uses $where with a JavaScript function?

Here's an example of the query I'm trying to create if using a GUI like MongoVUE:

{ $where: 'function() { return true }' }

I'm aware that I can use the legacy driver to do this, but wondered if there's a way using the new API only.


Solution

  • We don't have a builder helper for it because it's not something you should normally use. When you need it, you can drop down and build it out of a BsonDocument.

    new BsonDocument("$where", new BsonJavaScript("function() { return true; }"));