2

I am getting values based on formId in MongoDB using the following query:

db.FormData.find({FormID:new BinData(3,"nMVvjlcHGU68zFBBCeYdsg==")})

It works in the MongoDB command prompt only. How can I write the query for getting the values from MongoDB based on a form ID in C#?

  • In the past I used the MongoDB.Driver library to interact with MongoDB collections. It's a flexible and easy to use C# library and widely used. Maybe it helps you to achieve what you're looking for. docs.mongodb.com/ecosystem/drivers/csharp – Dražen Janjiček Nov 24 '16 at 9:41
  • Were you able to successfully use the code from my answer? Or do you need any more help? – Dmytro Shevchenko Nov 25 '16 at 10:36
1

I have described how to work with MongoDB in Sitecore in this answer.

Querying

Applying it for your case, the code will be similar to this:

MongoDbDriver driver = MongoDbDriver.FromConnectionString("analytics");
MongoDbCollection collection = driver["FormData"];

Guid formId = new Guid("insert-some-guid-here");

MongoCursor<FormData> cursor =
    collection.FindAs<FormData>(
        Query<FormData>.EQ(_ => _.FormID, formId));

IList<FormData> results = cursor.ToList();

Note that I'm using a System.Guid object to define the form ID. You should not use binary IDs in C# code.

Data mapping

The C# driver can automatically map data to strongly-typed models. I assume that the collection you're using in your question contains WFFM data. If that is the case, all you'll need to do is use WFFM's data class FormData. It's already registered in the driver, so just reference Sitecore.WFFM.Abstractions.dll in your project and add the following directive in the beginning of your C# file:

using Sitecore.WFFM.Abstractions.Analytics;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.