mongo-dart / lib / dbcollection.dart

class DbCollection{
  Db db;
  String collectionName;
  DbCollection(this.db, this.collectionName){}  
  String fullName() => "${db.databaseName}.$collectionName";
  save(Map document){
    var id;
    bool createId = false;
    if (document.containsKey("_id")){
      id = document["_id"];      
      if (id === null){
        createId = true;
      }
    }
    if (id !== null){
      update({"_id": id}, document);
    } 
    else{
      if (createId) {
        document["_id"] = new ObjectId();
      }     
      insert(document);
    }
  }
  insertAll(List<Map> documents){
    MongoInsertMessage insertMessage = new MongoInsertMessage(fullName(),documents);
    return db.executeDbCommand(insertMessage);   
  }
  update(Map selector, Map document){
    MongoUpdateMessage message = new MongoUpdateMessage(fullName(),selector, document, 0);
    return db.executeMessage(message);   
  }
  
  insert(Map document){
    return insertAll([document]);
  } 
  Cursor find([Map selector = const {}, Map fields = null, Map orderBy, int skip = 0,int limit = 0, bool hint = false, bool explain = false] ){
    return new Cursor(db, this, selector: selector, fields: fields, skip: skip, limit: limit, sort: orderBy);//, [selector, skip, limit,sort, hint, explain]);
  }
  Future<Map> findOne([Map selector = const {}, Map fields = null, Map orderBy, int skip = 0,int limit = 0, bool hint = false, bool explain = false] ){
    Cursor cursor = find(selector,fields,orderBy,skip,limit,hint,explain);
    Future<Map> result = cursor.nextObject();
    cursor.close();
    return result;
  }
  Future drop() => db.dropCollection(collectionName);
  remove([Map selector = const {}]) => db.removeFromCollection(collectionName, selector);
  Future count([Map selector = const {}]){
    Completer completer = new Completer();
    db.executeDbCommand(DbCommand.createCountCommand(db,collectionName,selector)).then((reply){       
      //print("reply = ${reply}");
      completer.complete(reply["n"]);      
    });
    return completer.future;
  }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.