Example #1
0
        /// <summary>
        /// Authenticates the specified connection.
        /// </summary>
        /// <param retval="connection">The connection.</param>
        /// <returns></returns>
        protected bool Authenticate(IConnection connection)
        {
            if (string.IsNullOrEmpty(ConnectionString.UserName))
            {
                return true;
            }

            var nonce = new MongoCollection<GetNonceResponse>("$cmd", new MongoDatabase("admin", connection), connection).FindOne(new { getnonce = 1 });

            if (nonce.WasSuccessful)
            {
                var result = new QueryMessage<GenericCommandResponse, AuthenticationRequest>(connection, string.Concat(connection.Database, ".$cmd"))
                {
                    NumberToTake = 1,
                    Query = new AuthenticationRequest
                    {
                        User = connection.UserName,
                        Nonce = nonce.Nonce,
                        Key = connection.Digest(nonce.Nonce),
                    }
                }.Execute();

                return result.Results.Count() == 1 && result.Results.ElementAt(0).WasSuccessful;
            }

            return false;
        }
Example #2
0
 public UpdateModifiersTests()
 {
     var admin = new MongoAdmin("mongodb://localhost/admin?pooling=false&strict=true");
     _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false&strict=true");
     _collection = _server.GetCollection<Post>("Posts");
     _buildInfo = admin.BuildInfo();
 }
        /// <summary>
        /// Demo inserting multiple document into collection.
        /// </summary>
        /// <param name="orders">The orders.</param>
        private static void CreateAndInsertMultipleDocumentIntoCollection(MongoCollection<Order> orders)
        {
            Console.WriteLine("\n\n======= Insert Multiple Documents =======");
            Console.WriteLine(string.Format("Document Count Before Insert: [ {0} ]", orders.Count()));

            // Create new orders.
            var order1 = new Order
                         	{
                         			OrderAmount = 100.23,
                         			CustomerName = "Bugs Bunny"
                         	};
            var order2 = new Order
                         	{
                         			OrderAmount = 0.01,
                         			CustomerName = "Daffy Duck"
                         	};

            IEnumerable<Order> orderList = new List<Order> { order1, order2 };

            // Insert an IEnumerable.
            orders.Insert(orderList);

            Console.WriteLine(string.Format("Inserted: {0}", order1));
            Console.WriteLine(string.Format("Inserted: {0}", order2));

            Console.WriteLine(string.Format("Document Count After Insert: [ {0} ]", orders.Count()));
        }
Example #4
0
 public QueryTests()
 {
     _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
     _collection = _server.GetCollection<Person>("People");
     //cause the collection to exist on the server by inserting, then deleting some things.
     _collection.Insert(new Person());
     _collection.Delete(new { });
 }
Example #5
0
 public QueryTests()
 {
     var admin = new MongoAdmin("mongodb://localhost/admin?pooling=false&strict=true");
     _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
     _collection = _server.GetCollection<Person>("People");
     _buildInfo = admin.BuildInfo();
     //cause the collection to exist on the server by inserting, then deleting some things.
     _collection.Insert(new Person());
     _collection.Delete(new { });
 }
        /// <summary>
        /// Demo deleting documents from collection.
        /// </summary>
        /// <param name="orders">The orders.</param>
        private static void DeleteAllDocumentsFromCollection(MongoCollection<Order> collection)
        {
            Console.WriteLine("\n\n======= Delete Documents =======");
            Console.WriteLine(string.Format("Document Count Before Delete: [ {0} ]", collection.Count()));

            // Delete documents matching a criteria.
            collection.Delete( new {CustomerName = "Elmer Fudd"} );
            Console.WriteLine(string.Format("Document Count After Deleting Elmer Fudd: [ {0} ]", collection.Count()));

            // Delete all docs.
            collection.Delete( new {} );

            Console.WriteLine("Deleted all docs");
            Console.WriteLine(string.Format("Document Count After Deleting All Docs: [ {0} ]", collection.Count()));
        }
        /// <summary>
        /// Inserts a new document.
        /// </summary>
        /// <param name="collection">The orders.</param>
        private static void CreateAndInsertSingleDocumentIntoCollection(MongoCollection<Order> collection)
        {
            Console.WriteLine("\n\n======= Insert Multiple Documents =======");
            Console.WriteLine(string.Format("Document Count Before Insert: [ {0} ]", collection.Count()));

            // Create a new order.
            var order = new Order()
                            {
                                    OrderAmount = 57.22,
                                    CustomerName = "Elmer Fudd"
                            };

            // Add the new order to the mongo orders colleciton.
            collection.Insert(order);
            Console.WriteLine(string.Format("Inserted: {0}", order));

            Console.WriteLine(string.Format("Document Count After Insert: [ {0} ]", collection.Count()));
        }
