插入多行并检查两个字段中的重复项。 Mongodb C#
Insert many rows and check for duplicates in both field. Mongodb C#
我在一个集合中有 2 个字段,该集合是空的,需要填充。
它是这样定义的:
public class MyColl
{
[BsonElement("id")]
public ObjectId id { get; set; }
public ObjectId A { get; set; }
public ObjectId B { get; set; }
}
what i want to achieve is this:
Let K be a pair to be insert e.g. K=(x,y) i want that mongodb before perform insertion of the rows check the following constraints:
1) if A==x AND B==y and this already exist in collection, don't insert that.
2) if A==y AND B==x and this already exist in collection, don't insert that.
3) if A==x AND B==y and already exist A==y and B==x, don't insert that.
4) if A==y AND B==x and already exist A==x and B==y, don't insert that.
我认为我需要的是 (A,B) 和 (B,A) 上的唯一复合索引来检查 1 和 2,但我不知道如何处理其他两个约束。
我该怎么做?
我通过稍微修改 class:
的定义解决了这个问题
public class MyColl {
[BsonElement("id")]
public ObjectId id { get; set; }
public ObjectId A { get; set; }
public ObjectId B { get; set; }
public string C {get;set;} }
然后每次我添加一个元素时我都会创建一个列表:
List<string> elements = new List<string>();
elements.Add(b.toString())
elements.Add(c.toString())
elements.Sort();
MyColl.C = string.Concat(elements[0],elements[1]);
C 包含按字母顺序排序的索引,因此将其设置为唯一索引将导致每次插入重复项时出现异常。
我在一个集合中有 2 个字段,该集合是空的,需要填充。 它是这样定义的:
public class MyColl { [BsonElement("id")] public ObjectId id { get; set; } public ObjectId A { get; set; } public ObjectId B { get; set; } }
what i want to achieve is this:
Let K be a pair to be insert e.g. K=(x,y) i want that mongodb before perform insertion of the rows check the following constraints:
1) if A==x AND B==y and this already exist in collection, don't insert that.
2) if A==y AND B==x and this already exist in collection, don't insert that.
3) if A==x AND B==y and already exist A==y and B==x, don't insert that.
4) if A==y AND B==x and already exist A==x and B==y, don't insert that.
我认为我需要的是 (A,B) 和 (B,A) 上的唯一复合索引来检查 1 和 2,但我不知道如何处理其他两个约束。
我该怎么做?
我通过稍微修改 class:
public class MyColl {
[BsonElement("id")]
public ObjectId id { get; set; }
public ObjectId A { get; set; }
public ObjectId B { get; set; }
public string C {get;set;} }
然后每次我添加一个元素时我都会创建一个列表:
List<string> elements = new List<string>();
elements.Add(b.toString())
elements.Add(c.toString())
elements.Sort();
MyColl.C = string.Concat(elements[0],elements[1]);
C 包含按字母顺序排序的索引,因此将其设置为唯一索引将导致每次插入重复项时出现异常。