Trim 存在于 Mongo 数据库中的值
Trim Values existing in Mongo Database
我集合中的数据可能在前面和后面都有空格我想做的是 trim 所有空格并进行 == 比较以获得适当的记录我的代码如下:
var test = await _dataStore.FindMostRecentAsync(x => x.Barcodes.PrimaryBarcode.Trim() == barcode.Trim());
当我 运行 这段代码时,它给我一个错误 .Trim()
不受支持(它仅在我 trim 我传入的条形码字符串变量时有效。
trim 我收集的数据的最佳方式是什么,以便我可以进行精确比较。
堆栈跟踪
at
MongoDB.Driver.Linq.Translators.PredicateTranslator.GetFieldExpression(Expression
expression) at
MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(Expression
variableExpression, ExpressionType operatorType, ConstantExpression
constantExpression) at
MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression
node) at
MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression
node, IBsonSerializerRegistry serializerRegistry) at
MongoDB.Driver.MongoCollectionImpl1.CreateFindOperation[TProjection](FilterDefinition
1
filter, FindOptions2 options) at
MongoDB.Driver.MongoCollectionImpl
1.FindAsync[TProjection](IClientSessionHandle
session, FilterDefinition1 filter, FindOptions
2 options,
CancellationToken cancellationToken) at
MongoDB.Driver.MongoCollectionImpl1.<>c__DisplayClass37_0
1.b__0(IClientSessionHandle
session) at
MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSessionAsync[TResult](Func
2
funcAsync, CancellationToken cancellationToken)
我为此提出的解决方案是执行以下操作:
var test = await _dataStore.FindMostRecentAsync(x => x.Barcodes.PrimaryBarcode.Contains(barcode.Trim()));
我不会尝试 trim 数据库中存在的值,而是 trim 变量,然后执行 Contains 这样我将获得与我的条形码匹配的所有值,稍后使用该对象数组,我可以继续按日期或不按日期过滤。
我还没有找到任何方法可以使用 C# MongoDriver
为集合对象传入 Trim() 方法
您必须使用聚合函数才能调用 trim operator。
遗憾的是,没有通过 C# 驱动程序调用的直接方法,但是您可以使用一些 BsonDocuments 构建一个,如下所示:
var barcode = " 1512356 ";
//This exclude the trimmedField from the result.
var projectionDefinition = Builders<BsonDocument>.Projection.Exclude("trimmedField");
//Call the trim operator and put it in the temporary trimmedField property (this trims the barcode on the database)
var expression = new BsonDocument(new List<BsonElement>
{
new BsonElement("trimmedField", new BsonDocument(new BsonDocument("$trim", new BsonDocument("input", "$Barcodes.PrimaryBarcode"))))
});
//Add the trimmedField to the document
var addFieldsStage = new BsonDocument(new BsonElement("$addFields", expression));
//Build a filter on the trimmedField and trim the local variable
var trimFilter = Builders<BsonDocument>.Filter.Eq(x => x["trimmedField"], barcode.Trim());
//Put it all together
var result = collection.Aggregate().AppendStage<BsonDocument>(addFieldsStage).Match(trimFilter).Project(projectionDefinition).As<YourType>().ToList();
确保在 .As<T>
中输入正确的类型,以便能够投射实体。
如果您在 class 上方添加 [BsonIgnoreExtraElements]
,您将能够放下投影舞台。
我集合中的数据可能在前面和后面都有空格我想做的是 trim 所有空格并进行 == 比较以获得适当的记录我的代码如下:
var test = await _dataStore.FindMostRecentAsync(x => x.Barcodes.PrimaryBarcode.Trim() == barcode.Trim());
当我 运行 这段代码时,它给我一个错误 .Trim()
不受支持(它仅在我 trim 我传入的条形码字符串变量时有效。
trim 我收集的数据的最佳方式是什么,以便我可以进行精确比较。
堆栈跟踪
at MongoDB.Driver.Linq.Translators.PredicateTranslator.GetFieldExpression(Expression expression) at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.MongoCollectionImpl
1.CreateFindOperation[TProjection](FilterDefinition
1 filter, FindOptions2 options) at MongoDB.Driver.MongoCollectionImpl
1.FindAsync[TProjection](IClientSessionHandle session, FilterDefinition1 filter, FindOptions
2 options, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.<>c__DisplayClass37_0
1.b__0(IClientSessionHandle session) at MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSessionAsync[TResult](Func
2 funcAsync, CancellationToken cancellationToken)
我为此提出的解决方案是执行以下操作:
var test = await _dataStore.FindMostRecentAsync(x => x.Barcodes.PrimaryBarcode.Contains(barcode.Trim()));
我不会尝试 trim 数据库中存在的值,而是 trim 变量,然后执行 Contains 这样我将获得与我的条形码匹配的所有值,稍后使用该对象数组,我可以继续按日期或不按日期过滤。
我还没有找到任何方法可以使用 C# MongoDriver
为集合对象传入 Trim() 方法您必须使用聚合函数才能调用 trim operator。
遗憾的是,没有通过 C# 驱动程序调用的直接方法,但是您可以使用一些 BsonDocuments 构建一个,如下所示:
var barcode = " 1512356 ";
//This exclude the trimmedField from the result.
var projectionDefinition = Builders<BsonDocument>.Projection.Exclude("trimmedField");
//Call the trim operator and put it in the temporary trimmedField property (this trims the barcode on the database)
var expression = new BsonDocument(new List<BsonElement>
{
new BsonElement("trimmedField", new BsonDocument(new BsonDocument("$trim", new BsonDocument("input", "$Barcodes.PrimaryBarcode"))))
});
//Add the trimmedField to the document
var addFieldsStage = new BsonDocument(new BsonElement("$addFields", expression));
//Build a filter on the trimmedField and trim the local variable
var trimFilter = Builders<BsonDocument>.Filter.Eq(x => x["trimmedField"], barcode.Trim());
//Put it all together
var result = collection.Aggregate().AppendStage<BsonDocument>(addFieldsStage).Match(trimFilter).Project(projectionDefinition).As<YourType>().ToList();
确保在 .As<T>
中输入正确的类型,以便能够投射实体。
如果您在 class 上方添加 [BsonIgnoreExtraElements]
,您将能够放下投影舞台。