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 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 void TestSerializeSortByWrapped() { var c = new C { X = 1 }; var w = SortByWrapper.Create(c); var json = w.ToJson(); var expected = "{ 'X' : 1 }".Replace("'", "\""); Assert.Equal(expected, json); }
/// <summary> /// Finds the specified spec. /// </summary> /// <param name="spec">The spec.</param> /// <param name="orderby">The orderby.</param> /// <param name="limit">The limit.</param> /// <param name="skip">The skip.</param> /// <returns></returns> public IEnumerable <TEntity> Find(object spec, object orderby, int limit, int skip) { using (_mongoHelper.Repository.RequestStart()) { return(_mongoHelper.Repository.GetCollection <TEntity>(_collection).Find(QueryWrapper.Create(spec)) .SetSortOrder(SortByWrapper.Create(orderby)) .SetSkip(skip) .SetLimit(limit) .ToList()); } }
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 UpdateDocuments(string databaseName, string collectionName, string query, Action <BsonDocument> updateDocument, string sort = null, int limit = 0, string options = null, string server = null) { MongoCollection collection = GetDatabase(server, databaseName).GetCollection(collectionName); QueryDocument queryDoc = query.zToQueryDocument(); SortByWrapper sortWrapper = sort.zToSortByWrapper(); BsonDocument optionsDoc = options.zDeserializeToBsonDocument(); MongoLog.CurrentMongoLog.LogUpdateDocuments(collection, queryDoc, sortWrapper, limit, optionsDoc); foreach (BsonDocument document in collection.zFind <BsonDocument>(queryDoc, sort: sortWrapper, limit: limit, options: optionsDoc)) { updateDocument(document); collection.zSave(document); } }
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 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(); } }
public void LogUpdateDocuments(MongoCollection collection, QueryDocument query, SortByWrapper sort = null, int limit = 0, BsonDocument options = null) { LogQuery("Update documents", collection, query: query, sort: sort, limit: limit, options: options); }
private void btnFind_Click(object sender, EventArgs e) { if (_treeListViewData.Roots == null) { return; } try { MainForm mf = (MainForm)Owner; MongoDatabase SelectedDB = mf.SelectedDB; string SelectedCollection = mf.SelectedCollection; MongoCollection coll = SelectedDB.GetCollection(SelectedCollection); //var mongoCursor1 = coll.FindAllAs<BsonDocument>().AsEnumerable(); BsonDocument queryBsonDoc = null; QueryDocument queryDoc = null; BsonDocument fieldBsonDoc = null; FieldsDocument fieldDoc = null; if (_rx.IsMatch(txtQuery.Text)) { queryBsonDoc = BsonSerializer.Deserialize <BsonDocument>(txtQuery.Text.Trim()); queryDoc = new QueryDocument(queryBsonDoc.Elements); } if (_rx.IsMatch(txtField.Text)) { fieldBsonDoc = BsonSerializer.Deserialize <BsonDocument>(txtField.Text.Trim()); fieldDoc = new FieldsDocument(fieldBsonDoc.Elements); } var mongoCursor = coll.FindAs <BsonDocument>(queryDoc); mongoCursor.Skip = (int)numSkip.Value; mongoCursor.Limit = (int)numLimit.Value; if (fieldDoc != null) { mongoCursor.Fields = fieldDoc; } if (_rx.IsMatch(txtSort.Text)) { BsonDocument orderDoc = BsonSerializer.Deserialize <BsonDocument>(txtSort.Text.Trim()); var order = new SortByWrapper(orderDoc); if (orderDoc != null) { mongoCursor.SetSortOrder(order); } } IList <BsonDocumentBindable> roots = new List <BsonDocumentBindable>(); int x = 1; foreach (BsonDocument doc in mongoCursor) { roots.Add(new BsonDocumentBindable(string.Format("({0}) Document ({1})", x, doc.ElementCount), null, doc.BsonType, doc)); x++; } _treeListViewData.Roots = roots; _treeListViewData.Refresh(); }catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }