protected MapReduceOperationBase(
     string collectionName,
     string databaseName,
     BsonJavaScript finalizeFunction,
     bool? javaScriptMode,
     long? limit,
     BsonJavaScript mapFunction,
     BsonDocument query,
     BsonJavaScript reduceFunction,
     BsonDocument scope,
     BsonDocument sort,
     bool? verbose)
 {
     _collectionName = collectionName;
     _databaseName = databaseName;
     _finalizeFunction = finalizeFunction;
     _javaScriptMode = javaScriptMode;
     _limit = limit;
     _mapFunction = mapFunction;
     _query = query;
     _reduceFunction = reduceFunction;
     _scope = scope;
     _sort = sort;
     _verbose = verbose;
 }
Esempio n. 2
0
 private void cmdEval_Click(object sender, EventArgs e)
 {
     MongoDatabase mongoDB = SystemManager.GetCurrentDataBase();
     BsonJavaScript js = new BsonJavaScript(txtevalJs.Text);
     List<Object> Params = new List<Object>();
     if (txtParm.Text != String.Empty) {
         foreach (String parm in txtParm.Text.Split(",".ToCharArray())) {
             if (parm.StartsWith("'") & parm.EndsWith("'"))
             {
                 Params.Add(parm);
             }
             else {
                 //TODO:检查数字型
                 try
                 {
                     Params.Add(Convert.ToInt16(parm));
                 }
                 catch (Exception ex)
                 {
                     MyMessageBox.ShowMessage("异常", "参数异常", ex.ToString(), true);
                 }
             }
         }
     }
     try
     {
         BsonValue result = mongoDB.Eval(js, Params.ToArray());
         MyMessageBox.ShowMessage("结果", "执行结果", MongoDBHelper.GetBsonElementText("Result",result,0), true);
     }
     catch (Exception ex)
     {
          MyMessageBox.ShowMessage("异常","执行异常",ex.ToString(),true);
     }
 }
 public void TestAsBsonJavaScript() {
     BsonValue v = new BsonJavaScript("code");
     BsonValue s = "";
     var js = v.AsBsonJavaScript;
     Assert.AreEqual("code", js.Code);
     Assert.Throws<InvalidCastException>(() => { var x = s.AsBsonJavaScript; });
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MapReduceOperationBase"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="mapFunction">The map function.</param>
 /// <param name="reduceFunction">The reduce function.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 protected MapReduceOperationBase(CollectionNamespace collectionNamespace, BsonJavaScript mapFunction, BsonJavaScript reduceFunction, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
     _mapFunction = Ensure.IsNotNull(mapFunction, nameof(mapFunction));
     _reduceFunction = Ensure.IsNotNull(reduceFunction, nameof(reduceFunction));
     _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
 }
 public void TestBsonJavaScriptEquals() {
     BsonJavaScript lhs = new BsonJavaScript("n = 1");
     BsonJavaScript rhs = new BsonJavaScript("n = 1");
     Assert.AreNotSame(lhs, rhs);
     Assert.AreEqual(lhs, rhs);
     Assert.AreEqual(lhs.GetHashCode(), rhs.GetHashCode());
 }
        public void constructor_should_throw_when_databaseNamespace_is_null()
        {
            var function = new BsonJavaScript("return 1");

            Action action = () => new EvalOperation(null, function, _messageEncoderSettings);

            action.ShouldThrow<ArgumentNullException>();
        }
 // constructors
 protected MapReduceOperationBase(string databaseName, string collectionName, BsonJavaScript mapFunction, BsonJavaScript reduceFunction, BsonDocument query = null)
 {
     _databaseName = Ensure.IsNotNullOrEmpty(databaseName, "databaseName");
     _collectionName = Ensure.IsNotNullOrEmpty(collectionName, "collectionName");
     _mapFunction = Ensure.IsNotNull(mapFunction, "mapFunction");
     _reduceFunction = Ensure.IsNotNull(reduceFunction, "reduceFunction");
     _query = query; // can be null
 }
 // constructors
 protected MapReduceOperationBase(CollectionNamespace collectionNamespace, BsonJavaScript mapFunction, BsonJavaScript reduceFunction, BsonDocument query, MessageEncoderSettings messageEncoderSettings)
 {
     _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
     _mapFunction = Ensure.IsNotNull(mapFunction, "mapFunction");
     _reduceFunction = Ensure.IsNotNull(reduceFunction, "reduceFunction");
     _query = query;
     _messageEncoderSettings = messageEncoderSettings;
 }
Esempio n. 9
0
        public void Aggregate_with_map_reduce()
        {
            var dataBase = new MongoClient("mongodb://localhost").GetServer().GetDatabase("MapReduce");
            var sessions = dataBase.GetCollection<Session>("Sessions");

            BsonJavaScript map = new BsonJavaScript(Scipts.map);
            BsonJavaScript reduce = new BsonJavaScript(Scipts.reduce);
            var result = sessions.MapReduce(map, reduce);
        }
        public void Args_should_work()
        {
            var function = new BsonJavaScript("return 1");
            var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings);
            var args = new BsonValue[] { 1, 2, 3 };

            subject.Args = args;

            subject.Args.Should().Equal(args);
        }
Esempio n. 11
0
    public void Seed()
    {
      var db = CreateDatabase();

      foreach (var seedDefinition in Description.Seeds)
      {
        var command = new BsonJavaScript(seedDefinition.Load());
        db.Eval(command);
      }
    }
Esempio n. 12
0
        private ICollection<KeyValuePair<string, int>> GetPageViewsByUserAgent(IMongoQuery query)
        {
            var reduce = new BsonJavaScript("function(o, agg) { agg.count++; }");
            var results = DB.PageViews.Group(query, "UserAgent", new { count = 0 }.ToBsonDocument(), reduce, null).ToList();

            return results
                .ToKeyValuePairs(x => x["UserAgent"].AsString, x => x["count"].ToInt32())
                .OrderByDescending(kvp => kvp.Value)
                .ToList();
        }
