Пример #1
0
        /// <summary>
        /// Updates or inserts 1 document on the server. Inspect CRUDResult for write errors and docs affected/matched
        /// </summary>
        public CRUDResult Save(BSONDocument document)
        {
            EnsureObjectNotDisposed();

            if (document == null)
            {
                throw new MongoDbConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.Save(document==null)");
            }

            var _id = document[Protocol._ID];

            if (_id == null || _id is BSONNullElement)
            {
                throw new MongoDbConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.Save(document._id absent|null)");
            }

            var connection = Server.AcquireConnection();

            try
            {
                var reqId   = Database.NextRequestID;
                var updates = new UpdateEntry[] { new UpdateEntry(
                                                      new Query("{'" + Protocol._ID + "': '$$ID'}", true, new TemplateArg("ID", _id.ElementType, _id.ObjectValue)),
                                                      document,
                                                      multi: false,
                                                      upsert: true
                                                      ) };

                return(connection.Update(reqId, this, updates));
            }
            finally
            {
                connection.Release();
            }
        }
Пример #2
0
        /// <summary>
        /// Executes a NOP command that round-trips from the server
        /// </summary>
        public void Ping()
        {
            EnsureObjectNotDisposed();

            var connection = Server.AcquireConnection();

            try
            {
                var requestID = NextRequestID;
                connection.Ping(requestID, this);
            }
            finally
            {
                connection.Release();
            }
        }
Пример #3
0
        /// <summary>
        /// Returns the names of collections in this database
        /// </summary>
        public string[] GetCollectionNames()
        {
            EnsureObjectNotDisposed();

            var connection = Server.AcquireConnection();

            try
            {
                var requestID = NextRequestID;
                return(connection.GetCollectionNames(requestID, this));
            }
            finally
            {
                connection.Release();
            }
        }
Пример #4
0
        /// <summary>
        /// Drops the collection from the server with all of its data and disposes the Collection object
        /// </summary>
        public void Drop()
        {
            EnsureObjectNotDisposed();

            var connection = Server.AcquireConnection();

            try
            {
                var reqId = Database.NextRequestID;
                connection.Drop(reqId, this);
                this.Dispose();
            }
            finally
            {
                connection.Release();
            }
        }
Пример #5
0
        /// <summary>
        /// Runs database-level command. Does not perform any error checks beyond network traffic req/resp passing
        /// </summary>
        public BSONDocument RunCommand(BSONDocument command)
        {
            EnsureObjectNotDisposed();

            if (command == null)
            {
                throw new MongoDbConnectorException(StringConsts.ARGUMENT_ERROR + "RunCommand(command==null)");
            }

            var connection = Server.AcquireConnection();

            try
            {
                var requestID = NextRequestID;
                return(connection.RunCommand(requestID, this, command));
            }
            finally
            {
                connection.Release();
            }
        }
Пример #6
0
        /// <summary>
        /// Finds a document that satisfied query or null
        /// </summary>
        public BSONDocument FindOne(Query query, BSONDocument selector = null)
        {
            EnsureObjectNotDisposed();

            if (query == null)
            {
                throw new MongoDbConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.FindOne(query==null)");
            }

            var connection = Server.AcquireConnection();

            try
            {
                var reqId = Database.NextRequestID;
                return(connection.FindOne(reqId, this, query, selector));
            }
            finally
            {
                connection.Release();
            }
        }
Пример #7
0
        /// <summary>
        /// Performs server-side count over cursor
        /// </summary>
        /// <param name="query">Optional. A query that selects which documents to count in a collection</param>
        /// <param name="limit">Optional. The maximum number of matching documents to return</param>
        /// <param name="skip">Optional. The number of matching documents to skip before returning results</param>
        /// <param name="hint">Optional. The index to use. Specify either the index name as a string or the index specification document.</param>
        /// <returns>Count </returns>
        public long Count(Query query = null, int limit = -1, int skip = -1, object hint = null)
        { // See http://docs.mongodb.org/v3.0/reference/command/count/
            EnsureObjectNotDisposed();

            if (hint != null && (!(hint is string)) && (!(hint is BSONDocument)))
            {
                throw new MongoDbConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.Count(hint must be string | BSONDocument)");
            }

            var connection = Server.AcquireConnection();

            try
            {
                var reqId = Database.NextRequestID;
                return(connection.Count(reqId, this, query, limit, skip, hint));
            }
            finally
            {
                connection.Release();
            }
        }
Пример #8
0
        /// <summary>
        /// Deletes documents from the server. Inspect CRUDResult for write errors and docs affected/matched
        /// </summary>
        public CRUDResult Delete(params DeleteEntry[] deletes)
        {
            EnsureObjectNotDisposed();

            if (deletes == null || deletes.Length < 1)
            {
                throw new MongoDbConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.Delete(deletes==null|empty)");
            }

            var connection = Server.AcquireConnection();

            try
            {
                var reqId = Database.NextRequestID;
                return(connection.Delete(reqId, this, deletes));
            }
            finally
            {
                connection.Release();
            }
        }
Пример #9
0
        /// <summary>
        /// Inserts documents on the server. Inspect CRUDResult for write errors and docs affected/matched
        /// </summary>
        public CRUDResult Insert(params BSONDocument[] documents)
        {
            EnsureObjectNotDisposed();

            if (documents == null || documents.Length < 1)
            {
                throw new MongoDbConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.Insert(documents==null|empty)");
            }

            var connection = Server.AcquireConnection();

            try
            {
                var reqId = Database.NextRequestID;
                return(connection.Insert(reqId, this, documents));
            }
            finally
            {
                connection.Release();
            }
        }
Пример #10
0
        /// <summary>
        /// Finds all documents that match the supplied query, optionally skipping some.
        /// Fetches only fetchBy at once, then lazily fetches via cursor
        /// </summary>
        /// <param name="query">Query to match against</param>
        /// <param name="skipCount">How many document sto skip at the beginning</param>
        /// <param name="fetchBy">The size of fetch block</param>
        /// <param name="selector">Optional field mapping document like: {"field_name": 1}</param>
        /// <returns>An iterable cursor</returns>
        public Cursor Find(Query query, int skipCount = 0, int fetchBy = 0, BSONDocument selector = null)
        {
            EnsureObjectNotDisposed();

            if (query == null)
            {
                throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR + "Collection.Find(query==null)");
            }

            var connection = Server.AcquireConnection();

            try
            {
                var reqId = Database.NextRequestID;
                return(connection.Find(reqId, this, query, selector, skipCount, fetchBy));
            }
            finally
            {
                connection.Release();
            }
        }