{"id":22008817,"url":"https://github.com/jiaoyinyi/ymongo","last_synced_at":"2026-05-11T07:47:35.719Z","repository":{"id":265366138,"uuid":"840305031","full_name":"jiaoyinyi/ymongo","owner":"jiaoyinyi","description":"erlang mongodb driver","archived":false,"fork":false,"pushed_at":"2024-11-29T03:51:06.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T07:29:42.345Z","etag":null,"topics":["erlang","mongodb"],"latest_commit_sha":null,"homepage":"","language":"Erlang","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jiaoyinyi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-09T12:09:15.000Z","updated_at":"2024-11-29T03:51:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"ac76a7ed-acb3-4f5c-9050-a3b7a642d3df","html_url":"https://github.com/jiaoyinyi/ymongo","commit_stats":null,"previous_names":["jiaoyinyi/ymongo"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jiaoyinyi/ymongo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaoyinyi%2Fymongo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaoyinyi%2Fymongo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaoyinyi%2Fymongo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaoyinyi%2Fymongo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jiaoyinyi","download_url":"https://codeload.github.com/jiaoyinyi/ymongo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiaoyinyi%2Fymongo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262240709,"owners_count":23280567,"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":["erlang","mongodb"],"created_at":"2024-11-30T02:07:20.415Z","updated_at":"2026-05-11T07:47:30.697Z","avatar_url":"https://github.com/jiaoyinyi.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"Erlang的简单mongodb驱动，基于OP_MSG协议实现。仅支持mongodb版本\u003e=2.7\r\n\r\n驱动主要分两个接口模块，mongo_api和mongo_pool_api。\r\n\r\n### mongo_api\r\n直接使用驱动连接进程的驱动接口\r\n\r\n#### 建立连接\r\n```\r\n%% Opts :: #{\r\n%%     host =\u003e inet:socket_address() | inet:hostname()\r\n%%     port =\u003e inet:port_number()\r\n%%     ssl =\u003e boolean()                                    %% use ssl connect or not\r\n%%     ssl_opts =\u003e [ssl:tls_client_option()]\r\n%%     database =\u003e binary()\r\n%%     login =\u003e binary()                                   %% login username\r\n%%     password =\u003e binary()                                %% login password\r\n%%     auth_source =\u003e binary()                             %% auth from where database\r\n%% }\r\n\r\nOpts = #{\r\n    host =\u003e \"127.0.0.1\", port =\u003e 27017, database =\u003e \u003c\u003c\"test_1\"\u003e\u003e, \r\n    login =\u003e \u003c\u003c\"test\"\u003e\u003e, password =\u003e \u003c\u003c\"test\"\u003e\u003e, auth_source =\u003e \u003c\u003c\"admin\"\u003e\u003e\r\n},\r\n{ok, Conn} = mongo_api:connect(Opts).\r\n```\r\n\r\n#### 断开连接\r\n```\r\nok = mongo_api:close(Conn).\r\n```\r\n\r\n#### 创建索引\r\n```\r\nIndexes = #{\u003c\u003c\"key\"\u003e\u003e =\u003e #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 1, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e 1}, \u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"role_id\"\u003e\u003e, \u003c\u003c\"unique\"\u003e\u003e =\u003e true},\r\n{ok, _} = mongo_api:create_index(Conn, \u003c\u003c\"role\"\u003e\u003e, Indexes).\r\n```\r\n\r\n#### 删除索引\r\n```\r\n{ok, _} = mongo_api:drop_index(Conn, \u003c\u003c\"role\"\u003e\u003e, \u003c\u003c\"role_id\"\u003e\u003e).\r\n```\r\n\r\n#### 增删改查\r\n```\r\n%% insert\r\nDoc = #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 1, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mark\"\u003e\u003e},\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 1}} = mongo_api:insert_one(Conn, \u003c\u003c\"role\"\u003e\u003e, Doc).\r\n\r\nDocs = [#{\u003c\u003c\"rid\"\u003e\u003e =\u003e 2, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mark\"\u003e\u003e},\r\n        #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 3, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mary\"\u003e\u003e}],\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 2}} = mongo_api:insert_many(Conn, \u003c\u003c\"role\"\u003e\u003e, Docs).\r\n\r\n%% find\r\n{ok, #{\u003c\u003c\"rid\"\u003e\u003e := 1, \u003c\u003c\"srv_id\"\u003e\u003e := \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e := \u003c\u003c\"Mark\"\u003e\u003e}} \r\n    = mongo_api:find_one(Conn, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 1, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e}, #{}).\r\n\r\n{ok, Cursor} = mongo_api:find_many(Conn, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mark\"\u003e\u003e}, #{}).\r\n[#{\u003c\u003c\"rid\"\u003e\u003e := 1, \u003c\u003c\"srv_id\"\u003e\u003e := \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e := \u003c\u003c\"Mark\"\u003e\u003e},\r\n #{\u003c\u003c\"rid\"\u003e\u003e := 2, \u003c\u003c\"srv_id\"\u003e\u003e := \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e := \u003c\u003c\"Mark\"\u003e\u003e}]\r\n  = mongo_cursor:rest(Cursor).\r\n\r\n%% count\r\n{ok, Count} = mongo_api:count(Conn, \u003c\u003c\"role\"\u003e\u003e, #{}).\r\n\r\n%% update\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 1}} = mongo_api:update_one(Conn, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 3, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e}, #{\u003c\u003c\"$set\"\u003e\u003e =\u003e #{\u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Test\"\u003e\u003e}}).\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 2}} = mongo_api:update_many(Conn, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mark\"\u003e\u003e}, #{\u003c\u003c\"$set\"\u003e\u003e =\u003e #{\u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Test\"\u003e\u003e}}).\r\n\r\n%% delete\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 1}} = mongo_api:delete_one(Conn, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 3, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e}).\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 2}} = mongo_api:delete_many(Conn, \u003c\u003c\"role\"\u003e\u003e, #{}).\r\n```\r\n\r\n#### 其他操作\r\n```\r\n{ok, Doc} = mongo_api:command(Conn, Command).\r\n{ok, Doc} = mongo_api:database_command(Conn, Db, Command).\r\n```\r\n\r\n### mongo_pool_api\r\n具有简单连接池的驱动接口\r\n\r\n#### 创建连接池\r\n```\r\n{ok, PoolPid} = mongo_pool_api:start_pool(PoolName, PoolArgs, DbArgs).\r\n```\r\n\r\n#### 移除连接池\r\n```\r\nok = mongo_pool_api:stop_pool(PoolName).\r\n```\r\n\r\n#### 创建索引\r\n```\r\nIndexs = #{\u003c\u003c\"key\"\u003e\u003e =\u003e #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 1, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e 1}, \u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"role_id\"\u003e\u003e, \u003c\u003c\"unique\"\u003e\u003e =\u003e true},\r\n{ok, _} = mongo_pool_api:create_index(PoolName, \u003c\u003c\"role\"\u003e\u003e, Indexes).\r\n```\r\n\r\n#### 删除索引\r\n```\r\n{ok, _} = mongo_pool_api:drop_index(PoolName, \u003c\u003c\"role\"\u003e\u003e, \u003c\u003c\"role_id\"\u003e\u003e).\r\n```\r\n\r\n#### 增删改查\r\n```\r\n%% insert\r\nDoc = #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 1, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mark\"\u003e\u003e},\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 1}} = mongo_pool_api:insert_one(PoolName, \u003c\u003c\"role\"\u003e\u003e, Doc).\r\n\r\nDocs = [#{\u003c\u003c\"rid\"\u003e\u003e =\u003e 2, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mark\"\u003e\u003e},\r\n        #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 3, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mary\"\u003e\u003e}],\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 2}} = mongo_pool_api:insert_many(PoolName, \u003c\u003c\"role\"\u003e\u003e, Docs).\r\n\r\n%% find\r\n{ok, #{\u003c\u003c\"rid\"\u003e\u003e := 1, \u003c\u003c\"srv_id\"\u003e\u003e := \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e := \u003c\u003c\"Mark\"\u003e\u003e}} \r\n    = mongo_pool_api:find_one(PoolName, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 1, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e}, #{}).\r\n\r\n{ok, Cursor} = mongo_pool_api:find_many(PoolName, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mark\"\u003e\u003e}, #{}).\r\n[#{\u003c\u003c\"rid\"\u003e\u003e := 1, \u003c\u003c\"srv_id\"\u003e\u003e := \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e := \u003c\u003c\"Mark\"\u003e\u003e},\r\n #{\u003c\u003c\"rid\"\u003e\u003e := 2, \u003c\u003c\"srv_id\"\u003e\u003e := \u003c\u003c\"test_1\"\u003e\u003e, \u003c\u003c\"name\"\u003e\u003e := \u003c\u003c\"Mark\"\u003e\u003e}]\r\n  = mongo_cursor:rest(Cursor).\r\n\r\n%% count\r\n{ok, Count} = mongo_pool_api:count(PoolName, \u003c\u003c\"role\"\u003e\u003e, #{}).\r\n\r\n%% update\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 1}} = mongo_pool_api:update_one(PoolName, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 3, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e}, #{\u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Test\"\u003e\u003e}).\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 2}} = mongo_pool_api:update_many(PoolName, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Mark\"\u003e\u003e}, #{\u003c\u003c\"name\"\u003e\u003e =\u003e \u003c\u003c\"Test\"\u003e\u003e}).\r\n\r\n%% delete\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 1}} = mongo_pool_api:delete_one(PoolName, \u003c\u003c\"role\"\u003e\u003e, #{\u003c\u003c\"rid\"\u003e\u003e =\u003e 3, \u003c\u003c\"srv_id\"\u003e\u003e =\u003e \u003c\u003c\"test_1\"\u003e\u003e}).\r\n{ok, #{\u003c\u003c\"n\"\u003e\u003e := 2}} = mongo_pool_api:delete_many(PoolName, \u003c\u003c\"role\"\u003e\u003e, #{}).\r\n```\r\n\r\n#### 其他操作\r\n```\r\n{ok, Doc} = mongo_pool_api:command(PoolName, Command).\r\n{ok, Doc} = mongo_pool_api:database_command(PoolName, Db, Command).\r\n```\r\n\r\n### 扩展\r\n详细指令可在mongodb手册查询 https://www.mongodb.com/docs/manual/reference/command/","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjiaoyinyi%2Fymongo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjiaoyinyi%2Fymongo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjiaoyinyi%2Fymongo/lists"}