Esempio n. 13
0
 public static void Eval(MongoDatabase database, string fileName)
 {
     using (var stream = new FileStream(fileName, FileMode.Open))
     {
         using (var reader = new StreamReader(stream))
         {
             var javaScript = new BsonJavaScript(reader.ReadToEnd());
             database.Eval(javaScript);
         }
     }
 }
Esempio n. 14
0
        public void constructor_with_key_should_throw_when_key_is_null()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var initial = new BsonDocument("x", 1);
            var reduceFunction = new BsonJavaScript("reduceFunction");
            var filter = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();

            Action action = () => new GroupOperation<BsonDocument>(collectionNamespace, (BsonDocument)null, initial, reduceFunction, filter, messageEncoderSettings);

            action.ShouldThrow<ArgumentNullException>();
        }
Esempio n. 15
0
 /// <summary>
 ///     运行
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cmdRun_Click(object sender, EventArgs e)
 {
     var map = new BsonJavaScript(ctlMapFunction.Context);
     var reduce = new BsonJavaScript(ctlReduceFunction.Context);
     //TODO:这里可能会超时,失去响应
     //需要设置SocketTimeOut
     MapReduceResult mMapReduceResult = SystemManager.GetCurrentCollection().MapReduce(map, reduce);
     MongoDbHelper.FillDataToTreeView("MapReduce Result", trvResult, mMapReduceResult.Response);
     trvResult.DatatreeView.BeginUpdate();
     trvResult.DatatreeView.ExpandAll();
     trvResult.DatatreeView.EndUpdate();
 }
Esempio n. 16
0
 /// <summary>
 /// 运行
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cmdRun_Click(object sender, EventArgs e)
 {
     BsonJavaScript map = new BsonJavaScript(txtMapJs.Text);
     BsonJavaScript reduce = new BsonJavaScript(txtReduceJs.Text);
     //TODO:这里可能会超时,失去响应
     //需要设置SocketTimeOut
     MapReduceResult mMapReduceResult = _mongocol.MapReduce(map, reduce);
     MongoDBHelper.FillDataToTreeView("MapReduce Result", trvResult, mMapReduceResult.Response);
     trvResult.DatatreeView.BeginUpdate();
     trvResult.DatatreeView.ExpandAll();
     trvResult.DatatreeView.EndUpdate();
 }
Esempio n. 17
0
    public void Create()
    {
      Destroy();

      var db = CreateDatabase();
      db.GetCollectionNames(); // Creation doesn't happen until a command is run against the database

      foreach (var schemaDefinition in Description.Schemas)
      {
        var command = new BsonJavaScript(schemaDefinition.Load());
        db.Eval(command);
      }
    }
Esempio n. 18
0
        /// <summary>
        /// 运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdRun_Click(object sender, EventArgs e)
        {
            BsonJavaScript map = new BsonJavaScript(txtMapJs.Text);
            BsonJavaScript reduce = new BsonJavaScript(txtReduceJs.Text);
            //TODO:这里可能会超时,失去响应
            //需要设置SocketTimeOut
            MapReduceResult rtn = _mongocol.MapReduce(map, reduce);

            List<BsonDocument> result = new List<BsonDocument>();
            result.Add(rtn.Response);
            MongoDBHelper.FillDataToTreeView("MapReduce Result", trvResult, result,0);
            trvResult.DatatreeView.ExpandAll();
        }
        public void constructor_should_initialize_subject()
        {
            var function = new BsonJavaScript("return 1");

            var subject = new EvalOperation(_adminDatabaseNamespace, function, _messageEncoderSettings);

            subject.Args.Should().BeNull();
            subject.DatabaseNamespace.Should().Be(_adminDatabaseNamespace);
            subject.Function.Should().Be(function);
            subject.MaxTime.Should().NotHaveValue();
            // subject.MessageEncoderSettings.Should().Be(_messageEncoderSettings);
            Assert.Equal(_messageEncoderSettings, subject.MessageEncoderSettings);
            subject.NoLock.Should().NotHaveValue();
        }
Esempio n. 20
0
 /// <summary>
 ///     eval Javascript
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cmdEval_Click(object sender, EventArgs e)
 {
     var mongoDb = RuntimeMongoDbContext.GetCurrentDataBase();
     var js = new BsonJavaScript(ctlEval.Context);
     var Params = new List<BsonValue>();
     if (txtParm.Text != string.Empty)
     {
         foreach (var parm in txtParm.Text.Split(",".ToCharArray()))
         {
             if (parm.StartsWith("'") & parm.EndsWith("'"))
             {
                 Params.Add(parm.Trim("'".ToCharArray()));
             }
             else
             {
                 try
                 {
                     var isNuberic = true;
                     for (var i = 0; i < parm.Length; i++)
                     {
                         if (!char.IsNumber(parm, i))
                         {
                             isNuberic = false;
                         }
                     }
                     if (isNuberic)
                     {
                         Params.Add(Convert.ToInt16(parm));
                     }
                 }
                 catch (Exception ex)
                 {
                     Utility.ExceptionDeal(ex, "Exception", "Parameter Exception");
                 }
             }
         }
     }
     try
     {
         var args = new EvalArgs {Args = Params.ToArray(), Code = js};
         var result = mongoDb.Eval(args);
         MyMessageBox.ShowMessage("Result", "Result",
             result.ToJson(MongoHelper.JsonWriterSettings), true);
     }
     catch (Exception ex)
     {
         Utility.ExceptionDeal(ex, "Exception", "Result");
     }
 }
