【发布时间】:2019-11-04 15:58:05
【问题描述】:
我正在尝试访问深度嵌套的 mongodb 结构,并希望使用 c# 更新和读取该结构。我想更新 UUT 数组中的 UUT 对象以及 UUT 历史数组。
我正在尝试更新 UUTId 的值:TS220,时钟周期:2000。
我知道一个覆盖完整文档的选项,但我不知道如何访问 UUTId 对象并为时钟周期插入一个值。
请使用 c# MongoDB.Driver 回答 提前致谢。
MongoClient mongoClient = new MongoClient("mongodb://server.net:27017");
IMongoDatabase db = mongoClient.GetDatabase("enduranceTest");
IMongoCollection<ServerTreeVm> collectionEndurance = db.GetCollection<ServerTreeVm>("EnduranceTests");
var filterVM = Builders<ServerTreeVm>.Filter;
var enduranceTestFound = filterVM.And(
Builders<ServerTreeVm>.Filter.Where(x => x.Countries.Any(y =>
y.Cities.Any(z => z.EnduranceTests.Any(a => a.EnduranceTestName == "Endurance 1")))),
Builders<ServerTreeVm>.Filter.Where(x => x.Countries.Any(y =>
y.Cities.Any(z => z.EnduranceTests.Any(a => a.EnduranceInfo.Any(b=>b.Title== "Taktverteiler_1"))))),
Builders<ServerTreeVm>.Filter.Where(x => x.Countries.Any(y =>
y.Cities.Any(z => z.EnduranceTests.Any(a => a.EnduranceInfo.Any(b => b.Channels.Any(c=>c.Uuts.Any(d=>d.UutId == "TS220"))))))));
var enduranceTest = collectionEndurance.Find(enduranceTestFound).SingleOrDefault();
//update with positional operator
var update = Builders<ServerTreeVm>.Update;
UUT uutObj = new UUT
{
Status = "Running",
UutId = "TS220",
ClockingCycles = 2000,
Offset = 100,
StartCycles = 0,
Timestamp = DateTime.Now,
UUTHistory = null
};
var enduranceUpdate = update.Set("Countries.$.Cities.$.EnduranceTest.$.ClockDistributor.$.Channels.$.UUTs.$", uutObj);
collectionEndurance.UpdateOne(enduranceTestFound, enduranceUpdate);
所以我在 C# ServerTreeVM 模型中从 mongodb 获取整个文档,然后我尝试使用位置运算符更新文档。但它没有更新文档。我不知道我在这里做错了什么。有什么提示或不同的方法吗?
{
"_id": {
"$oid": "5d5ff0be962f936368c28b1f"
},
"Countries": [
{
"CountryName": "Country1",
"Cities": [
{
"CityName": "City1",
"EnduranceTests": [
{
"EnduranceTestName": "endurance1",
"ClockDistributor": [
{
"IP": "192.168.2.6",
"Channels": [
{
"UUTs": [
{
"UUTId": "TS220",
"ClockingCycles": {
"$numberInt": "0"
},
"Status": "Steht",
"StartCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
},
"UUTHistory": [
{
"ClockingCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Status": "",
"StartCycles": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
}
}
]
},
{
"UUTId": "TS221",
"ClockingCycles": {
"$numberInt": "0"
},
"Status": "Steht",
"StartCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
},
"UUTHistory": [
{
"ClockingCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Status": "",
"StartCycles": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
}
}
]
}
],
"ChannelCycles": {
"$numberInt": "0"
},
"NodeName": ""
},
{
"ChannelCycles": {
"$numberInt": "0"
},
"UUTs": [
{
"UUTId": "TS230",
"ClockingCycles": {
"$numberInt": "0"
},
"Status": "Steht",
"StartCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
},
"UUTHistory": [
{
"ClockingCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Status": "",
"StartCycles": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
}
}
]
}
],
"NodeName": ""
},
{
"UUTs": [
{
"UUTId": "TS240",
"ClockingCycles": {
"$numberInt": "0"
},
"Status": "Steht",
"StartCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
},
"UUTHistory": [
{
"ClockingCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Status": "",
"StartCycles": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
}
}
]
}
],
"ChannelCycles": {
"$numberInt": "0"
},
"NodeName": ""
},
{
"ChannelCycles": {
"$numberInt": "0"
},
"UUTs": [
{
"UUTId": "TS250",
"ClockingCycles": {
"$numberInt": "0"
},
"Status": "Steht",
"StartCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
},
"UUTHistory": [
{
"ClockingCycles": {
"$numberInt": "0"
},
"Offset": {
"$numberInt": "0"
},
"Status": "",
"StartCycles": {
"$numberInt": "0"
},
"Timestamp": {
"$date": {
"$numberLong": "946681200000"
}
}
}
]
}
],
"NodeName": ""
}
],
"ConnectionType": "Opcua",
"Title": "Taktverteiler_1",
"Link": "http://192.168.0.6:8080/tv_webvisu.htm",
"PropertyNames": [
"P-Line",
"Modus",
"Status",
"Freq [Hz]",
"Verwendung"
],
"Nodes": [
"|var|CPX-CEC-S1-V3.Application.PersistentVars.ar_str_p1_p2",
"|var|CPX-CEC-S1-V3.Application.OPC_UA_Variablen_Dashboard.ar_str_Normal_VE_OPC_UA",
"|var|CPX-CEC-S1-V3.Application.OPC_UA_Variablen_Dashboard.ar_str_Status_OPC_UA",
"|var|CPX-CEC-S1-V3.Application.PersistentVars.ar_r_Frequenz_Kanal_X",
"|var|CPX-CEC-S1-V3.Application.OPC_UA_Variablen_Dashboard.ar_str_Verwendung"
],
"StartStopNodes": [
"|var|CPX-CEC-C1-V3.Application.GVL.b_Button_Stimulation_Starten_aktive_Kanaele",
"|var|CPX-CEC-C1-V3.Application.GVL.b_alle_Stopp"
]
}
]
}
]
}
]
}
]
}
[BsonIgnoreExtraElements]
public class ServerTreeVm
{
[BsonElement("Countries")]
public List<Country> Countries { get; set; }
public string ActiveEnduranceTest { get; set; }
public string ActiveDistributor { get; set; }
}
[BsonIgnoreExtraElements]
public class Country
{
[BsonElement("Cities")]
public List<City> Cities { get; set; }
[BsonElement("CountryName")]
public string CountryName { get; set; }
}
[BsonIgnoreExtraElements]
public class City
{
[BsonElement("EnduranceTests")]
public List<Endurance> EnduranceTests { get; set; }
[BsonElement("CityName")]
public string CityName { get; set; }
}
[BsonIgnoreExtraElements]
public class Endurance
{
[BsonElement("ClockDistributor")]
public List<EnduranceInformation> EnduranceInfo { get; set; }
[BsonElement("EnduranceTestName")]
public string EnduranceTestName { get; set; }
}
[BsonIgnoreExtraElements]
public class UUT
{
[BsonElement("UUTId")]
public string UutId { get; set; }
[BsonElement("ClockingCycles")]
public double ClockingCycles { get; set; }
[BsonElement("Status")]
public string Status { get; set; }
[BsonElement("StartCycles")]
public double StartCycles { get; set; }
[BsonElement("Offset")]
public double Offset { get; set; }
[BsonElement("Timestamp")]
public DateTime Timestamp { get; set; }
[BsonElement("UUTHistory")]
public List<UUTHistory> UUTHistory { get; set; }
}
[BsonIgnoreExtraElements]
public class UUTHistory
{
[BsonElement("ClockingCycles")]
public double ClockingCycles { get; set; }
[BsonElement("Status")]
public string Status { get; set; }
[BsonElement("StartCycles")]
public double StartCycles { get; set; }
[BsonElement("Offset")]
public double Offset { get; set; }
[BsonElement("Timestamp")]
public DateTime Timestamp { get; set; }
}
[BsonIgnoreExtraElements]
public class ChannelObjects
{
[BsonElement("UUTs")]
public List<UUT> Uuts { get; set; }
[BsonElement("ChannelCycles")]
public double ChannelCycles { get; set; }
[BsonElement("NodeName")]
public string NodeName { get; set; }
}
[BsonIgnoreExtraElements]
public class EnduranceInformation
{
[BsonElement("IP")]
public string Ip { get; set; }
[BsonElement("Title")]
public string Title { get; set; }
[BsonElement("Link")]
public string Link { get; set; }
[BsonElement("PropertyNames")]
public List<string> PropertyNames { get; set; }
[BsonElement("Nodes")]
public List<string> Nodes { get; set; }
[BsonElement("Channels")]
public List<ChannelObjects> Channels { get; set; }
[BsonElement("ConnectionType")]
public String ConnectionType { get; set; }
[BsonElement("StartStopNodes")]
public List<string> StartStopNodeNames { get; set; }
}
【问题讨论】:
-
MongoDB C# 驱动程序以 BsonDocument() 类型返回数据。您有自定义模型。您尚未指定用于在 MongoDB 驱动程序和自定义模型之间进行序列化/反序列化的框架。您的 DAL 使用什么?
-
顺便说一句,批量更新文档(即替换整个文档以更新单个字段)成本高且性能不佳。
-
我使用来自 Nuget 包的官方 C# Mongodb 驱动程序。链接:github.com/mongodb/mongo-csharp-driver