Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of strings into Bson Array

I am using MongoDb 2.4.9 version. when i try to convert list of strings in to Bson Array i end up in below error:

BsonArray bArray = new BsonArray();
foreach (var term in termMonitorIds)
{
    bArray.Add(term.ToBson());
}

Server Error in '/' Application.

A String value cannot be written to the root level of a BSON document.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: A String value cannot be written to the root level of a BSON document.

EDIT

When i use that in the LINQ Query below:

entities =
                    (from e in this.collection.AsQueryable<SocialRecord>()
                     where sources.Contains(e.SocialType) && (e.DateCreated >= fr) && (e.DateCreated <= to) && e.TermMonitorIds.Any(X=> bArray.Contains(X)) && e.IsExactMatch == isInstagramExactMatch
                     select e)
                    .Take(5000)
                    .ToList();

results in Value cannot be null. Parameter name: name . This error message only happens when i added bArray to the Query.

like image 391
Dheeraj Palagiri Avatar asked Jan 10 '23 09:01

Dheeraj Palagiri


1 Answers

You don't need to invoke ToBson. There's already an implicit conversion from string to BsonValue. Using ToBson actually generates bson, which probably isn't your goal:

BsonArray bArray = new BsonArray();
foreach (var term in termMonitorIds)
{
    bArray.Add(term));
}
like image 113
i3arnon Avatar answered Jan 19 '23 07:01

i3arnon