{"id":15154476,"url":"https://github.com/aamirmousavi/mongoleaf","last_synced_at":"2025-03-30T00:30:31.799Z","repository":{"id":52110836,"uuid":"445630994","full_name":"aamirmousavi/mongoleaf","owner":"aamirmousavi","description":"Mongo leaf is a simple tool between you and go-mongodb driver. in leaf we only use json format to use mongo just like mongo-shell if you have a dynamic scenario mongo leaf might be useful for you.","archived":false,"fork":false,"pushed_at":"2023-01-27T03:13:21.000Z","size":63,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-19T13:11:13.016Z","etag":null,"topics":["database","dynamic-programming","go","golang","golang-library","golang-module","golang-package","golang-tools","gorm","gorm-orm","mongo-go","mongo-go-driver","mongodb","mongodb-database","mongodb-driver","mongodb-orm","orm"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aamirmousavi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-07T19:28:36.000Z","updated_at":"2022-09-21T19:12:03.000Z","dependencies_parsed_at":"2023-02-15T04:46:16.404Z","dependency_job_id":null,"html_url":"https://github.com/aamirmousavi/mongoleaf","commit_stats":null,"previous_names":["aamirmousavi/mongo-leaf"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aamirmousavi%2Fmongoleaf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aamirmousavi%2Fmongoleaf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aamirmousavi%2Fmongoleaf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aamirmousavi%2Fmongoleaf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aamirmousavi","download_url":"https://codeload.github.com/aamirmousavi/mongoleaf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246262490,"owners_count":20749170,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","dynamic-programming","go","golang","golang-library","golang-module","golang-package","golang-tools","gorm","gorm-orm","mongo-go","mongo-go-driver","mongodb","mongodb-database","mongodb-driver","mongodb-orm","orm"],"created_at":"2024-09-26T17:40:30.125Z","updated_at":"2025-03-30T00:30:31.322Z","avatar_url":"https://github.com/aamirmousavi.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mongo Leaf\r\nMongo leaf is a simple tool between you and go-mongodb driver. in leaf we only use json format to use mongo just like mongo-shell if you have a dynamic scenario mongo leaf might be useful for you. I'm noob in go and programing sorry for bad explantion so I tryed to make anything simple for noobs like me.\r\n\r\n\r\n# Contents\r\n- [Create Connetion](#create-connetion)\r\n- [Create Database](#create-database)\r\n- [Create Colletion](#create-colletion)\r\n- [example](#simple-example)\r\n- [functions](#crud)\r\n    - [Client](#client-functions)\r\n        - [Connect](#Connect)\r\n        - [Disconnect](#Disconnect)\r\n        - [ListDatabaseNames](#ListDatabaseNames)\r\n        - [ListDatabases](#ListDatabases)\r\n    - [Database](#database-functions)\r\n        - [Aggregate](#Aggregate)\r\n        - [Drop](#Drop)\r\n        - [CreateView](#CreateView)\r\n        - [ListCollections](#ListCollections)\r\n        - [RunCommand](#RunCommand)\r\n        - [RunCommandCursor](#RunCommandCursor)\r\n    - [Colletion](#colletion-functions)\r\n        - [Find](#Find)\r\n        - [Aggregate](#Aggregate)\r\n        - [Update](#Update)\r\n        - [Delete](#Delete)\r\n        - [Insert](#Insert)\r\n        - [FindOneAnd](#FindOneAnd)\r\n        - [CountDocuments](#CountDocuments)\r\n        - [Distinct](#Distinct)\r\n        - [Drop](#Drop)\r\n        - [EstimatedDocumentCount](#EstimatedDocumentCount)\r\n\r\n\r\n# Install\r\nfirst execute this command\r\n```\r\n    go get github.com/aamirmousavi/mongoleaf\r\n```\r\nthen in your go code import leaf package\r\n```go\r\nimport (\r\n   \"github.com/aamirmousavi/mongoleaf\"\r\n)\r\n```\r\n\r\n\r\n# Create Connetion\r\n\r\nfor connect to mongodb \r\n```go\r\n    client, err := mongoleaf.New(\"mongodb://localhost:27017\")\r\n    if err != nil {\r\n        //deal with error\r\n\t}\r\n```\r\nput your mongo URL as an argument for make connetion with your database\r\n\r\n\r\n# Create Database\r\n\r\nin your mongodb client if you want to create or refrence to a database you would use DataBase Function just like this \r\nfor making a refrence to your database\r\n```go\r\n    client, err := mongoleaf.New(\"mongodb://localhost:27017\")\r\n    if err != nil {\r\n         //deal with error\r\n    }\r\n    myDataBase := client.DataBase(\"myDBName\")\r\n```\r\nnow you can esly access the colletions and function of each database you have\r\n\r\n# Create Colletion\r\n\r\nas you now in mongo each database might have many collations so when you define a database you can call or create a collation in that database just like this example\r\n```go\r\nmyCollation := client.DataBase(\"DbName\").Colletion(\"myCollation\")\r\n```\r\nif database or colletion you calling is not exsists it will create them\r\n\r\n\r\n# Simple Example\r\n```go\r\npackage main\r\n\r\nimport (\r\n\t\"fmt\"\r\n\r\n\t\"github.com/aamirmousavi/mongoleaf\"\r\n)\r\n\r\nfunc main() {\r\n\tclient, err := mongoleaf.New(\"mongodb://localhost:27017\")\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\r\n\tsiteDB := client.DataBase(\"website\")\r\n\tproducts := siteDB.Colletion(\"products\")\r\n\r\n\tproducts.InsertMany(`[\r\n\t\t{\r\n\t\t\"title\":\"pro_1\",\r\n\t\t\"price\":5\r\n\t}, {\r\n\t\t\"title\":\"pro_2\",\r\n\t\t\"price\":2\r\n\t}, {\r\n\t\t\"title\":\"pro_3\",\r\n\t\t\"price\":15\r\n\t}\r\n\t]`, \"\")\r\n\r\n    //simple index\r\n\tproducts.CreateIndex(`{\r\n\t\t\"Keys\":{\r\n\t\t\t\"price\":1\r\n\t\t},\r\n\t\t\"Options\":{\r\n\t\t\t\"Name\":\"myPriceIndex\"\r\n\t\t}\r\n\t}`, ``)\r\n    \r\n\t//first arg is filter that mongo filter rows and second is the update you want to set\r\n\t//and last is option if you don't have any option use empty string or empty json map: \"\" or \"{}\"\r\n\tresult, err := products.UpdateMany(`{\r\n\t\t\"price\":{\r\n\t\t\t\"$gt\":10\r\n\t\t}\r\n\t}`, `{\r\n\t\t\"$set\":{\r\n\t\t\t\"more_than_10\":true\r\n\t\t}\r\n\t}`, \"\")\r\n\r\n\tfmt.Printf(\"update \\n\\terror: %v \\n\\tresult: %v \\n\", err, result)\r\n\r\n\t//if your filter is \"{}\" you can also use empty string as no filter\r\n\t//it will return all rows in a colletion\r\n\trows, err := products.Find(\"\", \"\")\r\n\tjsonRows := mongoleaf.JSONPretty(rows)\r\n\tfmt.Printf(\"find result \\n\\terror: %v \\n\\trows: %v \\n\", err, jsonRows)\r\n}\r\n\r\n```\r\nOutPut\r\n```\r\nupdate \r\n        error: \u003cnil\u003e \r\n        result: map[MatchedCount:1 ModifiedCount:1 UpsertedCount:0 UpsertedID:\u003cnil\u003e] \r\nfind result \r\n        error: \u003cnil\u003e \r\n        rows: [\r\n        {\r\n                \"_id\": \"61e4bf46d16388a583426754\",\r\n                \"price\": 5,\r\n                \"title\": \"pro_1\"\r\n        },\r\n        {\r\n                \"_id\": \"61e4bf46d16388a583426755\",\r\n                \"price\": 2,\r\n                \"title\": \"pro_2\"\r\n        },\r\n        {\r\n                \"_id\": \"61e4bf46d16388a583426756\",\r\n                \"more_than_10\": true,\r\n                \"price\": 15,\r\n                \"title\": \"pro_3\"\r\n        }\r\n ] \r\n```\r\n# Functions\r\n\r\n- # Client Functions\r\n\r\n    you always able to use dirvers functions like start session or watch and extra like this example\r\n    ```go\r\n        client, err := mongoleaf.New(\"mongodb://localhost:27017\")\r\n        if err != nil {\r\n            panic(err)\r\n        }\r\n        dbNames, err := client.Client.StartSession(\u0026options.SessionOptions{})\r\n    ```\r\n    just add **``.Client``** and call your functions also this works for database and colletion not only client type\r\n    - # Connect\r\n        ```go\r\n            client, err := mongoleaf.New(\"mongodb://localhost:27017\")\r\n            if err != nil {\r\n                panic(err)\r\n            }\r\n            err = client.Connect()\r\n        ```\r\n    - # Disconnect\r\n        ```go\r\n            err = client.Disconnect()\r\n        ```\r\n\r\n    - # ListDatabaseNames\r\n        ````go\r\n            filter, option := `{}`, `{}` //empty filter return all \r\n            dbNames, err := client.ListDatabaseNames(filter, option)\r\n        ````\r\n\r\n    - # ListDatabases\r\n        reutrn a array of maps with Name, Empty and SizeOnDisk keys\r\n        ```go\r\n            dataBases, err := client.ListDatabases(filter, option)\r\n        ```\r\n\r\n-  # Database Functions\r\n    \r\n    - # Aggregate\r\n        ```go\r\n            out, err := client.DataBase(dbName).Aggregate(pipline, option)\r\n        ```\r\n    - # Drop\r\n        ```go\r\n            err := client.DataBase(dbName).Drop()\r\n        ```\r\n    - # CreateCollection\r\n        ```go\r\n        err := client.DataBase(dbName).CreateCollection(\"colName\")\r\n        ``` \r\n\r\n    - # CreateView\r\n        ```go\r\n        err := client.DataBase(dbName).CreateView(viewName, viewOn, pipline, option)\r\n        ```\r\n    - # ListCollectionNames\r\n        ```go\r\n        colNames, err := client.DataBase(dbName).ListCollectionNames(filter, option)\r\n        ```\r\n\r\n    - # ListCollections\r\n        ```go\r\n        colNames, err := client.DataBase(dbName).ListCollections(filter, option)\r\n        ```\r\n\r\n    - # RunCommand\r\n        ```go\r\n        out, err := client.DataBase(dbName).RunCommand(Command, option)\r\n        ```\r\n\r\n    - # RunCommandCursor\r\n        ```go\r\n        res, err := client.DataBase(dbName).RunCommandCursor(Command, option)\r\n        ```\r\n\r\n- # Colletion Functions\r\n\r\n    - # Find \r\n        for reading many recoard in a mongo colletion \r\n        ```go\r\n        myCollation := client.DataBase(\"DbName\").Colletion(\"myCollation\")\r\n        filter:=`{\"title\":\"my title\"}`  //just like mongoshell filter\r\n        options:=`{\"limit\":2}`           //just like mongoshell option\r\n        results, err := myCollation.Find(filter,options)\r\n        ```\r\n        it's retrun an array of maps if you want json  result you can parse any type to json with mongoleaf.JSON function\r\n        ```go\r\n        json := mongoleaf.JSON(results)\r\n        ```\r\n        for reading only one reacord \r\n        ```go\r\n        results, err := myCollation.FindOne(`{\"title\":\"my title\"}`,``)\r\n        ```\r\n        as you see we don't have any option we should pass empty  `` \"\" `` or `` `{}` `` as option \r\n        also if you have empty filter you can use empty string `` \"\" `` or `` `{}` `` as filter\r\n\r\n    - # Aggregate\r\n\r\n        ```go\r\n        results, err := myCollation.Aggregate(`[{},{}]`,``) \r\n        ```\r\n        as you know in mongodb Aggregate has array of map style so keep it in mind\r\n    - # Update \r\n        ```go\r\n        result, err := myCollation.UpdateMany(`{\"_id\":\"sd1d12ds12d...\"}`,`{\"title\":\"new title\"}`,option) \r\n        ```\r\n    - # Delete \r\n        for deleteing many recoards use DeleteMany fountion\r\n        ```go\r\n        result, err := client.DataBase(\"DbName\").Colletion(\"myCollation\").DeleteMany(`{\"_id\":\"sd12d12d2123\"}`,option)\r\n        ```\r\n        as you see we called it in defrent way you can call all CRUD fountions just like this code\r\n        ```go\r\n        result, err := myCollation.DeleteOne(`{\"_id\":\"sd1d12ds12d...\"}`,option) \r\n        ```\r\n    - # Insert\r\n        for inserting if you are using insert many you have to insert your rows in an array parent\r\n        ```go\r\n        result, err := myCollation.InsertMany(`[\r\n            {\"title\":\"the title 1\"},\r\n            {\"title\":\"the title 12\"},\r\n            {\"title\":\"the title 2\"}\r\n            ]`,option) \r\n        ```\r\n        for insetring one row \r\n        ```go\r\n        result, err := myCollation.InsertOne(`{\"title\":\"the title 1\"}`,option) \r\n        ```\r\n\r\n    - # FindOneAnd\r\n\r\n    - # CountDocuments\r\n\r\n    - # Distinct\r\n\r\n    - # Drop\r\n\r\n    - # EstimatedDocumentCount\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faamirmousavi%2Fmongoleaf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faamirmousavi%2Fmongoleaf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faamirmousavi%2Fmongoleaf/lists"}