Dheeraj Palagiri
Dheeraj Palagiri

Reputation: 1879

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.

Upvotes: 6

Views: 14971

Answers (1)

i3arnon
i3arnon

Reputation: 116596

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

Upvotes: 13

Related Questions