Esempio n. 21
0
 /// <summary>
 /// eval Javascript
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cmdEval_Click(object sender, EventArgs e)
 {
     MongoDatabase mongoDB = SystemManager.GetCurrentDataBase();
     BsonJavaScript js = new BsonJavaScript(txtevalJs.Text);
     List<Object> Params = new List<Object>();
     if (txtParm.Text != String.Empty)
     {
         foreach (String parm in txtParm.Text.Split(",".ToCharArray()))
         {
             if (parm.StartsWith("'") & parm.EndsWith("'"))
             {
                 Params.Add(parm.Trim("'".ToCharArray()));
             }
             else
             {
                 try
                 {
                     Boolean isNuberic = true;
                     for (int i = 0; i < parm.Length; i++)
                     {
                         if (!char.IsNumber(parm, i))
                         {
                             isNuberic = false;
                         }
                     }
                     if (isNuberic)
                     {
                         Params.Add(Convert.ToInt16(parm));
                     }
                 }
                 catch (Exception ex)
                 {
                     MyMessageBox.ShowMessage("Exception", "Parameter Exception", ex.ToString(), true);
                 }
             }
         }
     }
     try
     {
         BsonValue result = mongoDB.Eval(js, Params.ToArray());
         MyMessageBox.ShowMessage("Result", "Result", result.ToJson(SystemManager.JsonWriterSettings), true);
     }
     catch (Exception ex)
     {
         MyMessageBox.ShowMessage("Exception", "Result", ex.ToString(), true);
     }
 }
Esempio n. 22
0
        public void Eval(int iteration)
        {
            var code = new BsonJavaScript ("function(key) { return db.todos.findOne({ _id: key }); }");

            Stopwatch watch = new Stopwatch ();
            watch.Start ();

            for (int i=0; i < iteration; i++) {
                db.Eval (new EvalArgs {
                    Code = code,
                    Args = new BsonValue[] { ObjectId.Parse ("53e33773e59f82131aef12ab") }
                });
            }

            watch.Stop ();
            Console.WriteLine ("Eval({1}): {0}", watch.Elapsed, iteration);
        }
Esempio n. 23
0
        public void Execute_should_return_expected_results_when_Scope_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var finalizeFunction = new BsonJavaScript("function(key, reduced) { return reduced + zeroFromScope; }");
            var scope            = new BsonDocument("zeroFromScope", 0);
            var subject          = new MapReduceOperation <BsonDocument>(_collectionNamespace, _mapFunction, _reduceFunction, _resultSerializer, _messageEncoderSettings)
            {
                FinalizeFunction = finalizeFunction,
                Scope            = scope
            };

            var cursor  = ExecuteOperation(subject, async);
            var results = ReadCursorToEnd(cursor, async);

            results.Should().Equal(
                BsonDocument.Parse("{ _id : 1, value : 3 }"),
                BsonDocument.Parse("{ _id : 2, value : 4 }"));
        }
Esempio n. 24
0
 private MapReduceOperation <TResult> CreateMapReduceOperation <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TResult> options, IBsonSerializer <TResult> resultSerializer)
 {
     return(new MapReduceOperation <TResult>(
                _collectionNamespace,
                map,
                reduce,
                resultSerializer,
                _messageEncoderSettings)
     {
         Collation = options.Collation,
         Filter = options.Filter == null ? null : options.Filter.Render(_documentSerializer, _settings.SerializerRegistry),
         FinalizeFunction = options.Finalize,
         JavaScriptMode = options.JavaScriptMode,
         Limit = options.Limit,
         MaxTime = options.MaxTime,
         ReadConcern = _settings.ReadConcern,
         Scope = options.Scope,
         Sort = options.Sort == null ? null : options.Sort.Render(_documentSerializer, _settings.SerializerRegistry),
         Verbose = options.Verbose
     });
 }
