Good question. I will look into making AddToSetEach work more
seamlessly with custom C# types. I've created a JIRA case for this:
http://jira.mongodb.org/browse/CSHARP-111
In the meantime, here's a bit of a hack you can use, replace your call
to:
obj.ToBsonDocument()
with
// where FooBar is the class of your inner objects
BsonSerializer.Deserialize<BsonDocument>(obj.ToBson<FooBar>())
The serializer decides that a "_t" discriminator is needed when the
actual type being serialized is different from the "nominal" type. In
your call to ToBsonDocument the nominal type is object but the actual
type is FooBar In the second form we are removing the need for a
discriminator by calling ToBson<FooBar>, which sets the nominal type
to FooBar.
I probably should add an overload of ToBsonDocument that looks like
this:
obj.ToBsonDocument<FooBar>()
which would do the same thing internally.
And of course enhancing AddToSetEach is also a great idea.
Thanks for the feedback.