c# - 如何在 C# MongoDB Driver 聚合管道中使用正则表达式
问题描述
我想在 MongoDB 中用 C# 进行查询并在数据网格中显示结果。在我的查询方法的管道中,我首先要过滤年份。但是,在我的 MongoDB 数据库中,只有年份没有任何价值。我只有日期值。所以我的想法是使用正则表达式从日期值中过滤年份。该日期值的键是Bestelldatum
.
我尝试了在 MongoDB 文档中找到的以下正则表达式,但它不起作用。它给了我一个错误($regexMatch not recognized
):
https://docs.mongodb.com/manual/reference/operator/aggregation/regexMatch/#exp._S_regexMatch
public List<T> MeistenHv<T>(string coll, string year)
{
/* Pipeline anlegen */
//Alle Dokumente mit dem angegebenen Jahr
var regex = new BsonDocument
{
{
"$addFields", new BsonDocument
{
{ "results", new BsonDocument
{
{"$regexMatch", new BsonDocument
{
{ "input", "$Bestelldatum" },
{ "regex", year}
}
}
}
}
}
}
};
//Gruppiert nach Hv, bei jedem Treffer um 1 inkrementieren
var group = new BsonDocument
{
{ "$group", new BsonDocument
{
{"_id", new BsonDocument
{
{"Hv", "$Hv"}
}
},
{"uniqueIds", new BsonDocument
{
{"$addToSet", "$_id"}
}
},
{"count", new BsonDocument
{
{"$sum", 1 }
}
}
}
}
};
//Absteigend sortiert nach der Häufigkeit der Hv
var sort = new BsonDocument
{
{
"$sort", new BsonDocument
{
{"count", -1 }
}
}
};
var pipeline = new[] { regex, group, sort };
var collection = db.GetCollection<T>(coll);
var data = collection.Aggregate<T>(pipeline).ToList();
return data;
}
我也尝试过同样的错误:
var regex = new BsonDocument
{
{
"$regex", new BsonDocument
{
{ "Bestelldatum", year}
}
}
};
编辑:(包括sampledoc json)
{
"_id" : ObjectId("5e31bb10c1b1e773e46be7f8"),
"LsNr" : "L1016",
"Bv" : "Blahblah",
"Hv" : "27292",
"Bauleiter" : "Jonathan Fakename",
"Projektleiter" : "Patrick NotMyRealName",
"Besteller" : "zuwenig auf Baustelle",
"Abholer" : "Herr Brown",
"Lagerort" : "Lager",
"Bemerkung" : null,
"Status" : "gebucht",
"Sachbearbeiter" : "",
"Abholdatum" : null,
"Beleg" : "L1016",
"Umsatz" : "39.39",
"Gegenkonto" : "5999",
"Buchungsdatum" : "22.08.2018",
"Konto" : "5150",
"Kost1" : null,
"Kost2" : "27292",
"Text" : "Blahblah",
"Bestelldatum" : "22.08.2018",
"Heute" : "29.01.2020",
"artikelliste" : [
{
"_id" : ObjectId("5e31bb10c1b1e773e46be7f4"),
"Menge" : "1",
"Bezeichnung" : "My great article",
"Einheit" : "Eimer",
"Anforderungsgrund" : null,
"Einzelpreis" : "2.91",
"Gesamtpreis" : "29.1",
"Mgkzswert" : "31.43",
"Mgkzssatz" : "8",
"LetztesUpdate" : "29.01.2020 18:04:16",
"PersonUpdate" : "username",
"Kommentar" : null
},
{
"_id" : ObjectId("5e31bb10c1b1e773e46be7f5"),
"Menge" : "1",
"Bezeichnung" : "What an awesome article",
"Einheit" : "Stück",
"Anforderungsgrund" : null,
"Einzelpreis" : "3.98",
"Gesamtpreis" : "3.98",
"Mgkzswert" : "4.3",
"Mgkzssatz" : "8",
"LetztesUpdate" : "29.01.2020 18:04:16",
"PersonUpdate" : "username",
"Kommentar" : null
},
{
"_id" : ObjectId("5e31bb10c1b1e773e46be7f6"),
"Menge" : "1",
"Bezeichnung" : "Great articles you have here man",
"Einheit" : "Stück",
"Anforderungsgrund" : null,
"Einzelpreis" : "1.69",
"Gesamtpreis" : "1.69",
"Mgkzswert" : "1.83",
"Mgkzssatz" : "8",
"LetztesUpdate" : "29.01.2020 18:04:16",
"PersonUpdate" : "username",
"Kommentar" : null
},
{
"_id" : ObjectId("5e31bb10c1b1e773e46be7f7"),
"Menge" : "1",
"Bezeichnung" : "Is it available in huge, too?",
"Einheit" : "Stück",
"Anforderungsgrund" : null,
"Einzelpreis" : "1.69",
"Gesamtpreis" : "1.69",
"Mgkzswert" : "1.83",
"Mgkzssatz" : "8",
"LetztesUpdate" : "29.01.2020 18:04:16",
"PersonUpdate" : "username",
"Kommentar" : null
}
]
}
这是我的映射模型类:
using System;
using System.Windows;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Collections.ObjectModel;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
namespace Lieferscheine.Models
{
public class
LieferscheinModel : INotifyPropertyChanged
{
//ATTRIBUTE
[BsonRepresentation(BsonType.ObjectId)]
public string _id;
public string Bauleiter { get; set; }
public string Projektleiter { get; set; }
public string Besteller { get; set; }
public string Abholer { get; set; }
public string Lagerort { get; set; }
public string Bemerkung { get; set; }
//rechts
public string Status { get; set; }
public string Sachbearbeiter { get; set; }
public string Abholdatum { get; set; }
public string Buchungsdatum { get; set; }
public string Konto { get; set; }
public string Kost1 { get; set; }
private ObservableCollection<ArtikelModel> _artikel = new ObservableCollection<ArtikelModel>();
private string _heute = DateTime.Now.ToString("dd.MM.yyyy");
private string _bestelldatum;
//************************************************************************************
//PROPERTIES
private string _lsNr;
public string LsNr
{
get
{
return _lsNr;
}
set
{
_lsNr = value;
OnPropertyChanged("LsNr");
Beleg = _lsNr;
}
}
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
}
}
private string _hv;
public string Hv
{
get { return _hv; }
set
{
_hv = value;
OnPropertyChanged("Hv");
Kost2 = _hv;
}
}
private string _bv;
public string Bv
{
get { return _bv; }
set
{
_bv = value;
OnPropertyChanged("Bv");
Text = _bv;
}
}
private string _beleg;
public string Beleg
{
get { return _beleg; }
set
{
_beleg = value;
OnPropertyChanged("Beleg");
}
}
private string _gegenkonto = "5999";
public string Gegenkonto
{
get { return _gegenkonto; }
set
{
_gegenkonto = value;
OnPropertyChanged("Gegenkonto");
}
}
private decimal _umsatz = 0;
public decimal Umsatz
{
get { return _umsatz; }
set
{
_umsatz = value;
OnPropertyChanged("Umsatz");
}
}
private string _kost2;
public string Kost2
{
get { return _kost2; }
set
{
_kost2 = value;
OnPropertyChanged("Kost2");
}
}
public string Bestelldatum
{
get { return _bestelldatum; }
set
{
_bestelldatum = value;
OnPropertyChanged("Bestelldatum");
}
}
public string Heute
{
get { return _heute; }
set { _heute = value; } //private hinzufügen!
}
public ObservableCollection<ArtikelModel> artikelliste
{
get { return _artikel; }
set
{
_artikel = value;
OnPropertyChanged("artikelliste");
}
}
// *******************************************************************************
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public LieferscheinModel()
{
}
}
}
解决方案
您不需要正则表达式来匹配年份。像这样构建你的管道:
var collection = new MongoClient("mongodb://localhost")
.GetDatabase("test")
.GetCollection<BsonDocument>("LieferscheinModel");
var year = "2018";
PipelineStageDefinition<BsonDocument, BsonDocument> matchStage = $@"
{{
$match: {{
$expr: {{
$eq: [
{{
$year: {{
$toDate: ""$Bestelldatum""
}}
}},
{year}
]
}}
}}
}}";
PipelineStageDefinition<BsonDocument, BsonDocument> groupStage = @"
{
$group: {
_id: ""$Hv"",
uniqueIds: {
$addToSet: ""$_id""
},
count: {
$sum: 1
}
}
}";
PipelineStageDefinition<BsonDocument, BsonDocument> sortStage = @"
{
$sort: {
count: -1
}
}";
var pipeline = new[] { matchStage, groupStage, sortStage };
var result = collection.Aggregate<BsonDocument>(pipeline).ToList();
推荐阅读
- node.js - NodeJS https 模块创建可重用函数来动态发送请求和处理响应
- firebase - 按 UID 删除 Firebase 用户
- java - 将数据文件中的整数插入整数数组时,添加方法不起作用
- amazon-web-services - 自定义 AMI 和本地中的 AWS-Kinesis-Agent:请求中包含的安全令牌无效
- angular - 您如何编写用于 NodeJS 和 AWS CodeBuild 的 buildspec.yml?
- html - 底部对齐父 div 内的 2 个 div(同时将一个子 div 保持在另一个孩子的顶部)
- go - 如何使用 Nil Receiver 处理方法?
- node.js - 错误:当函数已经在异步函数中时,等待仅在异步函数中有效
- unit-testing - SAG webMethods WmTestSuite UnknownServiceException wm.ps.serviceMock:loadMock
- database - 类别的数据库结构,一个主要的,然后是次要的(没有子类别)