Created
December 22, 2010 10:54
-
-
Save bcambel/751375 to your computer and use it in GitHub Desktop.
MongoDB C# driver save fails because of duplication exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using MongoDB.Bson; | |
| using MongoDB.Bson.DefaultSerializer; | |
| using MongoDB.Bson.IO; | |
| using MongoDB.Bson.Serialization; | |
| using MongoDB.Driver; | |
| using MongoDB.Driver.Builders; | |
| using NUnit.Framework; | |
| namespace ContentFramework.UnitTests.Integration.MongoDb | |
| { | |
| public class MyDocument | |
| { | |
| public string CompelId { get; set; } | |
| public string ContentDbId { get; set; } | |
| public string DbId { get; set; } | |
| public string Factory { get; set; } | |
| public string Locale { get; set; } | |
| public string ContentType { get; set; } | |
| public string[] Keys { get; set; } | |
| public string ParentId { get; set; } | |
| public override string ToString() | |
| { | |
| return String.Format("[CompelId:{0},Locale={1},CType={2};Keys:{3};ContentDbId={4}]", | |
| CompelId, Locale, ContentType, String.Join("-", Keys), ContentDbId | |
| ); | |
| } | |
| } | |
| [TestFixture] | |
| public class MongoDbUpdate | |
| { | |
| [Test] | |
| public void insert_and_update_document_using_save() | |
| { | |
| BsonClassMap.UnregisterClassMap(typeof(MyDocument)); | |
| BsonClassMap.RegisterClassMap<MyDocument>(cm => | |
| { | |
| cm.AutoMap(); | |
| cm.MapIdProperty(x => x.DbId) | |
| .SetIdGenerator(new MyCustomIdGenerator()) | |
| .SetSerializer(new MyCustomSerializer()); | |
| }); | |
| var server = MongoServer.Create("mongodb://localhost/?safe=true"); | |
| var database = server["test"]; | |
| var documents = database.GetCollection<MyDocument>("document"); | |
| MyDocument document = new MyDocument(); | |
| document.CompelId = "ssadfasdfasf"; | |
| document.ContentDbId = "1"; | |
| document.Factory = "testfactory"; | |
| documents.Save(document); | |
| Console.WriteLine(document.DbId); | |
| document.Factory = "testfactory2"; | |
| SafeModeResult safeModeResult = documents.Save(document, SafeMode.True); | |
| Assert.IsNotEmpty(document.DbId,document.DbId); | |
| Console.WriteLine(document.DbId); | |
| MyDocument doc = documents.FindOne(Query.EQ("_id", document.DbId)); | |
| Assert.AreEqual("testfactory2",doc.Factory); | |
| } | |
| [Test] | |
| public void insert_and_update_document_using_update_query() | |
| { | |
| BsonClassMap.UnregisterClassMap(typeof(MyDocument)); | |
| BsonClassMap.RegisterClassMap<MyDocument>(cm => | |
| { | |
| cm.AutoMap(); | |
| cm.MapIdProperty(x => x.DbId) | |
| .SetIdGenerator(new MyCustomIdGenerator()) | |
| .SetSerializer(new MyCustomSerializer()); | |
| }); | |
| var server = MongoServer.Create("mongodb://localhost/?safe=true"); | |
| var database = server["test"]; | |
| var documents = database.GetCollection<MyDocument>("document"); | |
| MyDocument document = new MyDocument(); | |
| document.CompelId = "ssadfasdfasf"; | |
| document.ContentDbId = "1"; | |
| document.Factory = "testfactory"; | |
| documents.Save(document); | |
| document.Factory = "testfactory2"; | |
| Assert.IsNotEmpty(document.DbId, document.DbId); | |
| Console.WriteLine(document.DbId); | |
| documents.Update(Query.EQ("_id", new ObjectId(document.DbId)), Update.Set("Factory", "testfactory2"), SafeMode.True); | |
| MyDocument doc = documents.FindOne(Query.EQ("_id", new ObjectId(document.DbId))); | |
| Assert.AreEqual("testfactory2", doc.Factory); | |
| } | |
| public class MyCustomIdGenerator : IIdGenerator | |
| { | |
| public object GenerateId() | |
| { | |
| return ObjectId.GenerateNewId().ToString(); | |
| } | |
| public bool IsEmpty( | |
| object id | |
| ) | |
| { | |
| return string.IsNullOrEmpty((string)id); | |
| } | |
| } | |
| public class MyCustomSerializer : BsonBaseSerializer | |
| { | |
| public override object Deserialize( | |
| BsonReader bsonReader, | |
| Type nominalType, | |
| IBsonSerializationOptions options | |
| ) | |
| { | |
| // read an ObjectId from the database but convert it to a string before returning it | |
| int timestamp, machine, increment; | |
| short pid; | |
| bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment); | |
| return new ObjectId(timestamp, machine, pid, increment).ToString(); | |
| } | |
| public override void Serialize( | |
| BsonWriter bsonWriter, | |
| Type nominalType, | |
| object value, | |
| IBsonSerializationOptions options | |
| ) | |
| { | |
| // convert the string to an ObjectId before writing it to the database | |
| var objectId = new ObjectId((string)value); | |
| bsonWriter.WriteObjectId(objectId.Timestamp, objectId.Machine, objectId.Pid, objectId.Increment); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment