4

公式のC#ドライバーを使用して、mongodbのコレクションからいくつかのレコードを削除する必要があります。私のコードは次のとおりです。

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);
4

3 に答える 3

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);
    }
于 2012-08-31T10:32:45.067 に答える
4

これを再現することはできません。私はあなたのコードにできるだけ似たテストプログラムを書きました、そしてそれは実際に複数のレコードを削除します。これが私のテストプログラムです:

http://pastie.org/4618039

ご不明な点がございましたら、お気軽にお問い合わせください。

于 2012-08-30T18:46:33.763 に答える
1

次のようなメソッドを作成できます。

public void destroy() {
        ...
    }

このメソッドでは、ObjectIdのリストを使用して、それらをループします。

foreach(Enitity enitity in entities) {
    ...
}

MongoDB.Driver.Linqを使用して、リスト内の特定のクエリを実行できます。

var query = Query<Entity>.EQ(e => e.Attribute, entity.Value);
db.GetCollection<Entity>("Entity").Remove(query);

ここで、Valueは、必要なオブジェクトの値になります。これにより、特定の値を含む要素がデータベースから削除されます。

これがお役に立てば幸いです。

于 2014-12-06T15:21:04.363 に答える