Beispiel #1
0
        public static void Import(string databaseName, string collectionName, string file, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);

            Trace.WriteLine("Import : {0} from file \"{1}\"", collection.zGetFullName(), file);
            MongoCommand.Import(databaseName, collectionName, file, server);
        }
Beispiel #2
0
        public static void ExportDatabase(string databaseName, string directory, string sort = null, string server = null)
        {
            MongoDatabase database = MongoCommand.GetDatabase(server, databaseName);

            Trace.Write("Export database : {0} to directory \"{1}\"", database.zGetFullName(), directory);
            foreach (string collection in database.GetCollectionNames())
            {
                string file = zPath.Combine(directory, "export_" + collection + ".txt");
                Export(databaseName, collection, file, sort: sort, server: server);
            }
        }
Beispiel #3
0
        public static void FindOneById <TDocument>(string databaseName, string collectionName, object key, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);
            BsonValue       bsonKey    = BsonValue.Create(key);

            Trace.Write("FindOneByIdAs : {0} key ", collection.zGetFullName());
            Trace.Write(bsonKey.ToJson());
            Trace.Write(" type {0}", typeof(TDocument));
            TDocument result = collection.zFindOneById <TDocument>(bsonKey);

            Trace.WriteLine(result.zToJson());
        }
Beispiel #4
0
        public static void UpdateDocuments(string databaseName, string collectionName, string query, Action <BsonDocument> updateDocument, string sort = null, int limit = 0, string queryOptions = null,
                                           MongoInsertOptions saveOptions = null, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);

            Trace.Write("UpdateDocuments : {0} query ", collection.zGetFullName());

            QueryDocument queryDoc = query.zToQueryDocument();

            Trace.Write(queryDoc.ToJson());

            SortByWrapper sortByWrapper = null;

            if (sort != null)
            {
                sortByWrapper = sort.zToSortByWrapper();
                Trace.Write(" sort {0}", sortByWrapper.ToJson());
            }

            if (limit != 0)
            {
                Trace.Write(" limit {0}", limit);
            }

            BsonDocument queryOptionsDoc = null;

            if (queryOptions != null)
            {
                queryOptionsDoc = queryOptions.zDeserializeToBsonDocument();
                Trace.Write(" options {0}", queryOptionsDoc.ToJson());
            }

            Trace.WriteLine();

            if (saveOptions == null)
            {
                saveOptions = new MongoInsertOptions();
            }

            foreach (BsonDocument document in collection.zFind <BsonDocument>(queryDoc, sort: sortByWrapper, limit: limit, options: queryOptionsDoc))
            {
                updateDocument(document);

                Trace.Write("  Save : {0} document ", collection.zGetFullName());
                Trace.Write(document.ToJson());
                Trace.Write(" options ");
                Trace.Write(saveOptions.ToJson());
                Trace.WriteLine();

                collection.zSave(document, saveOptions);
            }
        }
Beispiel #5
0
        public static MongoCursor <TDocument> FindAll <TDocument>(string databaseName, string collectionName, string sort = null, string fields = null, int limit = 0, string options = null, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);

            Trace.Write("FindAllAs : {0}", collection.zGetFullName());

            SortByWrapper sortByWrapper = null;

            if (sort != null)
            {
                sortByWrapper = sort.zToSortByWrapper();
                Trace.Write(" sort {0}", sortByWrapper.ToJson());
            }

            FieldsWrapper fieldsWrapper = null;

            if (fields != null)
            {
                fieldsWrapper = fields.zToFieldsWrapper();
                Trace.Write(" fields {0}", fieldsWrapper.ToJson());
            }

            if (limit != 0)
            {
                Trace.Write(" limit {0}", limit);
            }

            BsonDocument optionsDoc = null;

            if (options != null)
            {
                optionsDoc = options.zDeserializeToBsonDocument();
                Trace.Write(" options {0}", optionsDoc.ToJson());
            }

            Trace.WriteLine();

            MongoCursor <TDocument> cursor = collection.zFindAll <TDocument>(sortByWrapper, fieldsWrapper, limit, optionsDoc);

            //if (ResultToText)
            //{
            //    int i = 0;
            //    foreach (TDocument value in cursor)
            //    {
            //        Trace.WriteLine(value.zToJson(true));
            //        i++;
            //    }
            //    Trace.WriteLine("found {0} document(s)", i);
            //}
            return(cursor);
        }
