Hello Robert,
I've pasted my code below... I read through the serialization tutorial
some more which was helpful.
I'm getting an exception "Id cannot be null";
users.Insert(user);
I've also tried using an Id element in my JSON input string which also
gives me an exception.
When I comment out the [BsonId] attribute and "Id" field, I get an
"Unexpected element: _id" at
user = users.FindOne();
So, if you could help me understand defining an Id element? I would
have assumed this field is used as the document id inside Mongo and
not applicable or should be exposed to application. Does every mapped
object in Bson need to have an Id property? Does it have to have the
Id namespace or just the [BsonId] attribute?
Secondly, can BSON/Mongo fill in id of document or does that need to
be done programatically with a NewGuid() type call?
thanks
-sepehr
ReadString cannot be called when BsonType is: ObjectId
class Program
{
static void Main(string[] args)
{
string input = @"{""name"":""user1"",""pass"":""pass1""}";
string mdbConfig = @"mongodb://localhost";
MongoServer mongoServer = MongoServer.Create(mdbConfig);
MongoDatabase db = mongoServer.GetDatabase(@"netopeso");
MongoCollection<usersClass> users =
db.GetCollection<usersClass>("users");
usersClass user = BsonSerializer.Deserialize<usersClass>(input);
users.Insert(user);
user = users.FindOne();
System.Console.WriteLine(
user.name + " " + user.pass + " " +
user.ToJson<usersClass>());
System.Console.ReadLine();
}
}
[Serializable]
public class usersClass
{
[BsonId]
public string Id;
public string name;
public string pass;
}