コード例 #1
0
        public static void Write(this BsonDocument document, string field, BsonValue value)
        {
            if (value == null)
            {
                return;
            }

            if (field.StartsWith("_"))
            {
                field = "PREFIX" + field;
            }
            // todo: make sure the search query builder also picks up this name change.

            bool forcearray = (value.BsonType == BsonType.Document);
            // anders kan er op zo'n document geen $elemMatch gedaan worden.

            BsonElement element;

            if (document.TryGetElement(field, out element))
            {
                if (element.Value.BsonType == BsonType.Array)
                {
                    element.Value.AsBsonArray.Add(value);
                }
                else
                {
                    document.Remove(field);
                    document.Append(field, new BsonArray()
                    {
                        element.Value, value ?? BsonNull.Value
                    });
                }
            }
            else
            {
                if (forcearray)
                {
                    document.Append(field, new BsonArray()
                    {
                        value ?? BsonNull.Value
                    });
                }
                else
                {
                    document.Append(field, value);
                }
            }
        }