我正在插入一批名字:
myCollection.InsertBatch(value.Split(',').Where(o=> !string.IsNullOrEmpty(o)).Select( o => new Client { Name = o.Trim() }));
如何只插入那些名称相同的?
P.S。 MongoInsertOptions在这种情况下有用吗?
答案 0 :(得分:2)
在“名称”上创建唯一索引
for example, in shell: db.MyCollection.ensureIndex({"Name":1}, {unique = true})
添加InsertOptions
var options = new MongoInsertOptions (myCollection) { CheckElementNames = true, Flags = InsertFlags.ContinueOnError, SafeMode = SafeMode.True};
var res = myCollection.InsertBatch(value.Split(',').Where(o => !string.IsNullOrEmpty(o)).Select(o => new Client { Name = o.Trim() }), options);