Giter Site home page Giter Site logo

mongo_dart's Introduction

Mongo-dart - MongoDB driver for Dart programming language.

Pub Build Status

Server-side driver library for MongoDb implemented in pure Dart.

Basic usage

Obtaining connection

  Db db = new Db("mongodb://localhost:27017/mongo_dart-blog");
  await db.open();

Querying

Method find returns stream of maps and accept query parameters, usually build by fluent API query builder provided by mongo_dart_query as top level getter where

  var coll = db.collection('user');
  await coll.find(where.lt("age", 18)).toList();
  
  //....
  
  await coll
      .find(where.gt("my_field", 995).sortBy('my_field'))
      .forEach((v) => print(v));
      
  //....
  
  await coll.find(where.sortBy('itemId').skip(300).limit(25)).toList();
  

Method findOne take the same parameter and returns Future of just one map (mongo document)

  val = await coll.findOne(where.eq("my_field", 17).fields(['str_field','my_field']));

Take notice in these samples that unlike mongo shell such parameters as projection (fields), limit and skip are passed as part of regular query through query builder

Inserting documents

  await usersCollection.insertAll([
    {'login': 'jdoe', 'name': 'John Doe', 'email': '[email protected]'},
    {'login': 'lsmith', 'name': 'Lucy Smith', 'email': '[email protected]'}
  ]);

Updating documents

You can update whole document with method save

  var v1 = await coll.findOne({"name": "c"});
  v1["value"] = 31;
  await coll.save(v1);

or you can perform field level updates with method update and top level getter modify for ModifierBuilder fluent API

  coll.update(where.eq('name', 'Daniel Robinson'), modify.set('age', 31));

Removing documents

  students.remove(where.id(id));
  /// or, to remove all documents from collection
  students.remove();    
    

Simple app on base of [JSON ZIPS dataset] (http://media.mongodb.org/zips.json)

import 'package:mongo_dart/mongo_dart.dart';

main() async {
  void displayZip(Map zip) {
    print(
        'state: ${zip["state"]}, city: ${zip["city"]}, zip: ${zip["id"]}, population: ${zip["pop"]}');
  }
  Db db =
      new Db("mongodb://reader:[email protected]:37468/samlple");
  var zips = db.collection('zip');
  await db.open();
  print('''
******************** Zips for state NY, with population between 14000 and 16000,
******************** reverse ordered by population''');
  await zips
      .find(where
          .eq('state', 'NY')
          .inRange('pop', 14000, 16000)
          .sortBy('pop', descending: true))
      .forEach(displayZip);
  print('\n******************** Find ZIP for code 78829 (BATESVILLE)');
  var batesville = await zips.findOne(where.eq('id', '78829'));
  displayZip(batesville);
  print('******************** Find 10 ZIP closest to BATESVILLE');
  await zips
      .find(where.near('loc', batesville["loc"]).limit(10))
      .forEach(displayZip);
  print('closing db');
  await db.close();
}

See also

mongo_dart's People

Contributors

adam-singer avatar analogic avatar cgarciae avatar eobodo avatar floitschg avatar giorgiofran avatar ivan-zaera avatar luizmineo avatar meriouma avatar nicolasjacob avatar pacane avatar patefacio avatar paulecoyote avatar rajmaniar avatar rdewilde avatar readmecritic avatar scorpiion avatar sestegra avatar thosakwe avatar tsander avatar vadimtsushko avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.