public void Document_copies_properties_to_KeyValue_array() { // ARRANGE // create a Bson document with all possible value types var document = new BsonDocument(); document.Add("string", new BsonValue("string")); document.Add("bool", new BsonValue(true)); document.Add("objectId", new BsonValue(ObjectId.NewObjectId())); document.Add("DateTime", new BsonValue(DateTime.Now)); document.Add("decimal", new BsonValue((decimal)1)); document.Add("double", new BsonValue((double)1.0)); document.Add("guid", new BsonValue(Guid.NewGuid())); document.Add("int", new BsonValue((int)1)); document.Add("long", new BsonValue((long)1)); document.Add("bytes", new BsonValue(new byte[] { (byte)1 })); document.Add("bsonDocument", new BsonDocument()); // ACT // copy all properties to destination array var result = new KeyValuePair <string, BsonValue> [document.Count()]; document.CopyTo(result, 0); // ASSERT // all BsonValue instances have been added to the array by reference Assert.IsTrue(result.All(kv => object.ReferenceEquals(document.Get(kv.Key), kv.Value))); }
public virtual BsonDocument PreSave(BsonDocument doc, string[] references = null) { var tempId = doc.GetString("_id", ""); if (tempId == "" || tempId == "0" || doc.Get("_id") == BsonNull.Value || tempId == null) { doc.Set("_id", SequenceNo.Get(this.TableName).ClaimAsInt()); } return(doc); }
public static BsonDocument GetAsTree(this BsonDocument doc, string[] itemElementIdSources, Func <BsonDocument, BsonDocument> fnTransform, string itemELementIdTarget = "Items") { BsonDocument bdoc = new BsonDocument(); bdoc = fnTransform(doc); var docItems = new List <BsonDocument>(); foreach (var itemElementIdSource in itemElementIdSources) { if (doc.HasElement(itemElementIdSource) && doc.Get(itemElementIdSource).IsBsonArray) { var sourceItems = doc.Get(itemElementIdSource).AsBsonArray; foreach (var sourceItem in sourceItems) { var docItem = sourceItem.AsBsonDocument; docItems.Add(fnTransform(docItem)); } } } bdoc.Set(itemELementIdTarget, new BsonArray(docItems)); return(bdoc); }
public void Deserialize(BsonDocument data) { Damage = (float)data.Get(nameof(Damage).ToLower()).AsDouble; Speed = data.Get(nameof(Speed).ToLower()).AsInt32; }