Beispiel #6
0
        public static void RenameCollection(string databaseName, string collectionName, string newCollectionName, string server = null)
        {
            MongoDatabase database = MongoCommand.GetDatabase(server, databaseName);

            Trace.Write("Rename collection : {0} \"{1}\" to \"{2}\"", database.zGetFullName(), collectionName, newCollectionName);
            try
            {
                CommandResult result = database.zRenameCollection(collectionName, newCollectionName);
                TraceResult(result);
            }
            finally
            {
                Trace.WriteLine();
            }
        }
Beispiel #7
0
        public static void RemoveAll(string databaseName, string collectionName, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);

            Trace.Write("RemoveAll : {0}", collection.zGetFullName());
            try
            {
                WriteConcernResult result = collection.zRemoveAll();
                TraceResult(result);
            }
            finally
            {
                Trace.WriteLine();
            }
        }
Beispiel #8
0
        public static void Count(string databaseName, string collectionName, string query = null, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);
            QueryDocument   queryDoc   = query.zToQueryDocument();

            Trace.Write("Count : {0}", collection.zGetFullName());
            if (queryDoc != null)
            {
                Trace.Write(" query ");
                Trace.Write(queryDoc.ToJson());
            }
            long count = collection.zCount(queryDoc);

            Trace.WriteLine(" count {0}", count);
        }
Beispiel #9
0
        //public static bool ResultToGrid = false;
        //public static bool ResultToText = false;

        public static void Eval(string code, string databaseName = "admin", string server = null)
        {
            MongoDatabase database = MongoCommand.GetDatabase(server, databaseName);
            //if (code.StartsWith("{") && code.EndsWith("}"))
            //    code = "db.runCommand( " + code + " )";
            //EvalArgs evalArgs = new EvalArgs { Code = new BsonJavaScript(code) };
            EvalArgs evalArgs = code.zToEvalArgs();

            Trace.WriteLine("Eval : {0} {1}", database.zGetFullName(), evalArgs.ToJson());
            // obsolete : 'MongoDB.Driver.MongoDatabase.Eval(MongoDB.Bson.BsonJavaScript, params object[])' is obsolete: 'Use the overload of Eval that has an EvalArgs parameter instead.'
            // BsonValue Eval(BsonJavaScript code, params object[] args);
            BsonValue result = database.zEval(evalArgs);

            Trace.WriteLine(result.zToJson());
        }
Beispiel #10
0
        public static void Export(string databaseName, string collectionName, string file, string query = null, string sort = null, string fields = null, int limit = 0, string options = null,
                                  Func <BsonDocument, BsonDocument> transformDocument = null, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);

            Trace.Write("Export : {0} to file \"{1}\"", collection.zGetFullName(), file);

            QueryDocument queryDoc = null;

            if (query != null)
            {
                queryDoc = query.zToQueryDocument();
                Trace.Write(queryDoc.ToJson());
            }

            SortByWrapper sortByWrapper = null;

            if (sort != null)
            {
                sortByWrapper = sort.zToSortByWrapper();
                Trace.Write(" sort {0}", sortByWrapper.ToJson());
            }

            FieldsWrapper fieldsWrapper = null;

            if (fields != null)
            {
                fieldsWrapper = fields.zToFieldsWrapper();
                Trace.Write(" fields {0}", fieldsWrapper.ToJson());
            }

            if (limit != 0)
            {
                Trace.Write(" limit {0}", limit);
            }

            BsonDocument optionsDoc = null;

            if (options != null)
            {
                optionsDoc = options.zDeserializeToBsonDocument();
                Trace.Write(" options {0}", optionsDoc.ToJson());
            }

            Trace.WriteLine();

            MongoCommand.Export(databaseName, collectionName, file, query, sort, fields, limit, options, transformDocument, server);
        }
