gleam_mongo

A mongodb driver for gleam

Quick start

gleam shell # Run an Erlang shell

Installation

gleam add gleam_mongo

Roadmap

Usage

Connection

import mongo
import bson/types
import mongo/utils
import mongo/aggregation.{aggregate, exec, lookup, match}

pub fn main() {
  assert Ok(conn) = mongo.connect("127.0.0.1", 27017)

  let db =
    conn
    |> mongo.db("app_db")

  let collection =
    db
    |> mongo.collection("users")

  collection
  |> mongo.insert_one(types.Document([
    #("first_name", types.Str("Steve")),
    #("last_name", types.Str("Wozniak")),
  ]))

  collection
  |> mongo.update_one(
    types.Document([#("first_name", types.Str("Dennis"))]),
    types.Document([
      #("$set", types.Document([#("last_name", types.Str("Ritchie"))])),
    ]),
    [utils.Upsert(True)],
  )

  collection
  |> aggregate()
  |> match(types.Document([#("first_name", types.Str("Dennis"))]))
  |> lookup(
    from: "technologies",
    local_field: "known_for",
    foreign_field: "name",
    alias: "known_for",
  )
  |> exec()
}