你能帮助我正确使用2.0驱动程序“拉(删除)”。
我有一个这样的集合,我想删除第一个跟随者字段命名为fethiye的追随者。
files, _ := ioutil.ReadDir(my_dir)
for _, f := range files {
fmt.Println(f.Name())
}
如何修复此查询?
{
"_id": ObjectId("554e05dfc90d3d4dfcaa2aea"),
"username": "bodrum",
"followerList": [
{
"_id": ObjectId("554e0625a51586362c33c6df"),
"follower": "fethiye",
"avatar": "fethiye.png"
},
{
"_id": ObjectId("554e0625a51586362c33c6df"),
"follower": "izmir",
"avatar": "izmir.png"
}
]
}
感谢。
答案 0 :(得分:39)
使用过滤器删除数组元素时,需要使用PullFilter
构建器而不是Pull
(与整个元素匹配)。
var collection = db.GetCollection<Person>("people");
var filter = new BsonDocument("username", "bodrum");
var update = Builders<Person>.Update.PullFilter("followerList",
Builders<Follower>.Filter.Eq("follower", "fethiye"));
var result = collection.FindOneAndUpdateAsync(filter, update).Result;
或者更简洁一点,使用lambdas:
var update = Builders<Person>.Update.PullFilter(p => p.followerList,
f => f.follower == "fethiye");
var result = collection
.FindOneAndUpdateAsync(p => p.username == "bodrum", update).Result;
答案 1 :(得分:1)
这是我用来删除嵌套数组对象的方法
-parentpath:followerList
-propertie:follower
值:fethiye.png
var filter = new BsonDocument("_id", ObjectId.Parse(id));
var updateValues = Builders<object>.Update.PullFilter(parentPath,
Builders<object>.Filter.Eq(propertie, value));
DatabaseCollection.FindOneAndUpdate(filter, updateValues);
删除更深层嵌套数组对象的示例:
让我们删除名为Doe
-parentPath:followerList.0.testArray
-propertie:name
- 值:Doe
{
"_id": ObjectId("554e05dfc90d3d4dfcaa2aea"),
"username": "bodrum",
"followerList": [
{
"_id": ObjectId("554e0625a51586362c33c6df"),
"follower": "fethiye",
"testArray": [
{
"name":"John"
},
{
"name":"Doe"
},
{
"name":"Jason"
}
]
},
{
"_id": ObjectId("554e0625a51586362c33c6df"),
"follower": "izmir",
"avatar": "izmir.png"
}
]
}
答案 2 :(得分:1)
我可以使用MongoDB.Entities提供一个简单得多的解决方案。 person
和followers
存储在自己的集合中,代表一对多关系。
using System.Linq;
using MongoDB.Entities;
namespace StackOverflow
{
public class Person : Entity
{
public string Username { get; set; }
public Many<Follower> FollowerList { get; set; }
public Person() => this.InitOneToMany(() => FollowerList);
}
public class Follower : Entity
{
public string Name { get; set; }
public string Avatar { get; set; }
}
class Program
{
static void Main(string[] args)
{
new DB("followers");
var person1 = new Person { Username = "bodrum"};
person1.Save();
var follower1 = new Follower { Name = "fethiye", Avatar= "fethiye.png" };
follower1.Save();
var follower2 = new Follower { Name = "izmir", Avatar = "izmir.png" };
follower2.Save();
person1.FollowerList.Add(follower1);
person1.FollowerList.Add(follower2);
var fathiye = person1.FollowerList.Collection()
.Where(p => p.Name == "fethiye")
.FirstOrDefault();
person1.FollowerList.Remove(fathiye);
}
}
}
答案 3 :(得分:1)
假设您有一个收藏名称“人”, 您可以使用PullFilter从数组中删除记录
var filterBuilder = Builders<Person>.Filter.Eq(person => person.username, "bodrum");
var updateBuilder = Builders<Person>.Update.PullFilter(p => p.followerList,
Builders<Person>.Filter.Eq(per => per.follower, "fethiye"));
var updateResult = collection.UpdateOne(filterBuilder, updateBuilder);
Console.WriteLine(
$"MatchedCount: {updateResult.MatchedCount}, ModifiedCount: {updateResult.ModifiedCount}");
如果我们还需要删除过滤后的文档中的值数组,可以用此行替换更新构建器
var updateBuilder = Builders<Person>.Update.PullFilter(p => p.followerList,
Builders<Person>.Filter.In(per => per.follower, new List<string> {"fethiye", "izmir"}));
还可以保存许多文档,可以将updateOne替换为updateMany
var updateResult = collection.UpdateMany(filterBuilder, updateBuilder);