Beispiel #11
0
        public static void Remove(string databaseName, string collectionName, string query, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);
            QueryDocument   queryDoc   = query.zToQueryDocument();

            Trace.Write("Remove : {0} query ", collection.zGetFullName());
            Trace.Write(queryDoc.ToJson());
            try
            {
                WriteConcernResult result = collection.zRemove(queryDoc);
                TraceResult(result);
            }
            finally
            {
                Trace.WriteLine();
            }
        }
Beispiel #12
0
        //IEnumerable<TValue> Distinct<TValue>(string key, IMongoQuery query);
        //public static MongoCursor<TDocument> Find<TDocument>(string databaseName, string collectionName, string query, string sort = null, string fields = null, int limit = 0, string options = null, string server = null)
        //{
        //}

        public static void FindAndModify(string databaseName, string collectionName, string query, string update, bool upsert = false, string sort = null, string fields = null, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);

            QueryDocument  queryDoc  = query.zToQueryDocument();
            UpdateDocument updateDoc = update.zToUpdateDocument();

            Trace.Write("FindAndModify : {0} query ", collection.zGetFullName());
            Trace.Write(queryDoc.ToJson());
            Trace.Write(" update ");
            Trace.Write(updateDoc.ToJson());
            Trace.Write(" upsert {0}", upsert);

            SortByWrapper sortByWrapper = null;

            if (sort != null)
            {
                sortByWrapper = sort.zToSortByWrapper();
                Trace.Write(" sort {0}", sortByWrapper.ToJson());
            }

            FieldsWrapper fieldsWrapper = null;

            if (fields != null)
            {
                fieldsWrapper = fields.zToFieldsWrapper();
                Trace.Write(" fields {0}", fieldsWrapper.ToJson());
            }

            FindAndModifyResult result;

            try
            {
                result = collection.zFindAndModify(queryDoc, updateDoc, upsert, sortByWrapper, fieldsWrapper);
                TraceResult(result);
            }
            finally
            {
                Trace.WriteLine();
            }
            Trace.WriteLine("document");
            Trace.WriteLine(result.ModifiedDocument.zToJson());
        }
Beispiel #13
0
        public static void Insert <TDocument>(string databaseName, string collectionName, TDocument document, MongoInsertOptions options = null, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);

            Trace.Write("Insert : {0}", collection.zGetFullName());
            Trace.Write(" document ");
            Trace.Write(document.ToJson());
            if (options == null)
            {
                options = new MongoInsertOptions();
            }
            Trace.Write(" check element names {0} flags {1}", options.CheckElementNames, options.Flags);
            try
            {
                WriteConcernResult result = collection.zInsert(document, options);
                TraceResult(result);
            }
            finally
            {
                Trace.WriteLine();
            }
        }
Beispiel #14
0
        public static void Update(string databaseName, string collectionName, string query, string update, UpdateFlags flags = UpdateFlags.None, string server = null)
        {
            MongoCollection collection = MongoCommand.GetDatabase(server, databaseName).GetCollection(collectionName);

            QueryDocument  queryDoc  = query.zToQueryDocument();
            UpdateDocument updateDoc = update.zToUpdateDocument();

            Trace.Write("Update : {0} query ", collection.zGetFullName());
            Trace.Write(queryDoc.ToJson());
            Trace.Write(" update ");
            Trace.Write(updateDoc.ToJson());
            Trace.Write(" flags ");
            Trace.Write(flags.ToString());

            try
            {
                WriteConcernResult result = collection.zUpdate(queryDoc, updateDoc, flags);
                TraceResult(result);
            }
            finally
            {
                Trace.WriteLine();
            }
        }