C#和MongoDB使用ObjectId列表删除记录

hak*_*kan 4 c# mongodb

我需要使用官方C#驱动程序从mongo db中的集合中删除一些记录.我的代码如下.

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; }
Run Code Online (Sandbox Code Playgroud)

它运行没有错误,但似乎不起作用.如何使用ObjectIds列表删除记录.

还尝试过:

ObjectMongoCollection.Remove(Query.In("_id", new BsonArray(objectIds)), SafeMode.True);
Run Code Online (Sandbox Code Playgroud)

hak*_*kan 5

我使用不同的方法来获取mongo查询并且有效.我构建了一个linq查询并将其转换为mongo查询.

    public static void Remove(List<ObjectId> objectIds)
    {
        var linqQuery = from n in ObjectMongoCollection.AsQueryable<Notification>() where n.NotificationId.In(objectIds) select n;
        var mongoQuery = ((MongoQueryable<Notification>)linqQuery).GetMongoQuery();       
        ObjectMongoCollection.Remove(mongoQuery);
    }
Run Code Online (Sandbox Code Playgroud)