[CSHARP-1514] Can you pass any mongo query as string to C# driver and get result back? Created: 16/Dec/15  Updated: 05/Apr/19  Resolved: 22/Dec/16

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

Type: Task Priority: Major - P3
Reporter: Nikhil Salunkhe Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows,

Mongo:
/* 1 */
{
"version" : "3.2.0",
"gitVersion" : "45d947729a0315accb6d4f15a6b06be6d9c19fe7",
"targetMinOS" : "Windows 7/Windows Server 2008 R2",
"modules" : [],
"allocator" : "tcmalloc",
"javascriptEngine" : "mozjs",
"sysInfo" : "deprecated",
"versionArray" : [
3,
2,
0,
0
],
"openssl" :

{ "running" : "OpenSSL 1.0.1p-fips 9 Jul 2015", "compiled" : "OpenSSL 1.0.1p-fips 9 Jul 2015" }

,
"buildEnvironment" :

{ "distmod" : "2008plus-ssl", "distarch" : "x86_64", "cc" : "cl: Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64", "ccflags" : "/nologo /EHsc /W3 /wd4355 /wd4800 /wd4267 /wd4244 /wd4290 /wd4068 /wd4351 /we4013 /we4099 /we4930 /Z7 /errorReport:none /MD /O2 /Oy- /Gw /Gy /Zc:inline", "cxx" : "cl: Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64", "cxxflags" : "/TP", "linkflags" : "/nologo /DEBUG /INCREMENTAL:NO /LARGEADDRESSAWARE /OPT:REF", "target_arch" : "x86_64", "target_os" : "windows" }

,
"bits" : 64,
"debug" : false,
"maxBsonObjectSize" : 1.67772e+007,
"storageEngines" : [
"devnull",
"ephemeralForTest",
"mmapv1",
"wiredTiger"
],
"ok" : 1.0000000000000000
}


Attachments: PNG File BuildInfo command in TimeScape.png     PNG File Find command TimeScape.png    

 Description   

I am using Mongo C# driver to query data. Executing command from shell/robomongo seems easy to use but trying to execute same command from C# seems little difficult.

e.g. In Shell, I am executing following command:
db.runCommand(
{
"find":"Equity",
"filter":

{"Ticker":"AAPL US Equity", "Ask":101.2600000000000100}

}
)

Same command is C# driver as follow:
var client = new MongoClient();
var database = client.GetServer().GetDatabase("MONGOXDB");
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("find", "Equity");

BsonElement bElement1 = new BsonElement("Ticker", "AAPL US Equity");

BsonElement bElement2 = new BsonElement("Ask"(BsonValue)101.2600000000000100);

dictionary.Add("filter", new BsonDocument

{ bElement1, bElement2 }

);

var command = new CommandDocument

{ dictionary }

;

CommandResult result = database.RunCommand(command);

BsonDocument bdocument = result.Response;

---------------------------------------
Now question is there way of passing Mongo query as string in C# and then get result back.



 Comments   
Comment by Craig Wilson [ 16/Dec/15 ]

Couple of things:

1. You'll be restricted to using mongodb 3.2 or greater. The find command was introduced in mongodb 3.2.
2. You'll need to manually handle dealing with cursors. Once you exhaust the first batch, you'll need to run the getMore command manually with the correct cursor id. Most importantly, once you are done with the results you want, you will need kill the cursor using the killCursor command or else you will leave unused resources open on the server.

Comment by Nikhil Salunkhe [ 16/Dec/15 ]

Thanks, Craig.

In future, I will post question on the google group that you mentioned. I
was not aware of this group till now so thank you for that.

Reason for running find command manually:

We have data management at Xenomorph software Inc to manage financial data.
We are doing POC work to see if our product can sit on top of Mongo DB
database and expose all data in Mongo DB. This way all data in MongoDb is
available through out product API.
In initial stages, we are looking to pass Mongo query from our product API
and underneath it talks to Mongo C# driver and returns result to product.

So far using runCommand we have managed to run simple query such as
buildInfo or find:"collectionname".But when it gets to complex queries
then you have to build command correctly in Mongo C# driver i.e. build
bason document etc and then pass to Command.

Please see attached screenshot for what are trying to do and may be you
suggest best approach for using Mongo C# driver.

Regards,
Nikhil

On Wed, Dec 16, 2015 at 2:42 PM, Craig Wilson (JIRA) <jira@mongodb.org>

Comment by Craig Wilson [ 16/Dec/15 ]

My apologies, you are using version 1.10 of the driver and not 2.2. You cannot do what you are asking with a string, although it is simple enough to parse by using BsonDocument.Parse(json). You can then pass that anywhere you'd like.

Craig

Comment by Craig Wilson [ 16/Dec/15 ]

Hi Nihkil,

The proper place to ask questions like this is in the user group. We generally reserve jira tickets for features and bug requests.

As has been answered in two previous tickets, CSHARP-1502 AND CSHARP-1513, there isn't a good reason to be running the find command manually. If you have one, please let us know. You can specify a filter using a json string: collection.Find("

{'Ticker':'AAPL US Equity', 'Ask':101.2600000000000100}

").ToList() will do what you want it do to. If you haven't read the documenation, you can find it here.

Craig

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