Esempio n. 25
0
        public void CreateCommand_should_return_the_expected_result_when_FinalizeFunction_is_provided()
        {
            var finalizeFunction = new BsonJavaScript("finalize");
            var subject          = new FakeMapReduceOperation(_collectionNamespace, _mapFunction, _reduceFunction, _query, _messageEncoderSettings)
            {
                FinalizeFunction = finalizeFunction
            };
            var expectedResult = new BsonDocument
            {
                { "mapreduce", "collectionName" },
                { "map", _mapFunction },
                { "reduce", _reduceFunction },
                { "out", new BsonDocument("fake", 1) },
                { "query", _query },
                { "finalize", finalizeFunction }
            };

            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
        public void TestBsonJavaScript()
        {
            var value = new BsonJavaScript("code");

            Assert.AreSame(value, ((IConvertible)value).ToType(typeof(object), null));
            Assert.Throws <InvalidCastException>(() => Convert.ToBoolean(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToByte(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToChar(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToDateTime(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToDecimal(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToDouble(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToInt16(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToInt32(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToInt64(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToSByte(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToSingle(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToString(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToUInt16(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToUInt32(value));
            Assert.Throws <InvalidCastException>(() => Convert.ToUInt64(value));
        }
Esempio n. 27
0
        public IActionResult RunScript(string query)
        {
            if (string.IsNullOrEmpty(query))
            {
                return(Json(new { Result = false, Message = "Empty query!" }));
            }

            try
            {
                var bscript      = new BsonJavaScript(query);
                var operation    = new EvalOperation(_mongoDBContext.Database().DatabaseNamespace, bscript, null);
                var writeBinding = new WritableServerBinding(_mongoDBContext.Database().Client.Cluster, NoCoreSession.NewHandle());
                var result       = operation.Execute(writeBinding, CancellationToken.None);
                var xx           = result["_ns"];
                return(Json(new { Result = true, Message = result.ToString() }));
            }
            catch (Exception ex)
            {
                return(Json(new { Result = false, Message = ex.Message }));
            }
        }
        public MongoAchievementService()
            : base(new MongoAchievementRepository())
        {
            _mapFunction = new BsonJavaScript(@"function()
            {
                this.Achievements.forEach(function(achieved)
                    {
                        emit(achieved.BlizzardID, {count : 1 });
                    });
            }");

            _reduceFunction = new BsonJavaScript(@"function(key, values)
            {
                var total = 0;
                for ( var i =0; i < values.length; i++)
                {
                    total += values[i].count;
                }
                return {count : total };
            }");
        }
Esempio n. 29
0
        public void _06_IncrementBooksCount()
        {
            Console.WriteLine(queryable.ToList()
                              .Select(bk => bk.name + "\t" + bk.count)
                              .Aggregate((a, b) => a + "\n" + b));

            Console.WriteLine();

            var map = new BsonJavaScript(
                @"
                function()
                {
                    emit('a', this.count)
                }");

            var reduce = new BsonJavaScript(
                @"
                function(key, values)
                {
                    return Array.sum(values)
                }");

            var oldOverallCount = collection
                                  .MapReduce <ReduceResult <int> >(map, reduce)
                                  .Single().value;

            collection.UpdateMany(
                "{}",
                "{$inc: {count: 1}}");

            var newOverallCount = collection
                                  .MapReduce <ReduceResult <int> >(map, reduce)
                                  .Single().value;

            Assert.IsTrue(newOverallCount - oldOverallCount == queryable.Count());

            Console.WriteLine(queryable.ToList()
                              .Select(bk => bk.name + "\t" + bk.count)
                              .Aggregate((a, b) => a + "\n" + b));
        }
Esempio n. 30
0
        /// <summary>
        /// Returns all objects from a location and the locations whitin
        /// </summary>
        /// <param name="locationId"></param>
        /// <returns></returns>
        public string GetSubObjects(string locationId = null, bool filter = false)
        {
            locationId = (locationId != null && locationId != "") ? locationId : "";

            string         queryFunction = "getSubObjects ( \"" + locationId + "\")";
            BsonJavaScript SubFunction   = new BsonJavaScript(queryFunction);

            //Calling the stores Mongo Function
            MongoConection      conection = (MongoConection)Conection.getConection();
            BsonArray           result    = conection.getDataBase().Eval(SubFunction).AsBsonArray;
            List <BsonDocument> documents = new List <BsonDocument>();

            foreach (BsonDocument document in result)
            {
                document.Set("_id", document.GetElement("_id").Value.ToString());
                try
                {
                    document.Set("CreatedTimeStamp", document.GetElement("CreatedTimeStamp").Value.ToString());
                }
                catch (Exception ex) { }
                string type = "true";
                if (filter)
                {
                    try
                    {
                        type = document.GetElement("system_status").Value.ToString().ToLower();
                    }
                    catch { }
                }
                if (filter && type == "false")
                {
                }
                else
                {
                    documents.Add(document);
                }
            }

            return(documents.ToJson());
        }
Esempio n. 31
0
        public void Execute_should_return_expected_results_when_FinalizeFunction_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check();
            EnsureTestData();
            var finalizeFunction = new BsonJavaScript("function(key, reduced) { return -reduced; }");

#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOperation <BsonDocument>(_collectionNamespace, _mapFunction, _reduceFunction, _resultSerializer, _messageEncoderSettings)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                FinalizeFunction = finalizeFunction
            };

            var cursor  = ExecuteOperation(subject, async);
            var results = ReadCursorToEnd(cursor, async);

            results.Should().BeEquivalentTo(
                BsonDocument.Parse("{ _id : 1, value : -3 }"),
                BsonDocument.Parse("{ _id : 2, value : -4 }"));
        }
        /*
         * private static void map(IMongoDatabase database)
         * {
         *
         *  // Firstly I will store all my map information inside of a map variable.
         *  //I am creating a map function and each dvd will be the 'this' parameter.
         *
         *  string mapfunction = @"
         *                  function() {
         *                          var dvd = this;
         *                               emit(collections.rating, { count: 1, priceFee: collections.price});
         * }";
         *
         *
         *  string reduceFunction = @"
         *                    function(key, values) {
         *                       var result = {count: 0, priceFee: 0};
         *
         *                       values.forEach(function(value){
         *                       result.count += value.count;
         *                       result.priceFee += value.priceFee;
         *              });
         *           return result;
         *      }";
         *
         *
         *  var collection = dataBase.GetCollection<BsonDocument>("DVDSystem");
         *  var options = new MapReduceOptionsBuilder();
         *  options.setFinalize(finalize);
         *  options.setOutput(MapReduceOutput.Inline);
         *
         *  var results = collection.MapReduce(map, reduce, options);
         *
         *  foreach (var result in results.GetResults())
         *  {
         *      Console.WriteLine(results.ToJson());
         *  }
         *
         * }
         */


        public void MapFunc(int specifiedAge)
        {
            BsonJavaScript map = "function() { " +
                                 "emit(this.dvdName, {'price' : this.cost , 'Rating' : this.rating});}";

            //Reduce function which does nothing than returning the result
            BsonJavaScript reduce = "function(key, values) { " +
                                    "values.forEach(" +
                                    "function(value) {" +
                                    "return value;" +
                                    "});" +
                                    "}";

            // Filtering Movies to only show movies depending on rating chosen by user

            var filteringMovies = Builders <BsonDocument> .Filter.Eq("rating", specifiedAge);

            //Options for the result of the output
            var options = new MapReduceOptions <BsonDocument, BsonDocument>
            {
                OutputOptions = MapReduceOutputOptions.Inline,
                Filter        = filteringMovies,
            };


            //Excute map and reduce functions
            var resultMR = collections.MapReduce(map, reduce, options);

            //print first result only

            try
            {
                CustomMessageBox.Show(resultMR.First().ToJson <MongoDB.Bson.BsonDocument>(), "caption", "caption", "caption");
            }
            catch (Exception e)
            {
                MessageBox.Show("No Movies found with rating : " + txtAge.Text);
            }
        }
Esempio n. 33
0
 /// <summary>
 ///     运行
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cmdRun_Click(object sender, EventArgs e)
 {
     var map = new BsonJavaScript(ctlMapFunction.Context);
     var reduce = new BsonJavaScript(ctlReduceFunction.Context);
     //TODO:这里可能会超时,失去响应
     //需要设置SocketTimeOut
     var args = new MapReduceArgs();
     args.MapFunction = map;
     args.ReduceFunction = reduce;
     try
     {
         var mMapReduceResult = RuntimeMongoDbContext.GetCurrentCollection().MapReduce(args);
         UiHelper.FillDataToTreeView("MapReduce Result", trvResult, mMapReduceResult.Response);
         trvResult.DatatreeView.BeginUpdate();
         trvResult.DatatreeView.ExpandAll();
         trvResult.DatatreeView.EndUpdate();
     }
     catch (Exception ex)
     {
         Utility.ExceptionDeal(ex);
     }
 }
Esempio n. 34
0
        public void constructor_with_key_should_initialize_subject()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key                    = new BsonDocument("key", 1);
            var initial                = new BsonDocument("x", 1);
            var reduceFunction         = new BsonJavaScript("reduceFunction");
            var criteria               = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();

            var subject = new GroupOperation <BsonDocument>(collectionNamespace, key, initial, reduceFunction, criteria, messageEncoderSettings);

            subject.CollectionNamespace.Should().Be(collectionNamespace);
            subject.Criteria.Should().Be(criteria);
            subject.FinalizeFunction.Should().BeNull();
            subject.Initial.Should().Be(initial);
            subject.Key.Should().Be(key);
            subject.KeyFunction.Should().BeNull();
            subject.MaxTime.Should().Be(default(TimeSpan?));
            Assert.That(subject.MessageEncoderSettings, Is.EqualTo(messageEncoderSettings));
            subject.ReduceFunction.Should().Be(reduceFunction);
            subject.ResultSerializer.Should().BeNull();
        }
Esempio n. 35
0
        public void constructor_with_key_should_initialize_subject()
        {
            var collectionNamespace = new CollectionNamespace("databaseName", "collectionName");
            var key = new BsonDocument("key", 1);
            var initial = new BsonDocument("x", 1);
            var reduceFunction = new BsonJavaScript("reduceFunction");
            var filter = new BsonDocument("y", 1);
            var messageEncoderSettings = new MessageEncoderSettings();

            var subject = new GroupOperation<BsonDocument>(collectionNamespace, key, initial, reduceFunction, filter, messageEncoderSettings);

            subject.CollectionNamespace.Should().Be(collectionNamespace);
            subject.Filter.Should().Be(filter);
            subject.FinalizeFunction.Should().BeNull();
            subject.Initial.Should().Be(initial);
            subject.Key.Should().Be(key);
            subject.KeyFunction.Should().BeNull();
            subject.MaxTime.Should().Be(default(TimeSpan?));
            Assert.Equal(messageEncoderSettings, subject.MessageEncoderSettings);
            subject.ReduceFunction.Should().Be(reduceFunction);
            subject.ResultSerializer.Should().BeNull();
        }
Esempio n. 36
0
        public int GetNumObjects(String idlocation)
        {
            int       result = 0;
            BsonArray objs   = new BsonArray();

            if (idlocation == null || idlocation == "")
            {
                idlocation = "null";
            }

            string         queryFunction = "getSubObjects ('" + idlocation + "' )";
            BsonJavaScript JoinFunction  = new BsonJavaScript(queryFunction);

            //Calling the stores Mongo Function
            MongoConection conection = (MongoConection)Conection.getConection();

            objs = conection.getDataBase().Eval(JoinFunction).AsBsonArray;

            result = objs.Count;

            return(result);
        }
Esempio n. 37
0
    public static void feePerEmployee()
    {
        // Get the employees collection
        var employees = _database.GetCollection <BsonDocument>("employees");

        // Get all the employees with there id, hours and fee
        BsonJavaScript map = "function() { " +
                             "emit(this._id, {hours : this.projects.positions.hours, fee: this.projects.positions.fee}); " +
                             "}";

        // Get the total hours and fee per employees
        BsonJavaScript reduce = "function(key, values) {" +
                                "var result = {hours: 0, fee: 0};" +
                                "values.forEach(" +
                                "function(value) {" +
                                "result.hours = value.hours;" +
                                "result.fee += value.fee;" +
                                "}" +
                                ");" +
                                "return result;" +
                                "}";

        // Set the MapReduce options
        var finalize = "function(key,value){ value.totalFee = value.hours * value.fee; return value;}";
        var options  = new MapReduceOptions <BsonDocument, BsonDocument>();

        options.OutputOptions = MapReduceOutputOptions.Inline;
        options.Finalize      = finalize;

        // Excute map and reduce functions
        var resultMR = employees.MapReduce(map, reduce, options).ToList();

        // Print the results
        foreach (var result in resultMR)
        {
            Console.WriteLine("employee: " + result["_id"] + ", hours: " + result["value"]["hours"] + ", fee: " + result["value"]["fee"] + ", totalFee: " + result["value"]["totalFee"]);
        }
    }
        public void Execute_should_return_expected_results_when_Scope_is_set(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet);
            EnsureTestData();
            var finalizeFunction = new BsonJavaScript("function(key, reduced) { return reduced + zeroFromScope; }");
            var scope            = new BsonDocument("zeroFromScope", 0);

#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new MapReduceOutputToCollectionOperation(_collectionNamespace, _outputCollectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                FinalizeFunction = finalizeFunction,
                Scope            = scope
            };

            ExecuteOperation(subject, async);

            ReadAllFromCollection(_outputCollectionNamespace).Should().BeEquivalentTo(
                BsonDocument.Parse("{ _id : 1, value : 3 }"),
                BsonDocument.Parse("{ _id : 2, value : 4 }"));
        }
Esempio n. 39
0
        public void CreateCommand_should_return_expected_result_when_keyFunction_was_provided()
        {
            var collectionNamespace    = new CollectionNamespace("databaseName", "collectionName");
            var keyFunction            = new BsonJavaScript("keyFunction");
            var initial                = new BsonDocument("x", 1);
            var reduceFunction         = new BsonJavaScript("reduceFunction");
            var messageEncoderSettings = new MessageEncoderSettings();
            var subject                = new GroupOperation <BsonDocument>(collectionNamespace, keyFunction, initial, reduceFunction, null, messageEncoderSettings);
            var expectedResult         = new BsonDocument
            {
                { "group", new BsonDocument
                  {
                      { "ns", collectionNamespace.CollectionName },
                      { "$keyf", keyFunction },
                      { "$reduce", reduceFunction },
                      { "initial", initial }
                  } }
            };

            var result = subject.CreateCommand();

            result.Should().Be(expectedResult);
        }
Esempio n. 40
0
        /// <summary>
        ///     运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdRun_Click(object sender, EventArgs e)
        {
            var map    = new BsonJavaScript(ctlMapFunction.Context);
            var reduce = new BsonJavaScript(ctlReduceFunction.Context);
            //TODO:这里可能会超时,失去响应
            //需要设置SocketTimeOut
            var args = new MapReduceArgs();

            args.MapFunction    = map;
            args.ReduceFunction = reduce;
            try
            {
                var mMapReduceResult = RuntimeMongoDbContext.GetCurrentCollection().MapReduce(args);
                UiHelper.FillDataToTreeView("MapReduce Result", trvResult, mMapReduceResult.Response);
                trvResult.DatatreeView.BeginUpdate();
                trvResult.DatatreeView.ExpandAll();
                trvResult.DatatreeView.EndUpdate();
            }
            catch (Exception ex)
            {
                Utility.ExceptionDeal(ex);
            }
        }
Esempio n. 41
0
        public Dictionary <int, int> StatYearsMapReduce(string auteur)
        {
            var db = GetDatabase(defaultDB);

            if (db != null)
            {
                Dictionary <int, int> statYears      = new Dictionary <int, int>();
                BsonJavaScript        mapFunction    = @"function () { emit(this.year , 1);};";
                BsonJavaScript        reduceFunction = @"function (key, values) { return Array.sum(values); }";
                var results = db
                              .GetCollection <BsonDocument>("publis2")
                              .MapReduce <BsonDocument>(mapFunction, reduceFunction);

                foreach (BsonDocument result in results.ToList())
                {
                    int key   = result["_id"].ToInt32();
                    int value = result["value"].ToInt32();
                    statYears.Add(key, value);
                }
                return(statYears);
            }
            return(new Dictionary <int, int>());
        }
Esempio n. 42
0
    public static void totalOverworkingEmployees()
    {
        // Get the employees collection
        var employees = _database.GetCollection <BsonDocument>("employees");

        // Get only the employees that are working more dan 20 hours
        BsonJavaScript map = "function() { " +
                             "if(this.projects.positions.hours > 20) { " +
                             "emit(this.projects.project_id, {count : 1}); " +
                             "}" +
                             "}";

        // Group the overworking employees by project_id
        BsonJavaScript reduce = "function(key, values) {" +
                                "var result = {count: 0};" +
                                "values.forEach(" +
                                "function(value) {" +
                                "result.count ++;" +
                                "}" +
                                ");" +
                                "return result;" +
                                "}";

        // Set the MapReduce options
        var options = new MapReduceOptions <BsonDocument, BsonDocument>();

        options.OutputOptions = MapReduceOutputOptions.Inline;

        // Excute map and reduce functions
        var resultMR = employees.MapReduce(map, reduce, options).ToList();

        // Print the results
        foreach (var result in resultMR)
        {
            Console.WriteLine("Project: " + result["_id"] + " has " + result["value"]["count"] + " overworking employees");
        }
    }
Esempio n. 43
0
        /// <summary>
        /// 客户任务调度统计
        /// </summary>
        /// <returns></returns>
        private Dictionary <string, int> GetCustomerJobInfo()
        {
            Dictionary <string, int> result = new Dictionary <string, int>();
            var col = new MongoOperation().GetCollection("BackgroundJob");
            Dictionary <string, int> dicInitial = new Dictionary <string, int>();

            dicInitial["count"] = 0;
            var r = col.Group(
                Query.And(Query.NE("customerCode", null), Query.NE("customerCode", ""), Query.Exists("customerCode", true)),
                "customerCode",
                BsonDocument.Create(dicInitial),
                BsonJavaScript.Create("function(doc,prev){prev.count++;}"),
                null
                ).ToList();

            if (r.Count > 0)
            {
                foreach (var item in r)
                {
                    result.Add(item.Text("customerCode"), item.Int("count"));
                }
            }
            return(result);
        }
Esempio n. 44
0
 private MapReduceOutputToCollectionOperation(
     string collectionName,
     string databaseName,
     BsonJavaScript finalizeFunction,
     bool?javaScriptMode,
     long?limit,
     BsonJavaScript mapFunction,
     bool?nonAtomicOutput,
     string outputCollectionName,
     string outputDatabaseName,
     MapReduceOutputMode outputMode,
     BsonDocument query,
     BsonJavaScript reduceFunction,
     BsonDocument scope,
     bool?shardedOutput,
     BsonDocument sort,
     bool?verbose)
     : base(
         collectionName,
         databaseName,
         finalizeFunction,
         javaScriptMode,
         limit,
         mapFunction,
         query,
         reduceFunction,
         scope,
         sort,
         verbose)
 {
     _nonAtomicOutput      = nonAtomicOutput;
     _outputCollectionName = outputCollectionName;
     _outputDatabaseName   = outputDatabaseName;
     _outputMode           = outputMode;
     _shardedOutput        = shardedOutput;
 }
Esempio n. 45
0
        private ICollection<KeyValuePair<DateTime, int>> GetPageViewsByDate(IMongoQuery query)
        {
            var map = new BsonJavaScript(
            @"function() {
            day = Date.UTC(this.ViewedAt.getFullYear(), this.ViewedAt.getMonth(), this.ViewedAt.getDate());
            emit({day: day, daynum: this.ViewedAt.getDate()}, {count: 1});
            }");

            var reduce = new BsonJavaScript(
            @"function(key, values) {
            var count = 0;
            values.forEach(function(v) {
            count += v['count'];
            });
            return {count: count};
            }");

            var results = DB.PageViews.MapReduce(query, map, reduce, MapReduceOptions.SetOutput(MapReduceOutput.Inline)).InlineResults.ToList();
            return results
                .ToKeyValuePairs(
                    x => DateTimeFromUnixTime(x["_id"].AsBsonDocument["day"].AsDouble),
                    x => x["value"].AsBsonDocument["count"].ToInt32())
                .OrderByDescending(kvp => kvp.Key).ToList();
        }
Esempio n. 46
0
 /// <summary>
 /// Who is are the most mentioned Twitter users? (Provide the top five.)
 /// </summary>
 public void Query3()
 {
     try
     {
         var options = new MapReduceOptions <BsonDocument, BsonDocument>();
         options.Filter = new BsonDocument {
         };
         options.Limit  = 5;
         options.Sort   = new BsonDocument {
             { "value", 1 }
         };
         var map       = new BsonJavaScript(@"function(){var match=this.text.match(/" + new Regex(@"@\w+") + "/); var key=match?match[0]:null; if(key){emit(key,1);}}");
         var reduce    = new BsonJavaScript(@"function(key,value){return Array.sum(value);}");
         var mapReduce = tweets.MapReduce(map, reduce, options).ToList();
         foreach (var item in mapReduce)
         {
             Console.WriteLine(item.ToString());
         }
     }
     catch (Exception e)
     {
         throw;
     }
 }
Esempio n. 47
0
        public IInfo getScoreNumber(String testname, String classs = "all")
        {
            MongoCollection collection;
            var             name = getTestAddr(testname);

            if ((DbBasic.rwHt[name] as int?) == DbBasic.STAGE_READ)//处于读阶段时,尝试直接读取数据库中的统计结果
            {
                collection = mcon.GetCollection("ChartStat");
                var query = Query.And(Query.EQ("_id", classs), Query.EQ("test", name));
                var tmp   = collection.FindOneAs <ScoreNumberInfo>(query);
                if (tmp != null)
                {
                    return(tmp);
                }
            }
            //处于写阶段时、没有统计结果时,调用数据库的StatScoreNumber2函数进行分数段人数统计
            collection = mcon.GetCollection("ChatStat");
            var str    = String.Format("StatScoreNumber2('{0}','{1}')", name, classs);
            var tmp2   = mcon.Eval(EvalFlags.NoLock, BsonJavaScript.Create(str));
            var result = JsonConvert.DeserializeObject <ScoreNumberInfo>(tmp2.ToJson());

            collection.Save(tmp2);
            return(result);
        }
 public TestClass(
     BsonJavaScript value
 )
 {
     this.B = value;
     this.V = value;
 }
Esempio n. 49
0
 /// <inheritdoc />
 public abstract Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken));
        public void TestBsonJavaScriptEquals()
        {
            var a = new BsonJavaScript("script 1");
            var b = new BsonJavaScript("script 1");
            var c = new BsonJavaScript("script 2");
            var n = (BsonJavaScript)null;

            Assert.IsTrue(object.Equals(a, b));
            Assert.IsFalse(object.Equals(a, c));
            Assert.IsFalse(object.Equals(a, BsonNull.Value));
            Assert.IsFalse(a.Equals(n));
            Assert.IsFalse(a.Equals(null));

            Assert.IsTrue(a == b);
            Assert.IsFalse(a == c);
            Assert.IsFalse(a == BsonNull.Value);
            Assert.IsFalse(a == null);
            Assert.IsFalse(null == a);
            Assert.IsTrue(n == null);
            Assert.IsTrue(null == n);

            Assert.IsFalse(a != b);
            Assert.IsTrue(a != c);
            Assert.IsTrue(a != BsonNull.Value);
            Assert.IsTrue(a != null);
            Assert.IsTrue(null != a);
            Assert.IsFalse(n != null);
            Assert.IsFalse(null != n);
        }
Esempio n. 51
0
 // public static methods
 /// <summary>
 /// Sets a key function.
 /// </summary>
 /// <param name="keyFunction">The key function.</param>
 /// <returns>A BsonJavaScript.</returns>
 public static BsonJavaScript Function(BsonJavaScript keyFunction)
 {
     return keyFunction;
 }
Esempio n. 52
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MapReduceLegacyOperation"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="mapFunction">The map function.</param>
 /// <param name="reduceFunction">The reduce function.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public MapReduceLegacyOperation(CollectionNamespace collectionNamespace, BsonJavaScript mapFunction, BsonJavaScript reduceFunction, MessageEncoderSettings messageEncoderSettings)
     : base(
         collectionNamespace,
         mapFunction,
         reduceFunction,
         messageEncoderSettings)
 {
 }
Esempio n. 53
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MapReduceOperation{TResult}"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="mapFunction">The map function.</param>
 /// <param name="reduceFunction">The reduce function.</param>
 /// <param name="resultSerializer">The result serializer.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public MapReduceOperation(CollectionNamespace collectionNamespace, BsonJavaScript mapFunction, BsonJavaScript reduceFunction, IBsonSerializer <TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
     : base(
         collectionNamespace,
         mapFunction,
         reduceFunction,
         messageEncoderSettings)
 {
     _resultSerializer = Ensure.IsNotNull(resultSerializer, nameof(resultSerializer));
 }
 /// <inheritdoc />
 public virtual IAsyncCursor <TResult> MapReduce <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
Esempio n. 55
0
 public Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <T, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_Repository.MapReduceAsync(map, reduce, options, cancellationToken));
 }
Esempio n. 56
0
        public ActionResult Services()
        {
            Dictionary <string, MachineModel> dicOfIList            = new Dictionary <string, MachineModel>();
            Dictionary <string, SeviceModel>  dicOfSeviceModelIList = new Dictionary <string, SeviceModel>();

            MongoServer server   = MongoServer.Create("mongodb://appsit01");
            var         database = server.GetDatabase("logs");

            using (server.RequestStart(database))
            {
                var servicePerformances = database.GetCollection("service_performance");

                var map    = new BsonJavaScript(@"
                function() {                                                                  
                    var key = {};
                    key.class = this.class;
                    key.method = this.method;
                    key.machineName = this.machineName;
                    emit(key, { count: 1 });                                                            
                }");
                var reduce = new BsonJavaScript(@"
                function(key, values) {
                    var result = { count: 0 };
                    values.forEach(function(value){               
                        result.count += value.count;
                    });
                    return result
                }");

                var results = servicePerformances.MapReduce(map, reduce);
                foreach (var result in results.GetResults())
                {
                    var doc   = result.ToBsonDocument();
                    var id    = doc["_id"].AsBsonDocument;
                    var value = doc["value"].AsBsonDocument;

                    string className   = id["class"].AsString;
                    string method      = id["method"].AsString;
                    string machineName = id["machineName"].AsString;

                    int totalCal = Convert.ToInt32(value["count"]);

                    if (!dicOfIList.ContainsKey(machineName))
                    {
                        dicOfIList.Add(machineName, new MachineModel
                        {
                            Name = machineName,
                        });
                    }

                    if (!dicOfSeviceModelIList.ContainsKey(machineName + className))
                    {
                        var seviceModel = new SeviceModel
                        {
                            Name = className,
                        };

                        dicOfSeviceModelIList.Add(machineName + className, seviceModel);
                        dicOfIList[machineName].Services.Add(seviceModel);
                    }

                    dicOfSeviceModelIList[machineName + className].Methods.Add(new MethodModel
                    {
                        Name       = method,
                        TotalUsage = totalCal,
                    });
                }
            }

            var v = dicOfIList.Values.OrderBy(item => item.Name).ToList();

            return(View(v));
        }
Esempio n. 57
0
 // public static methods
 /// <summary>
 /// Sets a key function.
 /// </summary>
 /// <param name="keyFunction">The key function.</param>
 /// <returns>A BsonJavaScript.</returns>
 public static BsonJavaScript Function(BsonJavaScript keyFunction)
 {
     return(keyFunction);
 }
Esempio n. 58
0
 // public static methods
 /// <summary>
 /// Sets the finalize function.
 /// </summary>
 /// <param name="finalize">The finalize function.</param>
 /// <returns>The builder (so method calls can be chained).</returns>
 public static MapReduceOptionsBuilder SetFinalize(BsonJavaScript finalize)
 {
     return(new MapReduceOptionsBuilder().SetFinalize(finalize));
 }
Esempio n. 59
0
 // public methods
 /// <summary>
 /// Sets the finalize function.
 /// </summary>
 /// <param name="finalize">The finalize function.</param>
 /// <returns>The builder (so method calls can be chained).</returns>
 public MapReduceOptionsBuilder SetFinalize(BsonJavaScript finalize)
 {
     _document["finalize"] = finalize;
     return(this);
 }
Esempio n. 60
0
 /// <summary>
 ///     运行
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cmdRun_Click(object sender, EventArgs e)
 {
     var map = new BsonJavaScript(ctlMapFunction.Context);
     var reduce = new BsonJavaScript(ctlReduceFunction.Context);
     //TODO:这里可能会超时,失去响应
     //需要设置SocketTimeOut
     var args = new MapReduceArgs();
     args.MapFunction = map;
     args.ReduceFunction = reduce;
     if (!string.IsNullOrEmpty(ctlFinalizeFunction.Context))
     {
         args.FinalizeFunction = new BsonJavaScript(ctlFinalizeFunction.Context);
     }
     args.OutputMode = (MapReduceOutputMode)cmbOutputMode.SelectedIndex;
     if (!string.IsNullOrEmpty(txtOutputCollectionName.Text)) args.OutputCollectionName = txtOutputCollectionName.Text;
     if (NumLimit.Value != 0) args.Limit = (long)NumLimit.Value;
     args.JsMode = chkjsMode.Checked;
     args.Verbose = chkverbose.Checked;
     args.BypassDocumentValidation = chkbypassDocumentValidation.Checked;
     if (QueryDoc != null) args.Query = new QueryDocument(QueryDoc);
     if (mCollation != null) args.Collation = mCollation;
     try
     {
         var mMapReduceResult = RuntimeMongoDbContext.GetCurrentCollection().MapReduce(args);
         var frm = new frmDataView();
         frm.ShowDocument = mMapReduceResult.Response;
         frm.Title = "MapReduce Result";
         UIAssistant.OpenModalForm(frm, true, true);
     }
     catch (Exception ex)
     {
         Utility.ExceptionDeal(ex);
     }
 }