private BsonValue ExecuteOperation(EvalOperation operation, IWriteBinding binding, bool async)
 {
     if (async)
     {
         return operation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult();
     }
     else
     {
         return operation.Execute(binding, CancellationToken.None);
     }
 }
예제 #2
0
        public static void MapReduce()
        {
            var databaseName = new DatabaseNamespace(DbConnection.Db.DatabaseNamespace.DatabaseName);

            var code = (BsonJavaScript) @"
                function () {
                    var docs = db.Entry.find({isShown: true}, {'publishDate':1});
                    var length = docs.count();
                    var merged = { };
                    for (var i = 0; i < length; i++)
                    {
                        var value = docs[i].publishDate;
                        var date = (new Date(value)).getUTCFullYear().toString() + '/' + ('0' + ((new Date(value)).getUTCMonth() + 1)).slice(-2);
                        if (date in merged) {
                            merged[date] += 1;
                        } else {
                            merged[date] = 1;
                        }
                    }
                    db.DateSummary.remove({});
                    db.DateSummary.insert({_id:'dateGrouping', value:merged});
                }";

            var messageEncodingSettings = new MessageEncoderSettings();
            var operation    = new EvalOperation(databaseName, code, messageEncodingSettings);
            var session      = new CoreSessionHandle(NoCoreSession.Instance);
            var writeBinding = new WritableServerBinding(DbConnection.Db.Client.Cluster, session);

            operation.Execute(writeBinding, CancellationToken.None);
        }
예제 #3
0
        public static void MapReduce()
        {
            var databaseName = new DatabaseNamespace(DbConnection.Db.DatabaseNamespace.DatabaseName);

            var code = (BsonJavaScript) @"
                function () {
                    var docs = db.Entry.find({isShown: true}, {'tag':1});
                    var length = docs.count();
                    var tags = [];
                    for (var i = 0; i < length; i++)
                    {
                        var doc = docs[i];
                        tags = tags.concat(doc.tag);
                    }
                    var merged = { };
                    for (var i = 0; i < tags.length; i++)
                    {
                        var tag = tags[i].replace('.', '\uff0e');
                        if (tag in merged) {
                            merged[tag] += 1;
                        } else {
                            merged[tag] = 1;
                        }
                    }
                    db.TagSummary.remove({});
                    db.TagSummary.insert({_id:'tag', value:merged});
                }";

            var messageEncodingSettings = new MessageEncoderSettings();
            var operation    = new EvalOperation(databaseName, code, messageEncodingSettings);
            var session      = new CoreSessionHandle(NoCoreSession.Instance);
            var writeBinding = new WritableServerBinding(DbConnection.Db.Client.Cluster, session);

            operation.Execute(writeBinding, CancellationToken.None);
        }
예제 #4
0
        public BsonValue RunScript(string command, CancellationToken cancellationToken)
        {
            var script       = new BsonJavaScript(command);
            var operation    = new EvalOperation(_database.DatabaseNamespace, script, null);
            var writeBinding = new WritableServerBinding(_client.Cluster);

            return(operation.Execute(writeBinding, CancellationToken.None));
        }
        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 });
            }
        }
        /// <summary>
        /// Evaluates JavaScript code at the server.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <returns>The result of evaluating the code.</returns>
        public virtual BsonValue Eval(EvalArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }
            if (args.Code == null)
            {
                throw new ArgumentException("Code is null.", "args");
            }

            var operation = new EvalOperation(_namespace, args.Code, GetMessageEncoderSettings())
            {
                Args    = args.Args,
                MaxTime = args.MaxTime,
                NoLock  = args.Lock.HasValue ? !args.Lock : null
            };

            using (var binding = _server.GetWriteBinding())
            {
                return(operation.Execute(binding));
            }
        }