Пример #1
0
        public async Task UpdateMultipleDocuments()
        {
            RequireServer.Where(minimumVersion: "2.6.0");
            AltersCollection();

            // @begin: update-multiple-documents
            // @code: start
            var collection = __database.GetCollection <BsonDocument>("restaurants");
            var builder    = Builders <BsonDocument> .Filter;
            var filter     = builder.Eq("address.zipcode", "10016") & builder.Eq("cuisine", "Other");
            var update     = Builders <BsonDocument> .Update
                             .Set("cuisine", "Category To Be Determined")
                             .CurrentDate("lastModified");

            var result = await collection.UpdateManyAsync(filter, update);

            // @code: end

            // @results: start
            result.MatchedCount.Should().Be(20);
            if (result.IsModifiedCountAvailable)
            {
                result.ModifiedCount.Should().Be(20);
            }
            // @results: end

            // @end: update-multiple-documents
        }
        public async Task ExecuteAsync_should_find_all_the_documents_matching_the_query_when_limit_is_used(
            [Values(1, 5, 6, 12)] int limit)
        {
            RequireServer.Where(versionLessThan: "3.2.0");
            var collectionNamespace = CoreTestConfiguration.GetCollectionNamespaceForTestMethod();

            for (var id = 1; id <= limit + 1; id++)
            {
                var document = new BsonDocument {
                    { "id", id }, { "filler", new string('x', 1000000) }
                };                                                                                        // about 1MB big
                var requests        = new[] { new InsertRequest(document) };
                var insertOperation = new BulkMixedWriteOperation(collectionNamespace, requests, new MessageEncoderSettings());
                ExecuteOperation(insertOperation);
            }
            var subject = new FindOpcodeOperation <BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Limit = limit
            };

            var cursor = await ExecuteOperationAsync(subject);

            var result = await ReadCursorToEndAsync(cursor);

            result.Should().HaveCount(limit);
        }
Пример #3
0
        public async Task UpdateTopLevelFields()
        {
            RequireServer.Where(minimumVersion: "2.6.0");
            AltersCollection();

            // @begin: update-top-level-fields
            // @code: start
            var collection = __database.GetCollection <BsonDocument>("restaurants");
            var filter     = Builders <BsonDocument> .Filter.Eq("name", "Juni");

            var update = Builders <BsonDocument> .Update
                         .Set("cuisine", "American (New)")
                         .CurrentDate("lastModified");

            var result = await collection.UpdateOneAsync(filter, update);

            // @code: end

            // @results: start
            result.MatchedCount.Should().Be(1);
            if (result.IsModifiedCountAvailable)
            {
                result.ModifiedCount.Should().Be(1);
            }
            // @results: end

            // @end: update-top-level-fields
        }
Пример #4
0
        public void Execute_should_throw_when_there_is_a_write_concern_error(
            [Values(false, true)] bool async)
        {
            RequireServer.Where(minimumVersion: "3.2.0-rc0", clusterTypes: ClusterTypes.ReplicaSet);
            EnsureTestData();
            var subject = new FindOneAndDeleteOperation <BsonDocument>(
                _collectionNamespace,
                BsonDocument.Parse("{ x : 1 }"),
                new FindAndModifyValueDeserializer <BsonDocument>(BsonDocumentSerializer.Instance),
                _messageEncoderSettings)
            {
                WriteConcern = new WriteConcern(9)
            };

            Action action = () => ExecuteOperation(subject, async);

            var exception     = action.ShouldThrow <MongoWriteConcernException>().Which;
            var commandResult = exception.Result;
            var result        = commandResult["value"].AsBsonDocument;

            result.Should().Be("{_id: 10, x: 1}");
            var serverList = ReadAllFromCollection(async);

            serverList.Should().BeEmpty();
        }
