コード例 #1
0
 public static bool Exists(this BsonValue bsonValue, string name)
 {
     return(bsonValue.Find(name) != BsonNull.Value);
 }
コード例 #2
0
        public static BsonValue Update(this BsonValue bsonValue, string name, BsonValue value)
        {
            var fieldNames = name.Split('.');

            // Value to set on next level
            BsonValue valueForNextLevel = value;

            for (int i = 0; i < fieldNames.Length; i++)
            {
                var fieldName = fieldNames[fieldNames.Length - 1 - i];
                if (fieldName == "$")
                {
                    fieldName = "0";
                }

                var valueToTake        = string.Join(".", fieldNames.Take(fieldNames.Length - 1 - i));
                var valueOfHigherLevel = string.IsNullOrEmpty(valueToTake) ? bsonValue : bsonValue.Find(valueToTake);
                if (valueOfHigherLevel.IsBsonDocument)
                {
                    valueForNextLevel = valueOfHigherLevel.AsBsonDocument.Set(fieldName, valueForNextLevel);
                }
                else if (valueOfHigherLevel.IsBsonArray)
                {
                    int fieldIndex = int.Parse(fieldName);
                    valueOfHigherLevel.AsBsonArray[fieldIndex] = valueForNextLevel;
                    valueForNextLevel = valueOfHigherLevel;
                }
            }

            return(valueForNextLevel);
        }