Wenn ich Ihre Anforderung richtig verstanden habe, ein pullFilter
ist das, was Sie brauchen.
var filter = Builders<UserCollection>.Filter.Where(u => u.User.Contacts.Any(c => c._Id == contactID));
var update = Builders<UserCollection>.Update.PullFilter(u => u.User.Contacts, c => c._Id == contactID);
collection.UpdateOne(filter, update);
Hier ist das vollständige Programm zum Testen, falls es jemanden interessiert.
using MongoDB.Entities; // PM> Install-Package MongoDB.Entities
using MongoDB.Bson;
using System.Linq;
namespace StackOverflow
{
public class Program
{
public class UserCollection : Entity
{
public User User { get; set; }
}
public class User
{
public Contact[] Contacts { get; set; }
}
public class Contact
{
public ObjectId _Id { get; set; }
}
private static void Main(string[] args)
{
new DB("test");
var contactID = ObjectId.GenerateNewId();
(new UserCollection
{
User = new User
{
Contacts = new[]
{
new Contact { _Id = ObjectId.GenerateNewId()},
new Contact { _Id = contactID}
}
}
}).Save();
DB.Update<UserCollection>()
.Match(u => u.User.Contacts.Any(c => c._Id == contactID))
.Modify(b => b.PullFilter(u => u.User.Contacts, c => c._Id == contactID))
.Execute();
}
}
}