1

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} )

1 Answer 1

2

FindAs returns MongoCursor which has SetFileds method. This will return one row with data asked but will not return object but rather Enumeration with one row:

_collection.FindAs(typeof(DictionaryDocument), Query.Exists(key)).SetFields(Fields.Include(key)).SetLimit(1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.