本文整理汇总了C#中MongoDB.Driver.MongoInsertOptions类的典型用法代码示例。如果您正苦于以下问题:C# MongoInsertOptions类的具体用法?C# MongoInsertOptions怎么用?C# MongoInsertOptions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MongoInsertOptions类属于MongoDB.Driver命名空间,在下文中一共展示了MongoInsertOptions类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign
public void TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign()
{
var server = Configuration.TestServer;
var database = Configuration.TestDatabase;
var collection = Configuration.TestCollection;
collection.Drop();
var document = new BsonDocument
{
{ "_id", 1 },
{ "v", new BsonDocument("$x", 1) } // server doesn't allow "$" at top level
};
var insertOptions = new MongoInsertOptions { CheckElementNames = false };
collection.Insert(document, insertOptions);
document = collection.FindOne();
Assert.AreEqual(1, document["v"]["$x"].AsInt32);
document["v"]["$x"] = 2;
var query = Query.EQ("_id", 1);
var update = Update.Replace(document);
var updateOptions = new MongoUpdateOptions { CheckElementNames = false };
collection.Update(query, update, updateOptions);
document = collection.FindOne();
Assert.AreEqual(2, document["v"]["$x"].AsInt32);
document["v"]["$x"] = 3;
collection.Save(document, insertOptions);
document = collection.FindOne();
Assert.AreEqual(3, document["v"]["$x"].AsInt32);
}
开发者ID:CloudMetal,项目名称:mongo-csharp-driver,代码行数:30,代码来源:CSharp358Tests.cs
示例2: TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign
public void TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign()
{
// starting with version 2.5.2 the server got stricter about dollars in element names
// so this test should only be run when testing against older servers
var server = Configuration.TestServer;
if (server.BuildInfo.Version < new Version(2, 6, 0))
{
var database = Configuration.TestDatabase;
var collection = Configuration.TestCollection;
collection.Drop();
var document = new BsonDocument
{
{ "_id", 1 },
{ "v", new BsonDocument("$x", 1) } // server doesn't allow "$" at top level
};
var insertOptions = new MongoInsertOptions { CheckElementNames = false };
collection.Insert(document, insertOptions);
document = collection.FindOne();
Assert.AreEqual(1, document["v"]["$x"].AsInt32);
document["v"]["$x"] = 2;
var query = Query.EQ("_id", 1);
var update = Update.Replace(document);
var updateOptions = new MongoUpdateOptions { CheckElementNames = false };
collection.Update(query, update, updateOptions);
document = collection.FindOne();
Assert.AreEqual(2, document["v"]["$x"].AsInt32);
document["v"]["$x"] = 3;
collection.Save(document, insertOptions);
document = collection.FindOne();
Assert.AreEqual(3, document["v"]["$x"].AsInt32);
}
}
开发者ID:Bogdan0x400,项目名称:mongo-csharp-driver,代码行数:35,代码来源:CSharp358Tests.cs
示例3: TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign
public void TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign()
{
var server = MongoServer.Create("mongodb://localhost/?safe=true;slaveOk=true");
var database = server["onlinetests"];
var collection = database["test"];
collection.Drop();
var document = new BsonDocument
{
{ "_id", 1 },
{ "v", new BsonDocument("$x", 1) } // server doesn't allow "$" at top level
};
var insertOptions = new MongoInsertOptions { CheckElementNames = false };
collection.Insert(document, insertOptions);
document = collection.FindOne();
Assert.AreEqual(1, document["v"].AsBsonDocument["$x"].AsInt32);
document["v"].AsBsonDocument["$x"] = 2;
var query = Query.EQ("_id", 1);
var update = Update.Replace(document);
var updateOptions = new MongoUpdateOptions { CheckElementNames = false };
collection.Update(query, update, updateOptions);
document = collection.FindOne();
Assert.AreEqual(2, document["v"].AsBsonDocument["$x"].AsInt32);
document["v"].AsBsonDocument["$x"] = 3;
collection.Save(document, insertOptions);
document = collection.FindOne();
Assert.AreEqual(3, document["v"].AsBsonDocument["$x"].AsInt32);
}
开发者ID:kamaradclimber,项目名称:mongo-csharp-driver,代码行数:30,代码来源:CSharp358Tests.cs
示例4: TestInsertBatchMultipleBatchesWriteConcernEnabledContinueOnErrorTrue
public void TestInsertBatchMultipleBatchesWriteConcernEnabledContinueOnErrorTrue()
{
var collectionName = Configuration.TestCollection.Name;
var collectionSettings = new MongoCollectionSettings { WriteConcern = WriteConcern.Acknowledged };
var collection = Configuration.TestDatabase.GetCollection<BsonDocument>(collectionName, collectionSettings);
if (collection.Exists()) { collection.Drop(); }
using (Configuration.TestDatabase.RequestStart())
{
var maxMessageLength = Configuration.TestServer.RequestConnection.ServerInstance.MaxMessageLength;
var filler = new string('x', maxMessageLength / 3); // after overhead results in two documents per sub-batch
var documents = new BsonDocument[]
{
// first sub-batch
new BsonDocument { { "_id", 1 }, { "filler", filler } },
new BsonDocument { { "_id", 2 }, { "filler", filler } },
// second sub-batch
new BsonDocument { { "_id", 3 }, { "filler", filler } },
new BsonDocument { { "_id", 3 }, { "filler", filler } }, // duplicate _id error
// third sub-batch
new BsonDocument { { "_id", 4 }, { "filler", filler } },
new BsonDocument { { "_id", 5 }, { "filler", filler } },
};
try
{
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
collection.InsertBatch(documents, options);
}
catch (WriteConcernException ex)
{
var results = (IEnumerable<WriteConcernResult>)ex.Data["results"];
Assert.AreEqual(3, results.Count());
var result1 = results.ElementAt(0);
Assert.AreEqual(false, result1.HasLastErrorMessage);
var result2 = results.ElementAt(1);
Assert.AreEqual(true, result2.HasLastErrorMessage);
var result3 = results.ElementAt(2);
Assert.AreEqual(false, result3.HasLastErrorMessage);
}
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 1)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 2)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 3)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 4)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 5)));
}
}
开发者ID:rjvranjan80,项目名称:mongo-csharp-driver,代码行数:52,代码来源:MongoCollectionTests.cs
示例5: TestInsertBatchMultipleBatchesWriteConcernDisabledContinueOnErrorTrue
public void TestInsertBatchMultipleBatchesWriteConcernDisabledContinueOnErrorTrue()
{
var collectionName = Configuration.TestCollection.Name;
var collectionSettings = new MongoCollectionSettings { WriteConcern = WriteConcern.Unacknowledged };
var collection = Configuration.TestDatabase.GetCollection<BsonDocument>(collectionName, collectionSettings);
if (collection.Exists()) { collection.Drop(); }
using (Configuration.TestDatabase.RequestStart())
{
var maxMessageLength = Configuration.TestServer.RequestConnection.ServerInstance.MaxMessageLength;
var filler = new string('x', maxMessageLength / 3); // after overhead results in two documents per sub-batch
var documents = new BsonDocument[]
{
// first sub-batch
new BsonDocument { { "_id", 1 }, { "filler", filler } },
new BsonDocument { { "_id", 2 }, { "filler", filler } },
// second sub-batch
new BsonDocument { { "_id", 3 }, { "filler", filler } },
new BsonDocument { { "_id", 3 }, { "filler", filler } }, // duplicate _id error
// third sub-batch
new BsonDocument { { "_id", 4 }, { "filler", filler } },
new BsonDocument { { "_id", 5 }, { "filler", filler } },
};
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
var result = collection.InsertBatch(documents, options);
Assert.AreEqual(null, result);
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 1)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 2)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 3)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 4)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 5)));
}
}
开发者ID:rjvranjan80,项目名称:mongo-csharp-driver,代码行数:36,代码来源:MongoCollectionTests.cs
示例6: TestInsertBatchMultipleBatchesWriteConcernEnabledContinueOnErrorTrue
public void TestInsertBatchMultipleBatchesWriteConcernEnabledContinueOnErrorTrue()
{
var collectionName = Configuration.TestCollection.Name;
var collectionSettings = new MongoCollectionSettings { WriteConcern = WriteConcern.Acknowledged };
var collection = Configuration.TestDatabase.GetCollection<BsonDocument>(collectionName, collectionSettings);
if (collection.Exists()) { collection.Drop(); }
using (Configuration.TestDatabase.RequestStart())
{
var maxMessageLength = Configuration.TestServer.RequestConnection.ServerInstance.MaxMessageLength;
var filler = new string('x', maxMessageLength / 3); // after overhead results in two documents per sub-batch
var documents = new BsonDocument[]
{
// first sub-batch
new BsonDocument { { "_id", 1 }, { "filler", filler } },
new BsonDocument { { "_id", 2 }, { "filler", filler } },
// second sub-batch
new BsonDocument { { "_id", 3 }, { "filler", filler } },
new BsonDocument { { "_id", 3 }, { "filler", filler } }, // duplicate _id error
// third sub-batch
new BsonDocument { { "_id", 4 }, { "filler", filler } },
new BsonDocument { { "_id", 5 }, { "filler", filler } },
};
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
var exception = Assert.Throws<MongoDuplicateKeyException>(() => { collection.InsertBatch(documents, options); });
var result = exception.WriteConcernResult;
var expectedResult = new ExpectedWriteConcernResult()
{
HasLastErrorMessage = true
};
CheckExpectedResult(expectedResult, result);
var results = ((IEnumerable<WriteConcernResult>)exception.Data["results"]).ToArray();
if (results.Length == 3)
{
Assert.AreEqual(false, results[0].HasLastErrorMessage);
Assert.AreEqual(true, results[1].HasLastErrorMessage);
Assert.AreEqual(false, results[2].HasLastErrorMessage);
}
else
{
// it the opcode was emulated there will just be one synthesized result
Assert.AreEqual(1, results.Length);
Assert.AreEqual(true, results[0].HasLastErrorMessage);
}
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 1)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 2)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 3)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 4)));
Assert.AreEqual(1, collection.Count(Query.EQ("_id", 5)));
}
}
开发者ID:RepoCorp,项目名称:mongo-csharp-driver,代码行数:56,代码来源:MongoCollectionTests.cs
示例7: TestInsertBatchContinueOnError
public void TestInsertBatchContinueOnError()
{
var collection = Configuration.TestCollection;
collection.Drop();
collection.CreateIndex(IndexKeys.Ascending("x"), IndexOptions.SetUnique(true));
var batch = new BsonDocument[]
{
new BsonDocument("x", 1),
new BsonDocument("x", 1), // duplicate
new BsonDocument("x", 2),
new BsonDocument("x", 2), // duplicate
new BsonDocument("x", 3),
new BsonDocument("x", 3) // duplicate
};
// try the batch without ContinueOnError
var exception = Assert.Throws<MongoDuplicateKeyException>(() => collection.InsertBatch(batch));
var result = exception.WriteConcernResult;
var expectedResult = new ExpectedWriteConcernResult
{
HasLastErrorMessage = true
};
CheckExpectedResult(expectedResult, result);
Assert.AreEqual(1, collection.Count());
Assert.AreEqual(1, collection.FindOne()["x"].AsInt32);
// try the batch again with ContinueOnError
if (_server.BuildInfo.Version >= new Version(2, 0, 0))
{
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
exception = Assert.Throws<MongoDuplicateKeyException>(() => collection.InsertBatch(batch, options));
result = exception.WriteConcernResult;
expectedResult = new ExpectedWriteConcernResult
{
HasLastErrorMessage = true
};
CheckExpectedResult(expectedResult, result);
Assert.AreEqual(3, collection.Count());
}
}
开发者ID:RepoCorp,项目名称:mongo-csharp-driver,代码行数:45,代码来源:MongoCollectionTests.cs
示例8: TestCountWithReadPreferenceFromFind
public void TestCountWithReadPreferenceFromFind()
{
_collection.Drop();
var all = LegacyTestConfiguration.Server.Secondaries.Length + 1;
var options = new MongoInsertOptions { WriteConcern = new WriteConcern(w: all) };
_collection.Insert(new BsonDocument("x", 1), options);
_collection.Insert(new BsonDocument("x", 2), options);
var count = _collection.Find(Query.EQ("x", 1)).SetReadPreference(ReadPreference.Secondary).Count();
Assert.AreEqual(1, count);
}
开发者ID:p3p3pp3,项目名称:mongo-csharp-driver-for-tokumx,代码行数:10,代码来源:MongoCollectionTests.cs
示例9: Initialize
private void Initialize()
{
lock (Sync)
{
var database = MongoDatabase.Create(_connectionString);
if (!database.CollectionExists(_collectionName))
{
var options = CollectionOptions
.SetCapped(true)
.SetAutoIndexId(true)
.SetMaxSize(_maxSize);
if (_maxDocuments != int.MaxValue)
options.SetMaxDocuments(_maxDocuments);
database.CreateCollection(_collectionName, options);
}
_collection = database.GetCollection(_collectionName);
_mongoInsertOptions = new MongoInsertOptions { CheckElementNames = false };
}
}
开发者ID:mikalai-silivonik,项目名称:elmah-mongodb,代码行数:22,代码来源:MongoErrorLog.cs
示例10: addNewUser
private void addNewUser()
{
var options = new MongoInsertOptions(names) { SafeMode = SafeMode.True };
BsonDocument name = new BsonDocument {
{ "name", newName }
};
names.Save(name, options);
}
开发者ID:guozanhua,项目名称:Kinect-Tracking-Project,代码行数:8,代码来源:Logon.xaml.cs
示例11: TestInsertBatchContinueOnError
public void TestInsertBatchContinueOnError() {
var collection = database["continueonerror"];
collection.Drop();
collection.CreateIndex(IndexKeys.Ascending("x"), IndexOptions.SetUnique(true));
var batch = new BsonDocument[] {
new BsonDocument("x", 1),
new BsonDocument("x", 1), // duplicate
new BsonDocument("x", 2),
new BsonDocument("x", 2), // duplicate
new BsonDocument("x", 3),
new BsonDocument("x", 3) // duplicate
};
// try the batch without ContinueOnError
try {
collection.InsertBatch(batch);
} catch (MongoSafeModeException) {
Assert.AreEqual(1, collection.Count());
Assert.AreEqual(1, collection.FindOne()["x"].AsInt32);
}
// try the batch again with ContinueOnError
try {
var options = new MongoInsertOptions(collection) { Flags = InsertFlags.ContinueOnError };
collection.InsertBatch(batch, options);
} catch (MongoSafeModeException) {
Assert.AreEqual(3, collection.Count());
}
}
开发者ID:zxy050,项目名称:mongo-csharp-driver,代码行数:30,代码来源:MongoCollectionTests.cs
示例12: Save
public void Save(IEnumerable<Object> docs)
{
var mongoInsertOptions = new MongoInsertOptions();
mongoInsertOptions.WriteConcern = WriteConcern.Acknowledged;
_collection.InsertBatch(docs, mongoInsertOptions);
}
开发者ID:sergey-korol,项目名称:uniform,代码行数:6,代码来源:MongodbCollection.cs
示例13: Flush
// 将积累的内存对象保存到数据库中
public int Flush(out string strError)
{
strError = "";
if (this.SearchLogCollection == null)
{
this._searchLogCache.Clear();
return 0;
}
try
{
List<SearchLogItem> whole = new List<SearchLogItem>();
// 将打算写入数据库的内存对象移出容器,这样可以减少锁定时间
if (this.m_lock.TryEnterWriteLock(m_nLockTimeout) == false)
throw new ApplicationException("锁定尝试中超时");
try
{
if (this._searchLogCache.Count == 0)
return 0;
whole.AddRange(this._searchLogCache);
this._searchLogCache.Clear();
// this.RemoveRange(0, nCount);
}
finally
{
this.m_lock.ExitWriteLock();
}
if (this.m_lock.TryEnterReadLock(m_nLockTimeout) == false)
throw new ApplicationException("锁定尝试中超时");
try
{
MongoCollection<SearchLogItem> db_items = this.SearchLogCollection;
MongoInsertOptions options = new MongoInsertOptions() { WriteConcern = WriteConcern.Unacknowledged };
foreach (SearchLogItem item in whole)
{
db_items.Insert(item, options);
}
}
finally
{
this.m_lock.ExitReadLock();
}
// TODO: 是否考虑失败后把元素重新插入回this数组?
return 1;
}
catch (Exception ex)
{
strError = "检索日志写入数据库的过程发生错误: " + ex.Message;
return -1;
}
}
开发者ID:renyh1013,项目名称:dp2,代码行数:58,代码来源:SearchLog.cs
示例14: Flush
public void Flush()
{
long tobson = 0;
int index = 0;
Task[] tasks = new Task[_inMemoryDatabase.Collections.Keys.Count];
foreach (var pair in _inMemoryDatabase.Collections)
{
var mongoSettings = new MongoCollectionSettings {AssignIdOnInsert = false};
var mongoCollection = Database.GetCollection(typeof(BsonDocument), pair.Key.CollectionName, mongoSettings);
var inMemoryCollection = (IInMemoryCollection) pair.Value;
var stopwatch = Stopwatch.StartNew();
var docs = BsonDocumentWrapper.CreateMultiple(inMemoryCollection.Documents.Values);
stopwatch.Stop();
//Console.WriteLine("Collection {0} serialized to bson in {1:n0} ms", pair.Key, stopwatch.ElapsedMilliseconds);
tobson += stopwatch.ElapsedMilliseconds;
stopwatch.Start();
tasks[index] = Task.Factory.StartNew(() =>
{
var mongoInsertOptions = new MongoInsertOptions();
mongoInsertOptions.CheckElementNames = false;
mongoInsertOptions.WriteConcern = WriteConcern.Acknowledged;
mongoCollection.InsertBatch(docs);
}, TaskCreationOptions.LongRunning);
stopwatch.Stop();
//Console.WriteLine("Collection {0} inserted to MongoDB in {1:n0} ms", pair.Key, stopwatch.ElapsedMilliseconds);
index++;
}
Task.WaitAll(tasks);
//Console.WriteLine("Total time for serialization: {0:n0} ms", tobson);
}
开发者ID:paralect,项目名称:uniform,代码行数:40,代码来源:MongodbFlusher.cs
示例15: Initialize
private void Initialize()
{
lock (Sync)
{
var mongoUrl = MongoUrl.Create(_connectionString);
var mongoClient = new MongoClient(mongoUrl);
var server = mongoClient.GetServer();
var database = server.GetDatabase(mongoUrl.DatabaseName);
if (!database.CollectionExists(_collectionName))
{
var options = CollectionOptions
.SetCapped(true)
.SetAutoIndexId(true)
.SetMaxSize(_maxSize);
if (_maxDocuments != int.MaxValue)
options.SetMaxDocuments(_maxDocuments);
database.CreateCollection(_collectionName, options);
}
_collection = database.GetCollection(_collectionName);
_mongoInsertOptions = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
}
}
开发者ID:AlekseiKudelia,项目名称:elmah-mongodb,代码行数:25,代码来源:MongoErrorLog.cs
示例16: chartCurrent
private void chartCurrent(string[] scores, DateTime dateTime)
{
MongoCollection<BsonDocument> history = exerHist.GetCollection<BsonDocument>("history");
// graph
//We need one data series for each chart series
DataSeries<int, double> cc1 = new DataSeries<int, double>(cc1legend);
DataSeries<int, double> cc2 = new DataSeries<int, double>(cc2legend);
DataSeries<int, double> cc3 = new DataSeries<int, double>(cc3legend);
bool perfect1 = true;
bool perfect2 = true;
bool perfect3 = true;
int j = 0;
for(int i = 1; i <=10; i++)
{
double cc1v = double.Parse(scores[(i*3)-3]);
double cc2v = double.Parse(scores[(i*3)-2]);
double cc3v = double.Parse(scores[(i*3)-1]);
if (cc1v == -1 | cc1v == -2)
{
j = i;
break;
}
if (cc1v == 0)
{
cc1v = 0.05;
}
if (cc2v == 0)
{
cc2v = 0.05;
}
if (cc3v == 0)
{
cc3v = 0.05;
}
BsonDocument rep = new BsonDocument {
{ "name", user.Content.ToString() },
{ "exercise", exercisesCB.SelectedValue.ToString() },
{ "cc1", cc1v },
{ "cc2", cc2v },
{ "cc3", cc3v },
{ "time", dateTime.AddSeconds(i)},
{"timeid", dateTime}
};
var options = new MongoInsertOptions(history) { SafeMode = SafeMode.True };
history.Save(rep, options);
if (perfect1 & (cc1v == 0.05 | cc1v == 1))
{
perfect1 = false;
}
if (perfect2 & (cc2v == 0.05 | cc2v == 1))
{
perfect2 = false;
}
if (perfect3 & (cc3v == 0.05 | cc3v == 1))
{
perfect3 = false;
}
cc1.Add(new DataPoint<int, double>() { X = i, Y = cc1v });
cc2.Add(new DataPoint<int, double>() { X = i, Y = cc2v });
cc3.Add(new DataPoint<int, double>() { X = i, Y = cc3v });
}
if (j == 1)
{
// plot nothing
for (int k = 1; k <= 5; k++)
{
cc1.Add(new DataPoint<int, double>() { X = k, Y = 0 });
cc2.Add(new DataPoint<int, double>() { X = k, Y = 0 });
cc3.Add(new DataPoint<int, double>() { X = k, Y = 0 });
}
criticalComponent1.Text = "";
criticalComponent1.Text = "";
criticalComponent1.Text = "";
date.Content = "N/A";
}
else
{
if (perfect1)
{
criticalComponent1.Text = cc1legend + ": Excellent!";
buttoncc1.IsEnabled = false;
}
else
{
criticalComponent1.Text = criticalComponent1Feedback;
buttoncc1.IsEnabled = true;
}
if (perfect2)
{
criticalComponent2.Text = cc2legend + ": Excellent!";
//.........这里部分代码省略.........
开发者ID:guozanhua,项目名称:Kinect-Tracking-Project,代码行数:101,代码来源:MainWindow.xaml.cs
示例17: OrderAccess
static OrderAccess()
{
DefaultInsertOptions = new MongoInsertOptions();
DefaultInsertOptions.Flags = InsertFlags.None;
DefaultInsertOptions.SafeMode = SafeMode.True;
}
开发者ID:abel,项目名称:sinan,代码行数:6,代码来源:OrderAccess.cs
示例18: TestInsertBatchMultipleBatchesWriteConcernDisabledContinueOnErrorTrue
public void TestInsertBatchMultipleBatchesWriteConcernDisabledContinueOnErrorTrue()
{
var collectionName = LegacyTestConfiguration.Collection.Name;
var collectionSettings = new MongoCollectionSettings { WriteConcern = WriteConcern.Unacknowledged };
var collection = LegacyTestConfiguration.Database.GetCollection<BsonDocument>(collectionName, collectionSettings);
if (collection.Exists()) { collection.Drop(); }
var maxMessageLength = _primary.MaxMessageLength;
var filler = new string('x', maxMessageLength / 3); // after overhead results in two documents per sub-batch
var documents = new BsonDocument[]
{
// first sub-batch
new BsonDocument { { "_id", 1 }, { "filler", filler } },
new BsonDocument { { "_id", 2 }, { "filler", filler } },
// second sub-batch
new BsonDocument { { "_id", 3 }, { "filler", filler } },
new BsonDocument { { "_id", 3 }, { "filler", filler } }, // duplicate _id error
// third sub-batch
new BsonDocument { { "_id", 4 }, { "filler", filler } },
new BsonDocument { { "_id", 5 }, { "filler", filler } },
};
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
var results = collection.InsertBatch(documents, options);
Assert.AreEqual(null, results);
for (int i = 1; i <= 5; i++)
{
if (!SpinWait.SpinUntil(() => collection.Count(Query.EQ("_id", i)) == 1, TimeSpan.FromSeconds(4)))
{
Assert.Fail("_id {0} does not exist.", i);
}
}
}
开发者ID:p3p3pp3,项目名称:mongo-csharp-driver-for-tokumx,代码行数:35,代码来源:MongoCollectionTests.cs
示例19: TestInsertBatchContinueOnError
public void TestInsertBatchContinueOnError()
{
var collection = Configuration.TestCollection;
collection.Drop();
collection.CreateIndex(IndexKeys.Ascending("x"), IndexOptions.SetUnique(true));
var batch = new BsonDocument[]
{
new BsonDocument("x", 1),
new BsonDocument("x", 1), // duplicate
new BsonDocument("x", 2),
new BsonDocument("x", 2), // duplicate
new BsonDocument("x", 3),
new BsonDocument("x", 3) // duplicate
};
// try the batch without ContinueOnError
try
{
collection.InsertBatch(batch);
}
catch (WriteConcernException)
{
Assert.AreEqual(1, collection.Count());
Assert.AreEqual(1, collection.FindOne()["x"].AsInt32);
}
// try the batch again with ContinueOnError
if (_server.BuildInfo.Version >= new Version(2, 0, 0))
{
try
{
var options = new MongoInsertOptions { Flags = InsertFlags.ContinueOnError };
collection.InsertBatch(batch, options);
}
catch (WriteConcernException)
{
Assert.AreEqual(3, collection.Count());
}
}
}
开发者ID:wireclub,项目名称:mongo-csharp-driver,代码行数:41,代码来源:MongoCollectionTests.cs
示例20: LoadMongoData
//.........这里部分代码省略.........
// new Location( new UnLocode("FIHEL"), "Helsinki") {Id = 5},
// new Location( new UnLocode("DEHAM"), "Hamburg") {Id = 6},
// new Location( new UnLocode("USCHI"), "Chicago") {Id = 7},
// };
// ExecuteUpdate(session, locationSql, locationArgs);
// }
//
// private static void LoadItineraryData(ISession session)
// {
// const string legSql =
// "insert into Leg (id, cargo_id, voyage_id, load_location_id, unload_location_id, load_time, unload_time, leg_index) " +
// "values (?,?,?,?,?,?,?,?)";
//
// var legArgs = new[]
// {
// // Cargo 5: Hongkong - Melbourne - Stockholm - Helsinki
// new object[] {1, 5, 1, 3, 2, Ts(1), Ts(2), 0},
// new object[] {2, 5, 1, 2, 1, Ts(3), Ts(4), 1},
// new object[] {3, 5, 1, 1, 5, Ts(4), Ts(5), 2},
// // Cargo 6: Hamburg - Stockholm - Chicago - Tokyo
// new object[] {4, 6, 2, 6, 1, Ts(1), Ts(2), 0},
// new object[] {5, 6, 2, 1, 7, Ts(3), Ts(4), 1},
// new object[] {6, 6, 2, 7, 4, Ts(5), Ts(6), 2}
// };
// ExecuteUpdate(session, legSql, legArgs);
// }
public static void LoadMongoData(HandlingEventFactory handlingEventFactory,
IHandlingEventRepository handlingEventRepository)
{
Console.WriteLine("*** Loading data ***");
var db = Utils.ShippingDb;
var locations = GetLocationCollection();
var mongoInsertOptions = new MongoInsertOptions() { Flags = InsertFlags.ContinueOnError };
try
{
locations.InsertBatch(SampleLocations.GetAll(), mongoInsertOptions);
}
catch (WriteConcernException ex)
{
}
var voyages = db.GetCollection<Location>("voyages");
try
{
voyages.InsertBatch(SampleVoyages.GetAll(), mongoInsertOptions);
}
catch (WriteConcernException ex)
{
}
var cargo = db.GetCollection<Cargo>("cargo");
var routeSpecification = new RouteSpecification(SampleLocations.HONGKONG,
SampleLocations.HELSINKI,
DateUtil.ToDate("2009-03-15"));
var trackingId = new TrackingId("ABC123");
var abc123exists = new CargoRepositoryMongo(db).Find(trackingId) != null;
var handlingEvents = db.GetCollection<HandlingEvent>("handlingEvents");
if (!abc123exists)
{
var abc123 = new Cargo(trackingId, routeSpecification);
var itinerary = new Itinerary(
new List<Leg>
{
开发者ID:chandmk,项目名称:esddd,代码行数:67,代码来源:SampleDataGenerator.cs
注:本文中的MongoDB.Driver.MongoInsertOptions类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论