public void TestVariableWorksForQuery() { int index = 10; IMongoQuery query = Query <TestClass> .EQ(x => x.List[index].Name, "Blah"); Assert.Equal("{ \"List.10.Name\" : \"Blah\" }", query.ToString()); }
public void TestVariableWorksForQueryWithVariableChange() { var queryBuilder = new QueryBuilder <TestClass>(); int index = 10; IMongoQuery query = queryBuilder.EQ(x => x.List[index].Name, "Blah"); index = 11; query = queryBuilder.EQ(x => x.List[index].Name, "Blah"); Assert.Equal("{ \"List.11.Name\" : \"Blah\" }", query.ToString()); }
public void query_on_mongo_direct_to_query_test() { MongoCursor <Person> query = mongo.PeopleCollection.Find(Query <Person> .GT(p => p.Age, 30)); const string jsQuery = "{ \"Age\" : { \"$gt\" : 30 } }"; IMongoQuery mongoQuery = query.ToMongoQuery(); Assert.IsNotNull(mongoQuery); Assert.AreEqual(jsQuery, mongoQuery.ToString()); }
public override MongoCursor FindAs(Type documentType, IMongoQuery query) { if (writer != null) { this.writer.WriteLine("FindAs Query:"); this.writer.WriteLine(query == null ? "No Query" : query.ToString()); this.writer.WriteLine(); this.writer.Flush(); } return(new MongoCursorInterceptor <U>(mCollection.FindAs <U>(query), this.TryRemember)); }
public override SafeModeResult Remove(IMongoQuery query, RemoveFlags flags, SafeMode safeMode) { if (writer != null) { this.writer.WriteLine("Remove Query:"); this.writer.WriteLine(query == null ? "Null" : query.ToString()); this.writer.WriteLine(); this.writer.Flush(); } return(mCollection.Remove(query, flags, safeMode)); }
public override FindAndModifyResult FindAndRemove(IMongoQuery query, IMongoSortBy sortBy) { if (writer != null) { this.writer.WriteLine("FindAndRemove Query:"); this.writer.WriteLine(query == null ? "No Query" : query.ToString()); this.writer.WriteLine("SortBy: " + (sortBy == null ? "Nothing" : sortBy.ToString())); this.writer.WriteLine(); this.writer.Flush(); } return(mCollection.FindAndRemove(query, sortBy)); }
public void query_on_linq_to_query_test() { var query = from p in mongo.People where p.Age > 30 select p; const string jsQuery = "{ \"Age\" : { \"$gt\" : 30 } }"; IMongoQuery mongoQuery = query.ToMongoQuery(); Assert.IsNotNull(mongoQuery); Assert.AreEqual(jsQuery, mongoQuery.ToString()); }
public override FindAndModifyResult FindAndModify( IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, bool returnNew, bool upsert) { if (writer != null) { this.writer.WriteLine("FindAndModify Query:"); this.writer.WriteLine(query == null ? "No Query" : query.ToString()); this.writer.WriteLine("SortBy: " + (sortBy == null ? "Nothing" : sortBy.ToString())); this.writer.WriteLine("Update: " + (update == null ? "Nothing" : update.ToString())); this.writer.WriteLine(); this.writer.Flush(); } return(mCollection.FindAndModify(query, sortBy, update, returnNew, upsert)); }
public override MongoCursor <T> FindAs <T>(IMongoQuery query) { if (writer != null) { this.writer.WriteLine("FindAs Query:"); this.writer.WriteLine(query == null ? "No Query" : query.ToString()); this.writer.WriteLine(); this.writer.Flush(); } if (typeof(U) == typeof(T)) { return(new MongoCursorInterceptor <T>(mCollection.FindAs <T>(query), this.TryRemember)); } else { return(base.FindAs <T>(query)); } }
/// <summary> /// Count /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void countToolStripMenuItem_Click(object sender, EventArgs e) { var Query = new DataFilter(); String ColPath = SystemManager.SelectTagData; Boolean IsUseFilter = false; if (_viewInfoList.ContainsKey(ColPath)) { Query = _viewInfoList[ColPath].mDataFilter; IsUseFilter = _viewInfoList[ColPath].IsUseFilter; } if (Query.QueryConditionList.Count == 0 || !IsUseFilter) { MyMessageBox.ShowEasyMessage("Count", "Count Result : " + SystemManager.GetCurrentCollection().Count()); } else { IMongoQuery mQuery = QueryHelper.GetQuery(Query.QueryConditionList); MyMessageBox.ShowMessage("Count", "Count[With DataView Filter]:" + SystemManager.GetCurrentCollection().Count(mQuery), mQuery.ToString(), true); } }
public void TestTypeIsOnProperty() { IMongoQuery query = Query <TestClass> .Where(x => x.Prop is B); Assert.AreEqual("{ \"Prop._t\" : \"B\" }", query.ToString()); }
//TODO: additional testing needed. /// <summary> /// WARNING: this has been tested (pdb) to work when all documents contain element you are renaming. Further testing is needed before this is production ready. /// Rename BsonElements within a collection. (e.g. If you change the name of 'LineId' to 'AnimalLineId' in a class) /// </summary> /// <param name="oldName">Case Sensitive BsonElement name you with to change</param> /// <param name="newName">New BsonElement name that will replace [oldName]</param> /// <param name="caseSensitivePKColumns">Case Sensitive list of all columns with comprise the Primary Key</param> public void RenameElement(string oldName, string newName, params string[] caseSensitivePKColumns) { //TODO: figure out how to determine if the 'Id' field is an ObjectId type, and then search by Id instead of PK cols foreach (T doc in FindAll()) { List <IMongoQuery> list = new List <IMongoQuery>(); BsonDocument bsonDoc = doc.ToBsonDocument(); foreach (string pkCol in caseSensitivePKColumns) { list.Add(Query.EQ(pkCol, bsonDoc.GetValue(pkCol))); } IMongoQuery query = Query.And(list.ToArray()); IMongoUpdate upd = global::MongoDB.Driver.Builders.Update.Rename(oldName, newName); var result = FindAndUpdate(query, SortBy.Null, upd); if (result.ErrorMessage != null) { throw new Exception(string.Format("Error renaming {0} to {1} for {2}", oldName, newName, query.ToString())); } } }
public void BuildQueryBuildsQueryWithToOnlyTest() { PrivateObject p = new PrivateObject(typeof(MongoDataSource.MongoDataSource)); string fieldName = "field1"; string toVal = "toval"; IMongoQuery query = (IMongoQuery)p.Invoke("BuildQuery", new object[] { fieldName, (BsonValue)null, (BsonValue)toVal }); Assert.AreEqual("{ \"" + fieldName + "\" : { \"$lte\" : \"" + toVal + "\" } }", query.ToString()); }
public void BuildQueryBuildsQueryWithFromOnlyTest() { PrivateObject p = new PrivateObject(typeof(MongoDataSource.MongoDataSource)); System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod; string fieldName = "field1"; string fromVal = "fromval"; Type[] types = new Type[] { typeof(string), typeof(BsonValue), typeof(BsonValue) }; object[] pars = new object[] { fieldName, (BsonValue)fromVal, (BsonValue)null }; IMongoQuery query = (IMongoQuery)p.Invoke("BuildQuery", flags, types, pars); Assert.AreEqual("{ \"" + fieldName + "\" : { \"$gte\" : \"" + fromVal + "\" } }", query.ToString()); }
private DBQuery QueryListView( string[] splitTerms, DataTree schema, string collection, string orderby, bool ascending, int documentperpage, int page, string searchIdField, string searchId, IMongoQuery filter) { var resultsQuery = new DBQuery(); var searchFields = Schema.GetSearchFields(schema); var andQueries = new List <IMongoQuery>(); // The query explained: // // If threre are search terms // - Get element that matches both search term and relation (if present) // - Repeat for each search term // // If there are no search terms // - Get element that matches relation. If no relation is present get all items. // // Apply query sorting paging etc... if (searchFields.Count > 0 && splitTerms.Length > 0) { foreach (string term in splitTerms) { if (term.Length < MinTermLength) { continue; } var orQueries = new List <IMongoQuery>(); foreach (string searchField in searchFields) { var filterQueries = new List <IMongoQuery>(); var regEx = new Regex(Regex.Escape(term), System.Text.RegularExpressions.RegexOptions.IgnoreCase); filterQueries.Add(Query.Matches(searchField, new BsonRegularExpression(regEx))); if (!string.IsNullOrEmpty(searchIdField) && !string.IsNullOrEmpty(searchId)) { filterQueries.Add( Query.EQ(searchIdField, new ObjectId(searchId))); } orQueries.Add(Query.And(filterQueries)); } if (orQueries.Count > 0) { andQueries.Add(Query.Or(orQueries)); } } if (filter != null) { andQueries.Add(filter); } resultsQuery[collection][DBQuery.Condition] = Query.And(andQueries).ToJson(); } else { if (!string.IsNullOrEmpty(searchIdField) && !string.IsNullOrEmpty(searchId)) { if (filter != null) { andQueries.Add(filter); } andQueries.Add(Query.EQ(searchIdField, new ObjectId(searchId))); resultsQuery[collection][DBQuery.Condition] = Query.And(andQueries).ToString(); } else { if (filter != null) { resultsQuery[collection][DBQuery.Condition] = filter.ToString(); } else { resultsQuery[collection][DBQuery.Condition] = DBQuery.All; } } } if (!resultsQuery[collection][DBQuery.Condition].Empty) { resultsQuery[collection][DBQuery.OrderBy] = orderby; resultsQuery[collection][DBQuery.Ascending] = ascending; resultsQuery[collection][DBQuery.DocumentsPerPage] = documentperpage; resultsQuery[collection][DBQuery.Page] = page; return(resultsQuery); } else { return(null); } }
public void TestTypeOfComparisonOnProperty() { IMongoQuery query = Query <TestClass> .Where(x => x.Prop.GetType() == typeof(B)); Assert.AreEqual("{ \"Prop._t.0\" : { \"$exists\" : false }, \"Prop._t\" : \"B\" }", query.ToString()); }
private static IMongoQuery SetParameter(this IMongoQuery query, string parameterName, String value) { return(new QueryDocument(BsonDocument.Parse(query.ToString().Replace(parameterName, value)))); }
public static IMongoQuery GeoQuery(MongoLocation loc, double prox, IMongoQuery query) { var locQuery = QueryConstants.GeoQuery(loc, prox); var q = new QueryDocument(); q.AddRange(BsonDocument.Parse(locQuery.ToString())); if (query != null) q.AddRange(BsonDocument.Parse(query.ToString())); return q; }