Source

mongo-dart / lib / selector_builder.dart

Full commit
SelectorBuilder query(){
  return new SelectorBuilder();
}
class SelectorBuilder<K,V> extends _MapProxy<K,V>{
  toString()=>"SelectorBuilder($map)";
  SelectorBuilder eq(String fieldName,Comparable value){
    map[fieldName] = value;
    return this;
  }
  SelectorBuilder ne(String fieldName, Comparable value){
    map[fieldName] = {"\$ne":value};
    return this;
  }
  SelectorBuilder gt(String fieldName,Comparable value){
    map[fieldName] = {"\$gt":value};
    return this;
  }
  SelectorBuilder lt(String fieldName,Comparable value){
    map[fieldName] = {"\$lt":value};
    return this;
  }
  SelectorBuilder gte(String fieldName,Comparable value){
    map[fieldName] = {"\$gte":value};
    return this;
  }
  SelectorBuilder lte(String fieldName,Comparable value){    
    map[fieldName] = {"\$lte":value};
    return this;
  }
  SelectorBuilder all(String fieldName, List values){
    map[fieldName] = {"\$all":values};
    return this;
  }
  SelectorBuilder nin(String fieldName, List values){
    map[fieldName] = {"\$nin":values};
    return this;
  }
  SelectorBuilder oneFrom(String fieldName, List values){
    map[fieldName] = {"\$in":values};
    return this;
  } 
  SelectorBuilder exists(String fieldName){
    map[fieldName] = {"\$exists":true};
    return this;    
  }
  SelectorBuilder notExists(String fieldName){
    map[fieldName] = {"\$exists":false};
    return this;    
  }
  SelectorBuilder mod(String fieldName, int value){
    map[fieldName] = {"\$mod":[value,0]};
    return this;    
  }
  SelectorBuilder match(String fieldName, String pattern,[bool multiLine, bool caseInsensitive, bool verbose, bool dotAll, bool unicodeMode, bool localeDependent]){    
    map[fieldName] = {'\$regex': new BsonRegexp('^str_(5|7|8)17\$',multiLine:multiLine, caseInsensitive:caseInsensitive,
        verbose:verbose, dotAll:dotAll,unicodeMode:unicodeMode,localeDependent:localeDependent)};
    return this;    
  }
  SelectorBuilder range(String fieldName, Comparable min, Comparable max, [bool minInclude=true, bool maxInclude=true]){
    Map rangeMap = {};
    if (minInclude){
      rangeMap["\$gte"] = min;
    }
    else{
      rangeMap["\$gt"] = min;
    }
    if (maxInclude){
      rangeMap["\$lte"] = max;
    }
    else{
      rangeMap["\$gt"] = max;
    }
    map[fieldName] = rangeMap;
    return this;    
  }
  _internQueryMap(){
    if (!map.containsKey("query")){
      LinkedHashMap queryMap = new LinkedHashMap.from(map);
      map.clear();
      map["query"] = queryMap;
    }    
  }
  SelectorBuilder sortBy(String fieldName, [bool descending=false]){
    _internQueryMap();
    if (!map.containsKey("orderby")){
      map["orderby"] = new LinkedHashMap();  
    }
    int order = 1;
    if (descending){
      order = -1;
    }
    map["orderby"][fieldName] = order;      
    return this;    
  }
  SelectorBuilder comment(String commentStr){
    _internQueryMap();  
    map["\$comment"] = commentStr;      
    return this;    
  }
  SelectorBuilder explain(){
    _internQueryMap();  
    map["\$explain"] = true;      
    return this;    
  }
  SelectorBuilder snapshot(){
    _internQueryMap();  
    map["\$snapshot"] = true;      
    return this;    
  }
  SelectorBuilder showDiskLoc(){
    _internQueryMap();  
    map["\$showDiskLoc"] = true;      
    return this;    
  }
  SelectorBuilder returnKey(){
    _internQueryMap();  
    map["\$sreturnKey"] = true;      
    return this;    
  }
  
}