List of strings into Bson Array

11,636

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));
}
Share:
11,636
Dheeraj Palagiri
Author by

Dheeraj Palagiri

An enthusiastic sitecore developer.

Updated on June 30, 2022

Comments

  • Dheeraj Palagiri
    Dheeraj Palagiri almost 2 years

    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.