I am having issues configuring a MongoDB database inside a Luminus project. This should be pretty straightforward given the lein templates: https://github.com/yogthos/luminus-template. Typing lein new luminus <name> +mongodb
will give you a default mongoDB setup which is the file: src/app-name/db/core.clj
To run the server, type lein ring server
which should open a web browser and point it to localhost:3000
by default.
A default home page will be displayed, and for me, it tells me that "MongoDB configuration is Required." It tells me that I can configure it in the same file: src/app-name/db/core.clj.
I have tried a number of different things, but what I am currently trying and what makes the most sense to me is the following:
(defonce coll "collection-name")
(defonce db (let [uri "mongodb://127.0.0.1/db-name"
{:keys [conn db]} (mg/connect-via-uri uri)]
db))
Unfortunately, when I connect my browser, I still get the same "MongoDB configuration is Required" message. I have also tried using CURL and various HTTP routes defined in my application that access the database without success. What is odd though, is that this works in the REPL.
EDIT: To be more clear, here is the example in the REPL:
clj-project-name.db.core> (get-replies 2)
["mew-mew" [1.0 "hello"]]
In the code I have the following pieces:
(ns clj-project-name.routes.home
(:require [compojure.core :refer :all]
[clj-project-name.layout :as layout]
[clj-project-name.util :as util]
[clj-project-name.db.core :as project-db]))
(defn get-replies [id] (mc/distinct db coll "replies" {:_id id}))
(GET "/user" [id] (user-page id)) ; defined in home-routes inside namespace clj-project-name.routes.home
(defn user-page [& [id]] ;defined inside namespace clj-project-name.routes.home
(layout/render "user.html"
{:id id
:replies (projectl-db/get-replies id)}))
<h1>User {{id}}'s page</h1> ; part of the HTML template
<p> <b>Replies:</b> {{replies}} </p>
Here is the page loaded in the browser:
As we can see, the replies
list is empty, when it should be ["mew-mew" [1.0 "hello"]]
as we saw in the REPL.
EDIT: Another oddity is that just when the browser is loaded after typing lein ring server
I can see the following output from mongodb
in the terminal:
2014-12-02T21:16:57.941-0500 [initandlisten] connection accepted from 127.0.0.1:38854 #28 (5 connections now open).
What else can I do to get connected to MongoDB? Thanks for your help.
I followed your steps and created new Luminus project using luminus template.
I also studied generated code an found, that default home page is 100% static. So, it shows
MongoDB Configuration is Required
regardless of whether it actually configured or not:In other words, it just renders
resources/public/md/docs.md
into.html
and shows it, always the same html page.As for your configuration, it's absolutely fine.
As for your
user.html
page, the actual problem is thatid
inuser-page
route is a string, while_id
in your database is a number. So, instead of(get-replies 2)
you're calling(get-replies "2")
. Try using stringified_id
s, or parse incomingid
s withread-string
function orLong/parseLong
first:I would recommend using stringified
_id
s, because it's easier and safer than parsing strings into numbers.