private BsonDocument CreateDoc() { // create same object, but using BsonDocument var doc = new BsonDocument(); doc["_id"] = 123; doc["FirstString"] = "BEGIN this string \" has \" \t and this \f \n\r END"; doc["CustomerId"] = Guid.NewGuid(); doc["Date"] = DateTime.Now; doc["MyNull"] = null; doc["EmptyObj"] = new BsonDocument(); doc["EmptyString"] = ""; doc["maxDate"] = DateTime.MaxValue; doc["minDate"] = DateTime.MinValue; doc.Set("Customer.Address.Street", "Av. Cacapava"); doc["Items"] = new BsonArray(); doc["Items"].AsArray.Add(new BsonDocument()); doc["Items"].AsArray[0].AsDocument["Qtd"] = 3; doc["Items"].AsArray[0].AsDocument["Description"] = "Big beer package"; doc["Items"].AsArray[0].AsDocument["Unit"] = (double)10 / (double)3; doc["Items"].AsArray.Add("string-one"); doc["Items"].AsArray.Add(null); doc["Items"].AsArray.Add(true); doc["Items"].AsArray.Add(DateTime.Now); return doc; }
private BsonDocument CreateDoc() { // create same object, but using BsonDocument var doc = new BsonDocument(); doc["_id"] = 123; doc["FirstString"] = "BEGIN this string \" has \" \t and this \f \n\r END"; doc["CustomerId"] = Guid.NewGuid(); doc["Date"] = new DateTime(2015, 1, 1); doc["MyNull"] = null; doc["Items"] = new BsonArray(); doc["MyObj"] = new BsonDocument(); doc["EmptyString"] = ""; var obj = new BsonDocument(); obj["Qtd"] = 3; obj["Description"] = "Big beer package"; obj["Unit"] = 1299.995; doc["Items"].AsArray.Add(obj); doc["Items"].AsArray.Add("string-one"); doc["Items"].AsArray.Add(null); doc["Items"].AsArray.Add(true); doc["Items"].AsArray.Add(DateTime.Now); doc.Set("MyObj.IsFirstId", true); return doc; }
/// <summary> /// Recursive method for resolving nested DbRef documents. /// </summary> private void ResolveReferences(BsonDocument bson, string[] pathSegments, int segmentIndex) { var path = pathSegments[segmentIndex]; var value = bson[path]; if (value.IsNull) { return; } // if property value is an array, populate all values if (value.IsArray) { var array = value.AsArray; if (array.Count == 0) { return; } var docs = array.ToArray(); array.Clear(); foreach (var doc in docs) { var colRef = doc.AsDocument["$ref"]; var colId = doc.AsDocument["$id"]; var bsonValue = doc; if (!colRef.IsString || colId.IsNull) { if (pathSegments.Length == segmentIndex + 1) { // This might be the case when an include is being used *AFTER* another include starting // with the same path, but going deeper. throw LiteException.InvalidDbRef(CreateInvalidPathMessage(pathSegments, segmentIndex)); } // Not a reference document, so add the original array.Add(bsonValue); } else { // Resolve this document bsonValue = _engine.Value.Find(colRef, Query.EQ("_id", colId)).FirstOrDefault(); // include only object that exists in external ref if (bsonValue != null) { array.Add(bsonValue); } } if (bsonValue != null && segmentIndex < pathSegments.Length - 1) { ResolveReferences(bsonValue.AsDocument, pathSegments, segmentIndex + 1); } } } else if (value.IsDocument) { // for BsonDocument, get property value update with full object reference var doc = value.AsDocument; var colRef = doc["$ref"]; var colId = doc["$id"]; var bsonValue = doc; if (!colRef.IsString || colId.IsNull) { if (segmentIndex == pathSegments.Length - 1) { throw LiteException.InvalidDbRef(CreateInvalidPathMessage(pathSegments, segmentIndex)); } } else { // Reference document, so replace with real document bsonValue = _engine.Value.Find(colRef, Query.EQ("_id", colId)).FirstOrDefault(); bson.Set(path, bsonValue); } if (segmentIndex < pathSegments.Length - 1) { // Not the last property on the path, so resolve nested documents ResolveReferences(bsonValue, pathSegments, segmentIndex + 1); } } else { throw LiteException.InvalidDbRef(path); } }