discord test stars

About

MongoDB client in the Mys programming language.

Project: https://github.com/mys-lang/package-mongodb

Examples

List databases

from mongodb import Client

func main():
    client = Client()
    client.connect()
    print(client.list_databases())
    client.disconnect()

List collections

from mongodb import Client

func main():
    client = Client()
    client.connect()
    print(client.list_collections("database"))
    client.disconnect()

Drop collection

from mongodb import Client

func main():
    client = Client()
    client.connect()
    client.drop("database", "collection")
    client.disconnect()

Insert documents

from bson import Array
from bson import Boolean
from bson import Document
from mongodb import Client

func main():
    client = Client()
    client.connect()
    client.insert("database",
                  "collection",
                  Array([
                      Document([("x", Boolean(True))]),
                      Document([("x", Boolean(False))])
                  ]))
    client.disconnect()

Find documents

from bson import Boolean
from bson import Document
from mongodb import Client

func main():
    client = Client()
    client.connect()
    filter = Document([("x", Document([("$eq", Boolean(False))]))])
    projection = Document([("_id", Int32(0))])
    documents = client.find_many("database",
                                 "collection",
                                 filter=filter,
                                 projection=projection)

    for document in documents:
        print(document.get("x").get_boolean())

    client.disconnect()

Delete documents

from bson import Array
from bson import Boolean
from bson import Document
from bson import Int32
from mongodb import Client

func main():
    client = Client()
    client.connect()
    client.delete("database",
                  "collection",
                  Array([
                      Document([
                          ("q", Document([("x", Boolean(True))])),
                          ("limit", Int32(0))
                      ])
                  ]))
    client.disconnect()

API

class MongodbError(Error):
    message: string
class Client:
    A MongoDB client.

    func __init__(self):

    func connect(self, host: string = "127.0.0.1", port: i64 = 27017):
        Connect to given server.

    func disconnect(self):
        Disconnect from the server.

    func list_databases(self) -> [string]:
        List all databases.

    func list_collections(self, database: string) -> [string]:
        List all collections in given database.

    func drop(self, database: string, collection: string):
        Drop given collection in given database.

    func insert(self, database: string, collection: string, documents: Array):
        Insert given documents in given collection in given database.

    func delete(self, database: string, collection: string, deletes: Array):
        Delete documents in given collection in given database.

    func find_many(self,
                   database: string,
                   collection: string,
                   filter: Document? = None,
                   sort: Document? = None,
                   projection: Document? = None) -> [Document]:
        Find documents in given collection in given database matching given
        filter.

    func find(self,
              database: string,
              collection: string,
              filter: Document? = None,
              sort: Document? = None,
              projection: Document? = None) -> Document:
        Find first batch of documents in given collection in given database
        matching given filter.

    func get_more(self, database: string, collection: string, cursor_id: Int64) -> Document:
        Get documents at given cursor id in given collection in given
        database starting. Call find() to get first batch of documents and
        the first cursor.

    func run_command(self, command: Document) -> Document:
        Run given command and returns its response.