How to write following MongoDB query in C# Driver?

Posted by user3043457 on Stack Overflow See other posts from Stack Overflow or by user3043457
Published on 2014-06-10T15:04:50Z Indexed on 2014/06/11 9:25 UTC
Read the original article Hit count: 208

Filed under:
|

I wrote the exact query I need in Mongo console, but I'm having trouble rewriting it in C# driver. Here's a sample of the document, it's simple dictionary:

{
        "_id" : ObjectId("539716bc101c588f941e2c27"),
        "_t" : "DictionaryDocument",
        "CsvSeparator" : ",",
        "SelectedAccounts" : "0",
...
}

Here's the query:

db.settings.find({"SelectedAccounts" :{$exists:true}},{"SelectedAccounts":1, "_id":0} )

Now, I got the first part, Find with exists working, but how to write the second parameter in C# driver? I'd just like a single string as a result, not entire document.

Here's C# code I got so far:

_collection.FindOneAs(typeof(DictionaryDocument), Query.Exists(key));

key in this case is "SelectedAccounts". I'd like the query to filter and return only the data I need, I don't want to return all the results and search on the C# side.

EDIT: I wouldn't mind if _id was passed back, but I don't need it. So only this part would work if it could be converted in C#:

db.settings.find({"SelectedAccounts" :{$exists:true}},{"SelectedAccounts":1} )

© Stack Overflow or respective owner

Related posts about c#

Related posts about mongodb