{"id":19719934,"url":"https://github.com/timboudreau/mongo-promises","last_synced_at":"2025-04-29T21:30:43.356Z","repository":{"id":38310983,"uuid":"42791803","full_name":"timboudreau/mongo-promises","owner":"timboudreau","description":"Promise-based wrappers for MongoDB's async Java API","archived":false,"fork":false,"pushed_at":"2023-05-02T06:31:08.000Z","size":101,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T19:23:44.252Z","etag":null,"topics":["async","guice","java","mongodb","promises"],"latest_commit_sha":null,"homepage":null,"language":"Java","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/timboudreau.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}},"created_at":"2015-09-19T22:25:33.000Z","updated_at":"2021-12-20T07:12:26.000Z","dependencies_parsed_at":"2023-01-22T09:00:54.993Z","dependency_job_id":null,"html_url":"https://github.com/timboudreau/mongo-promises","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timboudreau%2Fmongo-promises","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timboudreau%2Fmongo-promises/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timboudreau%2Fmongo-promises/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timboudreau%2Fmongo-promises/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timboudreau","download_url":"https://codeload.github.com/timboudreau/mongo-promises/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251585739,"owners_count":21613270,"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":["async","guice","java","mongodb","promises"],"created_at":"2024-11-11T23:09:44.595Z","updated_at":"2025-04-29T21:30:42.981Z","avatar_url":"https://github.com/timboudreau.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"MongoDB Async Promises\n======================\n\nThis library provides wrappers around MongoDB's asynchronous Java API to make programming\nto it easier - particularly chaining together chunks of work that should happen in sequence,\nasynchronously.\n\nRead [the Javadoc](http://timboudreau.com/builds/job/mastfrog-parent/lastSuccessfulBuild/artifact/mongo-promises/target/apidocs/index.html)\n\nIt uses the [async promises](https://github.com/timboudreau/async-promises) library under the\nhood, and also provides builder classes for things like updates, which have fairly fussy and\nnon-typesafe syntax in MongoDB.\n\nTo use, simply create a `CollectionPromises\u003cT\u003e` over a native MongoDB `MongoCollection\u003cT\u003e`,\nand use the instance methods on that.\n\nThe point is to provide a clean abstraction for chaining together asynchronous database operations.\n\nExample\n-------\n\n```java\nAsyncPromise\u003cBson, Long\u003e promise = p.find().descendingSortBy(\"ix\").withBatchSize(20).find(new FindReceiver\u003cList\u003cDocument\u003e\u003e(){\n\n    int total = 0;\n\n    @Override\n    public void withResults(List\u003cDocument\u003e obj, Trigger\u003cBoolean\u003e trigger, PromiseContext context) throws Exception {\n        total += obj.size();\n        System.out.println(\"GET ONE BATCH \" + obj.size() + \" TOTAL \" + total);\n        all.addAll(obj);\n        trigger.trigger(true, null);\n    }\n}).then(new SimpleLogic\u003cVoid,Void\u003e(){\n\n    @Override\n    public void run(Void data, Trigger\u003cVoid\u003e next) throws Exception {\n        nextWasRun.set(true);\n        next.trigger(null, null);\n    }\n}).then(new Document(), p.count().maxTime(10, TimeUnit.SECONDS).count());\n\npromise.start(new Document(), new Trigger\u003cLong\u003e(){\n\n    @Override\n    public void trigger(Long count, Throwable thrown) {\n        countHolder.set(count);\n        waitForAllResults.countDown();\n    }\n});\n```\n\nSimilar code using JDK 8's lambads:\n\n```java\nAsyncPromise\u003cVoid, Long\u003e promise = p.query().equal(\"name\",\"foo\").build()\n    .descendingSortBy(\"ix\").withBatchSize(20).find(\n(List\u003cDocument\u003e obj, Trigger\u003cBoolean\u003e trigger, PromiseContext context)-\u003e {\n        all.addAll(obj);\n        trigger.trigger(true, null);\n}).then((Void data, Trigger\u003cVoid\u003e next) -\u003e {\n        nextWasRun.set(true);\n        next.trigger(null, null);\n}).then(new Document(), p.count().maxTime(10, TimeUnit.SECONDS).count())\n.start();\n```\n\nBuilders\n--------\n\nA number of builder classes are included to make things easy - in each case, they return an\nAsyncPromise you can start to execute the operation or chain it together with other operations.\n\nFor example, querying can use a nice `QueryBuilder` (which lets you specify things like $in, $gt,\npartial or complete subdocument matches, etc.):\n\n```java\n\nCollectionPromises\u003cDocument\u003e p = new CollectionPromises\u003c\u003e(someCollection);\n\np.query().equal(\"skidoo\", 23).embedded(\"foo\").embedded(\"bar\").equal(\"meaning\", 42)\n                .build()\n                .build()\n                .elemMatch(\"array\").equal(\"name\", \"Joe\").greaterThan(\"age\", 18).lessThan(\"age\", 65).build()\n                .in(\"city\", \"Boston\", \"New York\", \"Shutesbury\").build()\n\n```\n\nresults in a MongoDB query as follows:\n\n```\n{\n\tskidoo : 23, \n\tfoo.bar.meaning : 42, \n\tlogins : { \n\t\tcountry : USA \n\t}, \n\tcity : { \n\t\t$in : [Boston, New York, Shutesbury] \n\t}, \n\tpeople : {\n\t\t$elemMatch : {\n\t\t\tname : Joe, \n\t\t\tage : {\n\t\t\t\t$gt : 18, $lt : 65\n\t\t\t}\n\t\t}\n\t}\n}\n```\n\nThis then drops you into a `FindBuilder` which lets you configure cursor attributes like limit\nand sort order, which in turn builds a promise you can run.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimboudreau%2Fmongo-promises","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimboudreau%2Fmongo-promises","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimboudreau%2Fmongo-promises/lists"}