public static void Export(MongoCollection collection, string file, string query = null, string sort = null, string fields = null, int limit = 0, string options = null, Func <BsonDocument, BsonDocument> transformDocument = null) { QueryDocument queryDoc = query.zToQueryDocument(); SortByWrapper sortWrapper = sort.zToSortByWrapper(); FieldsWrapper fieldsWrapper = fields.zToFieldsWrapper(); BsonDocument optionsDoc = options.zDeserializeToBsonDocument(); MongoLog.CurrentMongoLog.LogExport(collection, file, queryDoc, sortWrapper, fieldsWrapper, limit, optionsDoc); zfile.CreateFileDirectory(file); //FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read); FileStream fs = zFile.Open(file, FileMode.Create, FileAccess.Write, FileShare.Read); //StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); // no bom with new UTF8Encoding() StreamWriter sw = new StreamWriter(fs, new UTF8Encoding()); try { foreach (BsonDocument document in collection.zFind <BsonDocument>(queryDoc, sort: sortWrapper, fields: fieldsWrapper, limit: limit, options: optionsDoc)) { BsonDocument document2 = document; if (transformDocument != null) { document2 = transformDocument(document2); } sw.WriteLine(document2.zToJson()); } } finally { sw.Close(); fs.Close(); } }
public void TestSerializeFieldsWrapped() { var c = new C { X = 1 }; var w = FieldsWrapper.Create(c); var json = w.ToJson(); var expected = "{ 'X' : 1 }".Replace("'", "\""); Assert.Equal(expected, json); }
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); }
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); }
//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()); }
public void LogFindAndModify(MongoCollection collection, QueryDocument query, UpdateDocument update, bool upsert, SortByWrapper sort, FieldsWrapper fields) { if (_writeToFile == null) { return; } _writeToFile.Write("{0:yyyy-MM-dd HH:mm:ss.ffffff} ", DateTime.Now); _writeToFile.Write("FindAndModify : {0}", collection.zGetFullName()); _writeToFile.Write(" query {0}", query.ToJson()); _writeToFile.Write(" update "); _writeToFile.Write(update.ToJson()); _writeToFile.Write(" upsert {0}", upsert); if (sort != null) { _writeToFile.Write(" sort {0}", sort.ToJson()); } if (fields != null) { _writeToFile.Write(" fields {0}", fields.ToJson()); } //_log.WriteLine(); }
public void LogFindAllAs(MongoCollection collection, SortByWrapper sort = null, FieldsWrapper fields = null, int limit = 0, BsonDocument options = null) { LogQuery("FindAllAs", collection, sort: sort, fields: fields, limit: limit, options: options); if (_writeToFile != null) { _writeToFile.WriteLine(); } }
public static FindAndModifyResult zFindAndModify(this MongoCollection collection, QueryDocument query, UpdateDocument update, bool upsert = false, SortByWrapper sort = null, FieldsWrapper fields = null) { MongoLog.CurrentMongoLog.LogFindAndModify(collection, query, update, upsert, sort, fields); FindAndModifyArgs args = new FindAndModifyArgs(); args.Query = query; args.SortBy = sort; args.Fields = fields; args.Update = update; args.Upsert = upsert; return(MongoLog.CurrentMongoLog.ExecuteAndLogResult(() => collection.FindAndModify(args))); }
public static MongoCursor <TDocument> zFindAll <TDocument>(this MongoCollection collection, SortByWrapper sort = null, FieldsWrapper fields = null, int limit = 0, BsonDocument options = null) { MongoLog.CurrentMongoLog.LogFindAllAs(collection, sort, fields, limit, options); return(MongoLog.CurrentMongoLog.ExecuteAndLogResult( () => { MongoCursor <TDocument> cursor = collection.FindAllAs <TDocument>(); if (sort != null) { cursor.SetSortOrder(sort); } if (fields != null) { cursor.SetFields(fields); } if (limit != 0) { cursor.SetLimit(limit); } if (options != null) { cursor.SetOptions(options); } return cursor; })); }
private void LogQuery(string label, MongoCollection collection, string file = null, QueryDocument query = null, SortByWrapper sort = null, FieldsWrapper fields = null, int limit = 0, BsonDocument options = null) { if (_writeToFile == null) { return; } _writeToFile.Write("{0:yyyy-MM-dd HH:mm:ss.ffffff} ", DateTime.Now); _writeToFile.Write("{0} : {1}", label, collection.zGetFullName()); if (file != null) { _writeToFile.Write(" file \"{0}\"", file); } if (query != null) { _writeToFile.Write(" query {0}", query.ToJson()); } if (sort != null) { _writeToFile.Write(" sort {0}", sort.ToJson()); } if (fields != null) { _writeToFile.Write(" fields {0}", fields.ToJson()); } if (limit != 0) { _writeToFile.Write(" limit {0}", limit); } if (options != null) { _writeToFile.Write(" options {0}", options.ToJson()); } //_log.WriteLine(); }
public void LogExport(MongoCollection collection, string file, QueryDocument query, SortByWrapper sort = null, FieldsWrapper fields = null, int limit = 0, BsonDocument options = null) { LogQuery("Export", collection, file: file, query: query, sort: sort, fields: fields, limit: limit, options: options); if (_writeToFile != null) { _writeToFile.WriteLine(); } }