Пример #5
0
        public void Execute_should_return_expected_result(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Where(clusterTypes: ClusterTypes.StandaloneOrReplicaSet);
            EnsureTestData();
            var mapFunction    = "function() { emit(this.x, this.v); }";
            var reduceFunction = "function(key, values) { var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; }; return sum; }";
            var subject        = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, mapFunction, reduceFunction, _messageEncoderSettings)
            {
                BypassDocumentValidation = true
            };
            var expectedDocuments = new BsonDocument[]
            {
                new BsonDocument {
                    { "_id", 1 }, { "value", 3 }
                },
                new BsonDocument {
                    { "_id", 2 }, { "value", 4 }
                },
            };

            var response = ExecuteOperation(subject, async);

            response["ok"].ToBoolean().Should().BeTrue();

            var documents = ReadAllFromCollection(_outputCollectionNamespace, async);

            documents.Should().BeEquivalentTo(expectedDocuments);
        }
        public void StandardDeviationSample()
        {
            RequireServer.Where(minimumVersion: "3.1.9");

            var result = CreateQuery().Select(x => x.C.E.F).StandardDeviationSample();

            result.Should().BeApproximately(70.7106781186548, .0001);
        }
        public void StandardDeviationPopulation_with_selector()
        {
            RequireServer.Where(minimumVersion: "3.1.9");

            var result = CreateQuery().StandardDeviationPopulation(x => x.C.E.F);

            result.Should().Be(50);
        }
        public async Task StandardDeviationPopulationAsync_with_selector()
        {
            RequireServer.Where(minimumVersion: "3.1.9");

            var result = await CreateQuery().StandardDeviationPopulationAsync(x => x.C.E.F);

            result.Should().Be(50);
        }
        public void TestGetCurrentOp()
        {
            RequireServer.Where(clusterTypes: ClusterTypes.StandaloneOrReplicaSet);
            var adminDatabase = _server.GetDatabase("admin");
            var currentOp     = adminDatabase.GetCurrentOp();

            Assert.Equal("inprog", currentOp.GetElement(0).Name);
        }
        public async Task StandardDeviationSampleAsync_with_selector()
        {
            RequireServer.Where(minimumVersion: "3.1.9");

            var result = await CreateQuery().StandardDeviationSampleAsync(x => x.C.E.F);

            result.Should().BeApproximately(70.7106781186548, .0001);
        }
        public void Sample()
        {
            RequireServer.Where(minimumVersion: "3.1.9");
            var query = CreateQuery().Sample(100);

            Assert(query,
                   2,
                   "{ $sample: { size: 100 } }");
        }
        public void BitsAllSet_with_HasFlag()
        {
            RequireServer.Where(minimumVersion: "3.1.9");

            Assert(
                x => x.Q.HasFlag(Q.One),
                1,
                "{Q: { $bitsAllSet: 1 } }");
        }
        public void BitsAnySet_with_bitwise_operators()
        {
            RequireServer.Where(minimumVersion: "3.1.9");

            Assert(
                x => (x.C.E.F & 20) != 0,
                1,
                "{'C.E.F': { $bitsAnySet: 20 } }");
        }
        public void BitsAllClear_with_bitwise_operators()
        {
            RequireServer.Where(minimumVersion: "3.2.0");

            Assert(
                x => (x.C.E.F & 20) == 0,
                1,
                "{'C.E.F': { $bitsAllClear: 20 } }");
        }
        public void BitsAllSet_with_bitwise_operators()
        {
            RequireServer.Where(minimumVersion: "3.2.0");

            Assert(
                x => (x.C.E.F & 7) == 7,
                1,
                "{'C.E.F': { $bitsAllSet: 7 } }");
        }
        public void Select_method_embedded_pipeline()
        {
            RequireServer.Where(minimumVersion: "3.1.9");
            var query = CreateQuery().Select(x => x.M.First());

            Assert(query,
                   2,
                   "{ $project: { __fld0: { $arrayElemAt: ['$M', 0] }, _id: 0 } }");
        }
        public void Select_method_array_index()
        {
            RequireServer.Where(minimumVersion: "3.1.9");
            var query = CreateQuery().Select(x => x.M[0]);

            Assert(query,
                   2,
                   "{ $project: { __fld0: { $arrayElemAt: ['$M', 0] }, _id: 0 } }");
        }
        public void BitsAnyClear_with_bitwise_operators()
        {
            RequireServer.Where(minimumVersion: "3.1.9");

            Assert(
                x => (x.C.E.F & 7) != 7,
                1,
                "{'C.E.F': { $bitsAnyClear: 7 } }");
        }
        public void Should_translate_abs()
        {
            RequireServer.Where(minimumVersion: "3.1.6");
            var result = Project(x => new { Result = Math.Abs(x.C.E.F) });

            result.Projection.Should().Be("{ Result: { \"$abs\": \"$C.E.F\" }, _id: 0 }");

            result.Value.Result.Should().Be(11);
        }
        public void Should_translate_size_from_Count_property_on_Generic_ICollection()
        {
            RequireServer.Where(minimumVersion: "2.6.0");

            var result = Project(x => new { Result = x.L.Count });

            result.Projection.Should().Be("{ Result: { \"$size\": \"$L\" }, _id: 0 }");

            result.Value.Result.Should().Be(3);
        }
        public void Should_translate_set_difference_reversed()
        {
            RequireServer.Where(minimumVersion: "2.6.0");

            var result = Project(x => new { Result = new[] { "it", "not in here" }.Except(x.C.E.I) });

            result.Projection.Should().Be("{ Result: { \"$setDifference\": [[\"it\", \"not in here\"], \"$C.E.I\"] }, _id: 0 }");

            result.Value.Result.Should().Equal("not in here");
        }
        public void Select_method_computed_array()
        {
            RequireServer.Where(minimumVersion: "2.6.0");
            var query = CreateQuery()
                        .Select(x => x.M.Select(i => i + 1));

            Assert(query,
                   2,
                   "{ $project: { __fld0: { $map: { input: '$M', as: 'i', in: { $add: ['$$i', 1] } } }, _id: 0 } }");
        }
        public void Select_syntax_array_index()
        {
            RequireServer.Where(minimumVersion: "3.1.9");
            var query = from x in CreateQuery()
                        select x.M[0];

            Assert(query,
                   2,
                   "{ $project: { __fld0: { $arrayElemAt: ['$M', 0] }, _id: 0 } }");
        }
        public void Should_translate_set_union_reversed()
        {
            RequireServer.Where(minimumVersion: "2.6.0");

            var result = Project(x => new { Result = new[] { "it", "not in here" }.Union(x.C.E.I) });

            result.Projection.Should().Be("{ Result: { \"$setUnion\": [[\"it\", \"not in here\"], \"$C.E.I\"] }, _id: 0 }");

            result.Value.Result.Should().BeEquivalentTo("it", "icky", "not in here");
        }
