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 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); } }
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(); }