Skip to content

scalajs-io/mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB API for Scala.js

mongodb - The official MongoDB driver for Node.js.

Description

The official MongoDB driver for Node.js. Provides a high-level API on top of mongodb-core that is meant for end users.

Build Requirements

Build/publish the SDK locally

 $ sbt clean publish-local

Running the tests

Before running the tests the first time, you must ensure the npm packages are installed:

$ npm install

Then you can run the tests:

$ sbt test

Examples

import io.scalajs.JSON
import io.scalajs.nodejs._
import io.scalajs.npm.mongodb._
import scalajs.js


val url = "mongodb://localhost:27017/test"
MongoClient.connect(url, (err, db) => {
    val col = db.collection("streams")
    
    // Insert some documents
    col.insertMany(js.Array(new Sample(a = 1), new Sample(a = 2), new Sample(a = 3)), (err, iwr) => {        
        // Get the results using a find stream
        val stream = col.find[Sample](doc()).stream()
        stream.onData[Sample](doc => console.log(doc))
        stream.onEnd(() => db.close())
    })
})


class Sample(var _id: js.UndefOr[ObjectID] = js.undefined, 
             var a: js.UndefOr[Int] = js.undefined) 
  extends js.Object

Artifacts and Resolvers

To add the MongoDB binding to your project, add the following to your build.sbt:

libraryDependencies += "io.scalajs.npm" %%% "mongodb" % "0.5.0"

Optionally, you may add the Sonatype Repository resolver:

resolvers += Resolver.sonatypeRepo("releases")