Example #8
0
 public UpdateTests()
 {
     _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
     _collection = _server.GetCollection<CheeseClubContact>("CheeseClubContacts");
 }
Example #9
0
 public WhereQualifierTests()
 {
     _server = Mongo.Create("mongodb://localhost/NormTests?pooling=false");
     _collection = _server.GetCollection<TestClass>("TestClasses");
 }
        /// <summary>
        /// Demoes Updates a single field using an atomic Increment ($inc) operator.
        /// </summary>
        /// <param name="orders">The orders.</param>
        private static void UpdateUsingAtomicIncrement( MongoCollection< Order > orders )
        {
            Console.WriteLine("\n\n======= Update Document using Increment =======");

            var selector = new { CustomerName = "Daffy Duck" };
            Console.WriteLine("Before Update: " + orders.FindOne(selector));

            // Add 2000 to order amount on document matching selector.
            orders.Update( selector, x=>x.Increment( o=>o.OrderAmount, 2000 ) );

            Console.WriteLine("After Update: " + orders.FindOne(selector));
        }
        /// <summary>
        /// Demo of Updating a document.
        /// </summary>
        /// <param name="orders">The orders.</param>
        private static void UpdateDocument(MongoCollection<Order> orders)
        {
            Console.WriteLine("\n\n======= Update Documents =======");
            var selector = new {CustomerName = "Daffy Duck"};
            var orderToUpdate = orders.FindOne(selector);

            Console.WriteLine("Before Update: " + orderToUpdate);

            // I'm in the money!
            orderToUpdate.OrderAmount = 1000000.00;

            // Update Daffy's account before Hasaan finds him.
            orders.Save( orderToUpdate );

            Console.WriteLine("After Update: " + orders.FindOne(selector));
        }
        /// <summary>
        /// Demos querying the collection.
        /// </summary>
        /// <param name="orders">The orders.</param>
        private static void QueryFromCollection(MongoCollection<Order> orders)
        {
            Console.WriteLine("\n\n======= Query One Document =======");

            // Create a specification to query the orders collection.

            // Run the query.
            Order result = orders.FindOne(new {});
            Console.WriteLine(string.Format("Found: {0}", result));
        }
        private static void QueryConditionalOperators( MongoCollection< Order > collection )
        {
            Console.WriteLine("\n\n======= Query with Conditional Operators (Using Anonymous Objects) =======");
            var ordersWithAmountGreaterThan50 = collection.Find( new {OrderAmount = Q.GreaterThan( 50 )} );
            var ordersWithAmountGreaterThanOEqualTo50 = collection.Find( new {OrderAmount = Q.GreaterOrEqual( 50 )} );
            var ordersWithAmountLessThan100 = collection.Find( new {OrderAmount = Q.LessThan( 100 )} );
            var ordersWithAmountLessThanOEqualTo100 = collection.Find( new {OrderAmount = Q.LessOrEqual( 100 )} );
            var ordersWithAmountBetween50And200 = collection.Find( new {OrderAmount = Q.GreaterThan( 50 ).And(Q.LessThan( 200 ))} );
            var ordersWithAmountNotEqualTo100 = collection.Find( new {OrderAmount = Q.NotEqual( 100 )} );
            var ordersWithAmountInList = collection.Find( new {CustomerName = Q.In( "Elmer Fudd", "Daffy Duck" )} );

            Console.WriteLine("\nOrders with amount greater than 50:");
            foreach ( var order in ordersWithAmountGreaterThan50 )
                Console.WriteLine(order.ToString());

            Console.WriteLine("\nOrders with amount greater than or equal to 50:");
            foreach ( var order in ordersWithAmountGreaterThanOEqualTo50 )
                Console.WriteLine(order.ToString());

            Console.WriteLine("\nOrders with amount less than 100:");
            foreach ( var order in ordersWithAmountLessThan100 )
                Console.WriteLine(order.ToString());

            Console.WriteLine("\nOrders with amount less than or equal to 100:");
            foreach ( var order in ordersWithAmountLessThanOEqualTo100 )
                Console.WriteLine(order.ToString());

            Console.WriteLine("\nOrders with amount between 50 and 200:");
            foreach ( var order in ordersWithAmountBetween50And200 )
                Console.WriteLine(order.ToString());

            Console.WriteLine("\nOrders with amount not equal to 100:");
            foreach (var order in ordersWithAmountNotEqualTo100)
                Console.WriteLine(order.ToString());

            Console.WriteLine("\nOrders with amount in list:");
            foreach (var order in ordersWithAmountInList)
                Console.WriteLine(order.ToString());
        }
 public MongoSearchQualifierTests()
 {
     _server = Mongo.Create(TestHelper.ConnectionString("pooling=false"));
     _coll = _server.GetCollection<TestClass>("TestClasses");
 }