private void BuildScriptWithInsert(Table table, IDataWrapper data, IDataFilter filter, out object script) { InsertScript ins = new InsertScript(); int r = 1; if (data.GetValueByOriName(DocumentField) is BsonDocument doc) { ins.Data.Add(doc); while (r < table.PageSize && data.Read()) { r++; object obj = filter.GetValue(data, -1, DocumentField); if (obj == DBNull.Value || obj == null) { doc = data.GetValueByOriName(DocumentField) as BsonDocument; } else { doc = obj as BsonDocument; } ins.Data.Add(doc); } } else { data.MapFields(null); // 取消字段映射 doc = new BsonDocument(); for (int i = 0; i < data.FieldCount; i++) { doc[data.GetFieldName(i)] = GetValue(filter.GetValue(data, i, data.GetFieldName(i))); } ins.Data.Add(doc); while (r < table.PageSize && data.Read()) { r++; doc = new BsonDocument(); for (int i = 0; i < data.FieldCount; i++) { doc[data.GetFieldName(i)] = GetValue(filter.GetValue(data, i, data.GetFieldName(i))); } ins.Data.Add(doc); } } script = ins; }
private void BuildScriptWithUpdate(IDataWrapper data, IDataFilter filter, out object script) { UpdateScript upd = new UpdateScript(); if (data.GetValueByOriName(DocumentField) is BsonDocument doc) { object obj = filter.GetValue(data, -1, DocumentField); if (obj != DBNull.Value && obj != null) { doc = obj as BsonDocument; } upd.Filter = $"{{\"{IDField}\":\"{doc["_id"]}\"}}"; upd.Data.AddRange(doc); upd.Data.Remove(IDField); } else { for (int i = 0; i < data.FieldCount; i++) { string field = data.GetFieldName(i); if (IDField.Equals(field)) { upd.Filter = $"{{\"{IDField}\":\"{filter.GetValue(data, i, data.GetFieldName(i))}\"}}"; } else { upd.Data[data.GetFieldName(i)] = GetValue(filter.GetValue(data, i, data.GetFieldName(i))); } } } script = upd; }