本文整理汇总了C#中MongoCollection.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# MongoCollection.Remove方法的具体用法?C# MongoCollection.Remove怎么用?C# MongoCollection.Remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection.Remove方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DropDocument
/// <summary>
/// 删除单条数据
/// </summary>
/// <param name="mongoCol">表对象</param>
/// <param name="objectId">ObjectId</param>
/// <returns></returns>
public static string DropDocument(MongoCollection mongoCol, string objectId)
{
CommandResult result;
try
{
//有时候在序列化的过程中,objectId是由某个字段带上[id]特性客串的,所以无法转换为ObjectId对象
//这里先尝试转换,如果可以转换,则转换
ObjectId seekId;
if (ObjectId.TryParse(objectId, out seekId))
{
//如果可以转换,则转换
result =
new CommandResult(
mongoCol.Remove(Query.EQ(ConstMgr.KeyId, seekId), WriteConcern.Acknowledged)
.Response);
}
else
{
//不能转换则保持原状
result =
new CommandResult(
mongoCol.Remove(Query.EQ(ConstMgr.KeyId, objectId), WriteConcern.Acknowledged)
.Response);
}
}
catch (MongoCommandException ex)
{
result = new CommandResult(ex.Result);
}
BsonElement err;
return !result.Response.TryGetElement("err", out err) ? string.Empty : err.ToString();
}
示例2: Delete
private static void Delete(MongoCollection<Log> logs)
{
var findOldLogsQuery = Query.And(Query.LT("LogDate", DateTime.Now.AddDays(-1)));
logs.Remove(findOldLogsQuery);
Console.WriteLine("DELETE");
logs.FindAll().Print();
}
示例3: UpdateDocument
public override void UpdateDocument(MongoCollection<BsonDocument> collection, BsonDocument document) {
if (document.Contains("oid")) {
var organizationId = document.GetValue("oid").AsNullableObjectId;
if (!organizationId.HasValue || organizationId.Value == ObjectId.Empty) {
document.Remove("oid");
} else {
var organizationCollection = Database.GetCollection("organization");
var organization = organizationCollection.FindOneById(organizationId);
// The organization with this id could not be found.. Remove the webhook.
if (organization == null) {
collection.Remove(Query.EQ("_id", document.GetDocumentId()));
return;
}
}
}
var projectCollection = Database.GetCollection("project");
if (document.Contains("pid")) {
var projectId = document.GetValue("pid").AsNullableObjectId;
if (!projectId.HasValue || projectId.Value == ObjectId.Empty) {
document.Remove("pid");
} else {
var project = projectCollection.FindOneById(projectId);
// The project with this id could not be found.. Remove the webhook.
if (project == null) {
collection.Remove(Query.EQ("_id", document.GetDocumentId()));
return;
}
if (!document.Contains("oid"))
document.Set("oid", project.GetValue("oid").AsObjectId);
}
}
if (!document.Contains("oid") && !document.Contains("pid")) {
collection.Remove(Query.EQ("_id", document.GetDocumentId()));
return;
}
collection.Save(document);
}
示例4: UpdateDocument
public override void UpdateDocument(MongoCollection<BsonDocument> collection, BsonDocument document) {
if (document.Contains("oid")) {
var organizationId = document.GetValue("oid").AsNullableObjectId;
if (!organizationId.HasValue || organizationId.Value == ObjectId.Empty) {
// The organization id is invalid. Remove the token.
collection.Remove(Query.EQ("_id", document.GetDocumentId()));
return;
}
var organizationCollection = Database.GetCollection("organization");
var organization = organizationCollection.FindOneById(organizationId.Value);
// The organization with this id could not be found.. Remove the token.
if (organization == null) {
collection.Remove(Query.EQ("_id", document.GetDocumentId()));
return;
}
}
if (document.Contains("pid")) {
var projectId = document.GetValue("pid").AsNullableObjectId;
if (!projectId.HasValue || projectId.Value == ObjectId.Empty) {
// The project id is invalid. Remove the token.
collection.Remove(Query.EQ("_id", document.GetDocumentId()));
return;
}
var projectCollection = Database.GetCollection("project");
var project = projectCollection.FindOneById(projectId.Value);
// The project with this id could not be found.. Remove the token.
if (project == null) {
collection.Remove(Query.EQ("_id", document.GetDocumentId()));
return;
}
}
// Remove the token if it's not associated to an organization, project or user.
if (!document.Contains("oid") && !document.Contains("pid") && !document.Contains("uid")) {
collection.Remove(Query.EQ("_id", document.GetDocumentId()));
}
}
示例5: DeleteGift
public bool DeleteGift(Guid id)
{
if (mongoServer != null && giftedDB != null)
{
giftCollection = giftedDB.GetCollection<Gift>("Gift");
IMongoQuery query = Query.EQ("_id", id);
SafeModeResult result = giftCollection.Remove(query);
return result.DocumentsAffected == 1;
}
else
return false;
}
示例6: RemoveWords
public static void RemoveWords(MongoCollection words)
{
Console.Write("Enter word for remove: ");
string editWord = Console.ReadLine().ToLower();
var searched = words.AsQueryable<Word>().FirstOrDefault(w => w.Value == editWord);
if (searched != null)
{
var query = Query.EQ("_id", searched.Id);
words.Remove(query);
}
else
{
Console.WriteLine("This word is not exist");
}
}
示例7: DeleteAllDocumentsFromCollection
/// <summary>
/// Demo deleting documents from collection.
/// </summary>
/// <param name="orders">The orders.</param>
static void DeleteAllDocumentsFromCollection( MongoCollection< Order > collection )
{
Console.WriteLine( "\n\n======= Delete Documents =======" );
Console.WriteLine( string.Format( "Document Count Before Delete: [ {0} ]", collection.Count() ) );
// Delete documents matching a criteria.
collection.Remove( new {CustomerName = "Elmer Fudd"} );
Console.WriteLine( string.Format( "Document Count After Deleting Elmer Fudd: [ {0} ]", collection.Count() ) );
// Delete all docs.
collection.Remove( new {} );
Console.WriteLine( "Deleted all docs" );
Console.WriteLine( string.Format( "Document Count After Deleting All Docs: [ {0} ]", collection.Count() ) );
}
示例8: Remove
protected bool Remove(MongoCollection<object> collection, Guid id)
{
return collection.Remove(Query.EQ("_id", id), WriteConcern.Acknowledged).Ok;
}
示例9: DropJsDocument
/// <summary>
/// 删除js数据
/// </summary>
/// <param name="mongoCol"></param>
/// <param name="strKey"></param>
/// <returns></returns>
public static string DropJsDocument(MongoCollection mongoCol, object strKey)
{
var result = new CommandResult(new BsonDocument());
if (QueryHelper.IsExistByKey(strKey.ToString()))
{
try
{
result =
new CommandResult(
mongoCol.Remove(Query.EQ(ConstMgr.KeyId, (BsonValue) strKey), WriteConcern.Acknowledged)
.Response);
}
catch (MongoCommandException ex)
{
result = new CommandResult(ex.Result);
}
}
BsonElement err;
return !result.Response.TryGetElement("err", out err) ? string.Empty : err.ToString();
}
示例10: DropDocument
/// <summary>
/// 删除数据
/// </summary>
/// <param name="mongoCol"></param>
/// <param name="strKey"></param>
/// <param name="keyField"></param>
/// <returns></returns>
public static Boolean DropDocument(MongoCollection mongoCol, object strKey, String keyField = "_id")
{
if (IsExistByKey(mongoCol, (BsonValue)strKey, keyField))
{
mongoCol.Remove(Query.EQ(keyField, (BsonValue)strKey));
}
return true;
}
示例11: DropDocument
/// <summary>
/// 删除数据
/// </summary>
/// <param name="mongoCol"></param>
/// <param name="strKey"></param>
/// <returns></returns>
public static String DropDocument(MongoCollection mongoCol, object strKey)
{
var result = new CommandResult(new BsonDocument());
if (QueryHelper.IsExistByKey(mongoCol, (BsonValue)strKey))
{
try
{
result = mongoCol.Remove(Query.EQ(KEY_ID, (BsonValue) strKey), WriteConcern.Acknowledged);
}
catch (MongoCommandException ex)
{
result = ex.CommandResult;
}
}
if (result.Response["err"] == BsonNull.Value)
{
return String.Empty;
}
return result.Response["err"].ToString();
}
示例12: DeleteTestData
/// <summary>
/// 删除测试数据.
/// </summary>
/// <param name="collection"></param>
private void DeleteTestData(MongoCollection<TestLinq> collection)
{
var query = Query<TestLinq>.GT(p => p.SeqNumber , 0);
collection.Remove(query);
}
示例13: DropDocument
/// <summary>
/// 删除数据
/// </summary>
/// <param name="mongoCol"></param>
/// <param name="strKey"></param>
/// <param name="keyField"></param>
/// <returns></returns>
public static Boolean DropDocument(MongoCollection mongoCol, object strKey)
{
if (IsExistByKey(mongoCol, (BsonValue)strKey))
{
try
{
mongoCol.Remove(Query.EQ(KEY_ID, (BsonValue)strKey), new SafeMode(true));
}
catch (Exception)
{
return false;
}
}
return true;
}
示例14: NewMethod2
private int NewMethod2(MongoCollection coll)
{
var v = coll.FindAs<BsonDocument>(Query.Null);
Dictionary<string, string> results = new Dictionary<string, string>();
List<BsonDocument> list = new List<BsonDocument>();
List<string> oldID = new List<string>();
foreach (var item in v)
{
BsonValue value;
if (item.TryGetValue("_id", out value))
{
string oldName = value.ToString();
string newName = ReplayceName(oldName);
if (oldName != newName)
{
oldID.Add(oldName);
item["_id"] = newName;
list.Add(item);
}
}
}
foreach (var item in list)
{
coll.Save(item);
}
foreach (var item in oldID)
{
coll.Remove(Query.EQ("_id", item));
}
return results.Count;
}
示例15: DeleteClick
static void DeleteClick(Click click, MongoCollection clicks)
{
var query = Query.EQ("_id", click.Id);
clicks.Remove(query);
}