public IEnumerable <AlarmNotification> GetNotificationDetails(IEnumerable <string> devicesIds) { BsonDocument unwindStage = new BsonDocument("$unwind", "$Devices"); BsonDocument matchStage = new BsonDocument("$match", new BsonDocument { { "Devices.Id", new BsonDocument("$in", new BsonArray(devicesIds)) } }); BsonDocument projectStage = new BsonDocument("$project", new BsonDocument { { "_id", 0 }, { "SiteId", "$Id" }, { "SiteName", "$Name" }, { "DeviceId", "$Devices.Id" }, { "DeviceName", "$Devices.Name" } }); PipelineDefinition <Site, BsonDocument> pipelineDefinition = PipelineDefinition <Site, BsonDocument> .Create(new List <BsonDocument> { unwindStage, matchStage, projectStage }); return(BsonIterator.Iterate <Site, IList <AlarmNotification> >(collection, pipelineDefinition, (e, items) => { if (items == null) { items = new List <AlarmNotification>(); } items.Add(JsonConvert.DeserializeObject <AlarmNotification>(e)); return items; })); }
public void TestFilteredDoc() { var doc = new BsonDocument(); doc["c"] = "d"; doc["aaa"] = 11; doc["ndoc"] = BsonDocument.ValueOf(new { aaaa = "nv1", d = "nv2", nnd = BsonDocument.ValueOf(new { nnv = true, nns = "s" }) }); doc["ndoc2"] = BsonDocument.ValueOf(new { n = "v" }); doc["f"] = "f"; BsonIterator it = new BsonIterator(doc); BsonDocument doc2 = it.ToBsonDocument("c", "ndoc.d", "ndoc.nnd.nns", "f"); Assert.AreEqual(3, doc2.KeysCount); Assert.AreEqual("d", doc2["c"]); Assert.AreEqual(2, ((BsonDocument)doc2["ndoc"]).KeysCount); Assert.AreEqual("nv2", ((BsonDocument)doc2["ndoc"])["d"]); Assert.AreEqual("s", ((BsonDocument)((BsonDocument)doc2["ndoc"])["nnd"])["nns"]); Assert.AreEqual("s", doc2["ndoc.nnd.nns"]); Assert.AreEqual("f", "f"); //Console.WriteLine("doc2=" + doc2); }
public Alarm GetLatestAlarmByType(string deviceId, Alarm.Type type, Alarm.Gravity?gravity) { IEnumerable <BsonDocument> pipeline = GetPipeline(deviceId, type, gravity); PipelineDefinition <Site, BsonDocument> pipelineDefinition = PipelineDefinition <Site, BsonDocument> .Create(pipeline); return(BsonIterator.Iterate(collection, pipelineDefinition, (BsonDocument e, Alarm item) => BsonSerializer.Deserialize <SingleBsonItem <Alarm> >(e).Item)); }
public Dictionary <string, Telemetry> GetLastTelemetryByDevice() { BsonDocument sortStage = new BsonDocument("$sort", new BsonDocument { { "OccuredAt", 1 } }); BsonDocument groupStage = new BsonDocument("$group", new BsonDocument { { "_id", "$DeviceId" }, { "Accumulator", new BsonDocument { { "$last", "$$ROOT" } } } }); PipelineDefinition <Telemetry, BsonDocument> pipelineDefinition = PipelineDefinition <Telemetry, BsonDocument> .Create(new List <BsonDocument> { sortStage, groupStage }); var elements = BsonIterator.Iterate(collection, pipelineDefinition, (BsonDocument e, IList <BsonGroupClass <Telemetry> > items) => { if (items == null) { items = new List <BsonGroupClass <Telemetry> >(); } items.Add(BsonSerializer.Deserialize <BsonGroupClass <Telemetry> >(e.ToBson())); return(items); }); return(elements.Where(e => e.Id != null).ToDictionary(k => k.Id, v => v.Accumulator)); }
public void TestIterate1() { var doc = new BsonDocument(); doc["a"] = "av"; doc["bb"] = 24; //doc["ccc"] = BsonDocument.ValueOf(new{na1 = 1, nb = "2"}); //doc["d"] = new ObjectId("51b9f3af98195c4600000000"); //17-00-00-00 +4 //02-61-00-03-00-00-00-61-76-00 +10 //10-62-62-00-18-00-00-00 +8 //00 +1 Assert.AreEqual("17-00-00-00-02-61-00-03-00-00-00-61-76-00-10-62-62-00-18-00-00-00-00", doc.ToDebugDataString()); BsonIterator it = new BsonIterator(doc); Assert.AreEqual(doc.ToByteArray().Length, it.DocumentLength); var c = ""; while (it.Next() != BsonType.EOO) { c += it.CurrentKey; } Assert.AreEqual("abb", c); it.Dispose(); it = new BsonIterator(doc); var cnt = 0; while (it.Next() != BsonType.EOO) { BsonValue bv = it.FetchCurrentValue(); Assert.IsNotNull(bv); if (cnt == 0) { Assert.IsTrue(bv.BsonType == BsonType.STRING); Assert.IsTrue(bv.Key == "a"); Assert.AreEqual("av", bv.Value); } if (cnt == 1) { Assert.IsTrue(bv.BsonType == BsonType.INT); Assert.IsTrue(bv.Key == "bb"); Assert.AreEqual(24, bv.Value); } cnt++; } }
public IList <Alarm> GetCrossAlarmsByDevice(string deviceId, DateTime start, DateTime end) { IEnumerable <BsonDocument> pipelineDef = GetCrossAlarmPipeline(deviceId, start, end); PipelineDefinition <Site, BsonDocument> pipelineDefinition = PipelineDefinition <Site, BsonDocument> .Create(pipelineDef); return(BsonIterator.Iterate(collection, pipelineDefinition, (BsonDocument e, IList <Alarm> items) => { if (items == null) { items = new List <Alarm>(); } items.Add(BsonSerializer.Deserialize <SingleBsonItem <Alarm> >(e).Item); return items; })); }
public Dictionary <string, IEnumerable <Freeze> > GetByDevice(IEnumerable <string> devicesIds = null, DateTime?from = null) { PipelineDefinition <Freeze, BsonDocument> pipelineDefinition = PipelineDefinition <Freeze, BsonDocument> .Create(GetPipeline(devicesIds, from)); return(BsonIterator.Iterate(collection, pipelineDefinition, (BsonDocument e, Dictionary <string, IEnumerable <Freeze> > items) => { if (items == null) { items = new Dictionary <string, IEnumerable <Freeze> >(); } var item = BsonSerializer.Deserialize <BsonGroup <string, IEnumerable <Freeze> > >(e); items.Add(item.Id, item.Accumulator); return items; })); }
public IEnumerable <Alarm> Get(DeviceAlarmFilter filter, int rowsPerPage, int pageNumber) { filter.DeviceId = string.Empty; IEnumerable <BsonDocument> pipeline = filter.SkipedAlarmsPipeline(rowsPerPage, pageNumber); PipelineDefinition <Site, BsonDocument> pipelineDefinition = PipelineDefinition <Site, BsonDocument> .Create(pipeline); return(BsonIterator.Iterate(collection, pipelineDefinition, (BsonDocument e, IList <Alarm> alarms) => { if (alarms == null) { alarms = new List <Alarm>(); } alarms.Add(BsonSerializer.Deserialize <BsonAlarmRoot>(e).Alarms); return alarms; })); }
public void TestIterate2() { var doc = new BsonDocument(); doc["a"] = "av"; doc["b"] = BsonDocument.ValueOf(new { cc = 1 }); var bsonOid = new ObjectId("51b9f3af98195c4600000000"); Console.WriteLine(bsonOid); doc["d"] = bsonOid; Assert.AreEqual(3, doc.KeysCount); //Console.WriteLine(doc.KeysCount); //Console.WriteLine(doc.ToDebugDataString()); //2E-00-00-00 +4 //02-61-00-03-00-00-00-61-76-00 +10 (14) //03-62-00 +3 (17) "d" = //0D-00-00-00 +4 (21) doc len = 13 //10-63-63-00-01-00-00-00 -00 +9 (30) //07-64-00 +3 (33) //51-B9-F3-AF-98-19-5C-46-00-00-00-00 +12 (45) //00 +1 (46) Assert.AreEqual("2E-00-00-00-" + "02-61-00-03-00-00-00-61-76-00-" + "03-62-00-" + "0D-00-00-00-" + "10-63-63-00-01-00-00-00-00-" + "07-64-00-" + "51-B9-F3-AF-98-19-5C-46-00-00-00-00-" + "00", doc.ToDebugDataString()); BsonIterator it = new BsonIterator(doc); int c = 0; foreach (var bt in it) { if (c == 0) { Assert.IsTrue(bt == BsonType.STRING); } if (c == 1) { Assert.IsTrue(bt == BsonType.OBJECT); } if (c == 2) { Assert.IsTrue(bt == BsonType.OID); } ++c; } bool thrown = false; Assert.IsTrue(it.Disposed); try { it.Next(); } catch (ObjectDisposedException) { thrown = true; } Assert.IsTrue(thrown); c = 0; it = new BsonIterator(doc); foreach (var bv in it.Values()) { if (c == 0) { Assert.AreEqual("a", bv.Key); Assert.AreEqual("av", bv.Value); } if (c == 1) { Assert.AreEqual("b", bv.Key); BsonDocument sdoc = bv.Value as BsonDocument; Assert.IsNotNull(sdoc); foreach (var bv2 in new BsonIterator(sdoc).Values()) { Assert.AreEqual("cc", bv2.Key); Assert.AreEqual(1, bv2.Value); Assert.AreEqual(BsonType.INT, bv2.BsonType); } } if (c == 2) { Assert.AreEqual(BsonType.OID, bv.BsonType); Assert.IsInstanceOf(typeof(ObjectId), bv.Value); var oid = bv.Value is ObjectId ? (ObjectId)bv.Value : new ObjectId(); Assert.AreEqual("51b9f3af98195c4600000000", oid.ToString()); } c++; } }