[CSHARP-1513] How to run query specified below using c# driver Created: 15/Dec/15  Updated: 05/Apr/19  Resolved: 04/Jan/16

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

Type: Task Priority: Blocker - P1
Reporter: Nikhil Salunkhe Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File C# Source Code.txt     PNG File Mongo Error.png     PNG File image.png     PNG File image.png    
Issue Links:
Duplicate
duplicates CSHARP-1502 db.RunCommand() does work for "find" ... Closed

 Description   

I am running following query in RoboMongo which seems to work fine.
db.runCommand(
{"find":"Equity","filter":{"Ticker": "IBM US Equity"}}
)

I want to run same query using C# driver version - 1.10.1.73
I see following error while running query:
Command 'find' failed: Failed to parse: filter: "'Ticker': 'IBM US Equity'". 'filter' field must be of BSON type Object. (response:

{ "waitedMS" : NumberLong(0), "ok" : 0.0, "errmsg" : "Failed to parse: filter: \"'Ticker': 'IBM US Equity'\". 'filter' field must be of BSON type Object.", "code" : 9 }

)



 Comments   
Comment by Nikhil Salunkhe [ 16/Dec/15 ]

Out of interest is there a method/call inside .NET driver where i can pass
Mongo query as string and get result back.
So far I seen that C# driver is strongly types.

e.g
RunCommand(string(MongoQuery))

Thank you in advance.

Nikhil

On Tue, Dec 15, 2015 at 11:07 PM, Nikhil Salunke <nikhilsalunkhe@gmail.com>

Comment by Nikhil Salunkhe [ 16/Dec/15 ]

Great!.

This works as expected.

Reason we are trying to call find command the way it is called because we
have database management solution TimeScape
<http://www.xenomorph.com/solutions/timescape-edm/> which manages financial
data.
Data store for TimeScape product could be any kind of database such SQL or
Mongo or any proprietary database.
We are passing Mongo DB queries from TimeScape product API which in turns
goes TimeScape product query engine and then talks to Mongo C# api
underneath and returns result as shon below.

Thank you, Robert.

[image: Inline image 1]

Regards,
Nikhil

On Tue, Dec 15, 2015 at 10:26 PM, Robert Stam (JIRA) <jira@mongodb.org>

Comment by Robert Stam [ 16/Dec/15 ]

I can't quite read your code to create the command, but it looks like you are setting the filter field to a string instead of a query document.

It should look like this:

var command = new CommandDocument
{
    { "find", "Equity" },
    { "filter", new BsonDocument { { "Ticker", "IBM US Equity" } }
};

Also, you wouldn't normally call the find command yourself. Just use the Find methods on MongoCollection and let the driver do the find using whatever protocol is appropriate for the version of the server you are connected to.

The API for version 2.x of the driver is slightly different.

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