我需要使用官方C#驅動程序從mongo db中的集合中刪除一些記錄。我的代碼如下。C#和MongoDB使用ObjectId列表刪除記錄
public static void Remove(List<ObjectId> objectIds)
{
ObjectMongoCollection.Remove(Query.In("NotificationId", new BsonArray(objectIds)), SafeMode.True);
}
public class Notification
{
public static MongoCollection<Notification> ObjectMongoCollection = MongoHelper.GetMongoServer("MongoKelimeYarisi").GetDatabase("KelimeYarisi").GetCollection<Notification>("Notification");
[BsonId]
public ObjectId NotificationId { get; set; }
public int PlayerId { get; set; }
public string OpponentName { get; set; }
public string gameId { get; set; }
public DateTime CreateDate { get; set; }
public NotificationStatus Status = NotificationStatus.New;
public NotificationType Type = NotificationType.RoundSubmit;
public bool IsPushed { get; set; }
它運行沒有錯誤,但似乎沒有工作。我如何使用ObjectId列表刪除記錄。
也試過:
ObjectMongoCollection.Remove(Query.In("_id", new BsonArray(objectIds)), SafeMode.True);
想要添加:如果使用無類型的查詢構建器,則確實使用「_id」作爲字段名稱。我添加了一段註釋的代碼行,說明如何使用類型化查詢構建器創建相同的查詢,這通常是首選的方法。 –