Пример #25
0
        public void Should_translate_stdDevPop_with_selected_projector()
        {
            RequireServer.Where(minimumVersion: "3.1.7");

            var result = Group(x => 1, g => new { Result = g.Select(x => x.C.E.F).StandardDeviationPopulation() });

            result.Projection.Should().Be("{ _id: 1, Result: { \"$stdDevPop\": \"$C.E.F\" } }");

            result.Value.Result.Should().Be(50);
        }
Пример #26
0
        public void Should_translate_stdDevSamp_with_selected_projector()
        {
            RequireServer.Where(minimumVersion: "3.1.7");

            var result = Group(x => 1, g => new { Result = g.Select(x => x.C.E.F).StandardDeviationSample() });

            result.Projection.Should().Be("{ _id: 1, Result: { \"$stdDevSamp\": \"$C.E.F\" } }");

            result.Value.Result.Should().BeApproximately(70.7106781156545, .0001);
        }
        public void Should_translate_allElementsTrue()
        {
            RequireServer.Where(minimumVersion: "2.6.0");

            var result = Project(x => new { Result = x.G.All(g => g.E.F > 30) });

            result.Projection.Should().Be("{ Result: { \"$allElementsTrue\" : { \"$map\": { input: \"$G\", as: \"g\", in: { \"$gt\": [\"$$g.E.F\", 30 ] } } } }, _id: 0 }");

            result.Value.Result.Should().BeTrue();
        }
        public void Should_translate_set_intersection()
        {
            RequireServer.Where(minimumVersion: "2.6.0");

            var result = Project(x => new { Result = x.C.E.I.Intersect(new[] { "it", "not in here" }) });

            result.Projection.Should().Be("{ Result: { \"$setIntersection\": [\"$C.E.I\", [\"it\", \"not in here\"] ] }, _id: 0 }");

            result.Value.Result.Should().Equal("it");
        }
        public void Select_method_with_predicated_any()
        {
            RequireServer.Where(minimumVersion: "2.6.0");
            var query = CreateQuery()
                        .Select(x => x.G.Any(g => g.D == "Don't"));

            Assert(query,
                   2,
                   "{ $project: { __fld0: { $anyElementTrue: { $map: { input: '$G', as: 'g', 'in': { $eq: ['$$g.D', \"Don't\"] } } } }, _id: 0 } }");
        }
        public void Should_translate_set_is_subset()
        {
            RequireServer.Where(minimumVersion: "2.6.0");

            var result = Project(x => new { Result = x.L.IsSubsetOf(new[] { 1, 3, 5 }) });

            result.Projection.Should().Be("{ Result: { \"$setIsSubset\": [\"$L\", [1, 3, 5]] }, _id: 0 }");

            result.Value.Result.Should().BeTrue();
        }