2017-06-18 7 views
0

この要素をfruitsというmongodbコレクションから削除するにはどうすればいいですか? 与えられたコレクションでは、 "タグ"の要素として "オレンジ"が含まれているので、最初の要素を削除する式が必要です。キーが配列内の値を持っている場合、mongodbのコレクションから要素を削除します

{"_id":ObjectId("dedrfrcece"), "tags":["apple", "orange"]} 
{"_id":ObjectId("afedfrcece"), "tags":["apple", "banana"]} 

答えて

0

私はあなたが削除することを前提としてい「タグ」配列に「オレンジ」要素を含むすべてのドキュメント。

db.fruits.remove({ tags: "orange"}) 
0

the "$pull" operatorを使用する必要があります。ここで

は完全な例(私は任意のカスタムマッピングを必要とせずにあなたの例に沿ったものにするために小文字のプロパティ名の「タグ」を選択しました)です:

public class Test 
{ 
    public ObjectId Id { get; set; } 
    public string[] tags { get; set; } 
} 

public class Program 
{ 
    static void Main(string[] args) 
    { 
     var collection = new MongoClient().GetDatabase("test").GetCollection<Test>("Test"); 

     collection.InsertMany(new[] { new Test { tags = new[] { "apple", "orange" } } }); 
     collection.InsertMany(new[] { new Test { tags = new[] { "apple", "banana" } } }); 

     collection.UpdateMany(Builders<Test>.Filter.Empty, Builders<Test>.Update.Pull(test => test.tags, "orange")); 

     Console.ReadLine(); 
    } 
} 
関連する問題