Beispiel #1
0
        public Dictionary(object value)
        {
            if (Environment.GetEnvironmentVariable("MdbcDictionaryLegacy") == "0")
            {
                throw new InvalidOperationException("Used deprecated Mdbc.Dictionary(object)");
            }

            value = Actor.BaseObject(value);
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (value is Dictionary that)
            {
                _document = (BsonDocument)that._document.DeepClone();
            }
            else
            {
                var bson = Actor.ToBsonValue(value);
                if (bson.BsonType == BsonType.Document)
                {
                    _document = bson.AsBsonDocument;
                }
                else
                {
                    _document = new BsonDocument(BsonId.Element(bson));
                }
            }
        }
Beispiel #2
0
        public static BsonDocument NewDocumentWithId(bool newId, object id, object input)
        {
            if (newId && id != null)
            {
                throw new PSInvalidOperationException("Parameters Id and NewId cannot be used together.");
            }

            if (newId)
            {
                return(new BsonDocument(BsonId.Element(new BsonObjectId(ObjectId.GenerateNewId()))));
            }

            if (id == null)
            {
                return(null);
            }

            id = Actor.BaseObject(id);
            if (!(id is ScriptBlock sb))
            {
                return(new BsonDocument(BsonId.Element(BsonValue.Create(id))));
            }

            var arr = Actor.InvokeScript(sb, input);

            if (arr.Count != 1)
            {
                throw new ArgumentException("-Id script must return a single object.");                 //! use this type
            }
            return(new BsonDocument(BsonId.Element(BsonValue.Create(arr[0].BaseObject))));
        }
Beispiel #3
0
 public void EnsureId()
 {
     if (!_document.Contains(BsonId.Name))
     {
         _document.InsertAt(0, BsonId.Element(new BsonObjectId(ObjectId.GenerateNewId())));
     }
 }