Hoping for a lifeline, newbie disappointed and ready to entirely give up on MongoDB

74 views
Skip to first unread message

Jeff Ovadya

unread,
Apr 7, 2016, 11:28:11 PM4/7/16
to mongodb-user
A .Net solution, developed in Visual Studio 2008, .Net v. 2.0, cannot be upgraded at this time.

Dependencies include:
1) Sql Server 2012
2)  CLR SQL assemblies developed in Visual Studio 2012, .Net v. 4.5, used to write data to files, run as Jobs (SQL Server version of crons) 
3)  MongoMigration cron application reads files created by CLR SQL assemblies and writes them to MongoDB before deleting them

Need to read data from MongoDB. Options include:
1)  My preferred method:  Read data SYNCHRONOUSLY (not sure how) from CLR SQL assemblies developed in Visual Studio 2012, and integrate with data from Sql Server
2)  Not my obvious choice:  Read data from C# code on IIS 7.5 web server, developed in Visual Studio 2008 (.Net v.2.0), and integrate with records retrieved from Sql Server sproc

Problems: 
1)  NuGet will not reference MongoDB drivers in CLR SQL assemblies in Visual Studio 2012
2)  I find legacy drivers documentation very confusing
3)  I prefer to retrieve all records as a JSON array in a string variable, and without MongoDB's "_id" field, as it will need to be removed.
4)  Note from the attached image, a problem exists with the two await statements. I wonder if changing to a synchronous read will resolve the issue.

Context:
The database called SR's collection called EmailUs stores webmail from users. Stored data include messages from users ands replies from our staff, so in addition to email-message date and sender's email address, a composite key is included to group each email in the thread, composed of the sender's email address and a 64-bit integer (representing milliseconds since Jan 1 1970) taken from their first email in the grouping. Thus the system should be able to retrieve all emails belonging to a specific thread, ordered by date sent and, marked as either originating from the user or from staff.

The retrieved data is to be presented to users in a web page, and the composite key enables them to reply to staff responses as dialogue ensues.

Code Process:
The code iterates [JSON-formatted] files retrieved from MongoDB collection and appends them to a StringBuilder object and joins them with commas to create a properly-formatted JSON array object, which will be sent to the client browser after adding records from Sql Server.

Code:
public static string ViewThread(SqlString Email, SqlString Ref) 
{
#region Instantiations and Initializations 
string email = Email.ToString();
string refId = Ref.ToString();
long ref64 = long.Parse(refId);
string mongoString = null; //contains data retrieved from MongoDB
string sqlString = null; //contains dataz retrieved from Sql Server
string returnString = string.Empty; //to contain JSON-formatted data integrated from both data sources
string myDB = "SR";
string myColl = "EmailUs";
StringBuilder sbRecords = new StringBuilder();
MongoClient client = new MongoClient("mongodb://localhost");
IMongoDatabase database = client.GetDatabase(myDB);
IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>(myColl);
FilterDefinition<BsonDocument> filter = Builders<BsonDocument>.Filter.Eq("threadDate", ref64) & Builders<BsonDocument>.Filter.Eq("threadEmail", email);
IEnumerable<BsonDocument> batch = null;
int count = 0;
#endregion

#region Read Data from MongoDB 
count = 0;
using (IAsyncCursor<BsonDocument> cursor = await collection.FindAsync(filter))
{
    while (await cursor.MoveNextAsync())
    {
        batch = cursor.Current;
        mongoString = "";
        foreach (var document in batch)
        {
            sbRecords.Append(document);
            sbRecords.Append(mongoString);
            mongoString = ",";
        }

    }
}
mongoString = sbRecords.ToString().Length > 0 ? sbRecords.ToString() : string.Empty;
#endregion

... // code continues...






MongoDB_Read.jpg

Kevin Adistambha

unread,
Apr 20, 2016, 3:38:47 AM4/20/16
to mongodb-user

Hi Jeff,

The database called SR’s collection called EmailUs stores webmail from users. Stored data include messages from users ands replies from our staff, so in addition to email-message date and sender’s email address, a composite key is included to group each email in the thread, composed of the sender’s email address and a 64-bit integer (representing milliseconds since Jan 1 1970) taken from their first email in the grouping. Thus the system should be able to retrieve all emails belonging to a specific thread, ordered by date sent and, marked as either originating from the user or from staff.

Is my understanding correct that you wanted to query data from a MongoDB server using a SQL Server stored procedure written in C#?

1) My preferred method: Read data SYNCHRONOUSLY (not sure how) from CLR SQL assemblies developed in Visual Studio 2012, and integrate with data from Sql Server

You may find some examples in the Quick Tour page under the heading “Query the Collection”. There are examples for querying a collection using synchronous and asynchronous methods.

2) Not my obvious choice: Read data from C# code on IIS 7.5 web server, developed in Visual Studio 2008 (.Net v.2.0), and integrate with records retrieved from Sql Server sproc

Please note that .NET 2.0 is not supported by MongoDB C# driver. The lowest .NET version supported by the C# driver is .NET 3.5, and the latest C# driver requires .NET version 4.5.

An alternative solution could be using a REST-style API layer in front of the MongoDB server, and perform queries on the MongoDB database using REST instead of using a driver. Examples of REST interfaces can be found in the HTTP Interface page.

Regarding your issues:

1) NuGet will not reference MongoDB drivers in CLR SQL assemblies in Visual Studio 2012

I believe Nuget is not supported for CLR SQL assembly. You would have to add the dll files manually in the project.

2) I find legacy drivers documentation very confusing

Please let us know which page you find confusing, as there may be room for improvements in the documentation.

3) I prefer to retrieve all records as a JSON array in a string variable, and without MongoDB’s “id” field, as it will need to be removed.

You can find an example in the Quick Tour page, under the section labeled Projecting Fields . The example describes how to exclude the _id field from query result.

4) Note from the attached image, a problem exists with the two await statements. I wonder if changing to a synchronous read will resolve the issue.

As mentioned in the await reference page: “The asynchronous method in which await is used must be modified by the async keyword”. I.e., the ViewThread method must be changed to public static async <return type> ViewThread(...). You can find some synchronous and asynchronous query examples in the Quick Tour page that you can follow.

You may find these links useful for your case:

If you have time, I would recommend you to enroll in the M101N: MongoDB for .NET Developers online course for an in-depth introduction to MongoDB and the C# driver. It is a free course, and the next one will start at May 24 2016.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages