PUG
PUG

Reputation: 4472

Convert BsonString To BsonArray

I am trying to convert a BsonString to BsonArray as follows:

BsonString str = "[{ \"_id\" : \"domain\", \"nm\" : \"Domain\", \"value\" : [\"test.com\"], \"dt\" : ISODate(\"2014-08-04T04:00:00Z\") }]\"";

BsonArray array = str.AsBsonArray;

And I get exception:

Unable to cast object of type 'MongoDB.Bson.BsonString' to type 'MongoDB.Bson.BsonArray'.

Any ideas on how can I parse a string to a BsonArray object. Without doing it by hand?

Upvotes: 0

Views: 2287

Answers (1)

PUG
PUG

Reputation: 4472

Some mucking around, and found the solution

BsonArray array = BsonSerializer.Deserialize<BsonArray>(str);

Upvotes: 4

Related Questions