How do I add a document to mongodb with monger and Clojure?

363 Views Asked by At

I have never used MongoDb before, nor have I used Monger before. I am working on a Clojure app. I simply want to be able to add one document, as an upsert. This is some of my code:

(defn convert-data-to-be-persisted [session-data]
  (convert/to-db-object session-data))

(defn persist-data-to-database [converted-document-to-persist]
  (mc/update "timeout_discovery" {:sessions converted-document-to-persist} {} :upsert true))

I am using monger:

http://clojuremongodb.info/

The println and timbre/spy statements I have are printing this data to the terminal output:

in persist-data-to-database: { "serverUsed" : "/127.0.0.1:27017" , "updatedExisting" : false , "upserted" : { "$oid" : "510680444077296acfa67d6b"} , "n" : 1 , "connectionId" : 4 , "err" : null , "ok" : 1.0}

If I log into MongoDb at the command line, it seems the collection remains empty:

db.timeout_discovery.find() { "_id" : ObjectId("51017c5fa86c7bcc3d423c4d"), "timeout" : "discovery" }

There is one document, but it has nothing in it except "timeout" and "discovery".

I looked here to try to figure out what to do, but every variation that I tried failed:

http://clojuremongodb.info/articles/updating.html

This line:

(convert/to-db-object session-data))

returns this document (but I shorten it here, as the original is long, bloated with more of the HTML that you see here):

in get-data-to-be-persisted the document to persist is: { "e6e20a2c-cb46-498c-b2f2-743e2b38b917" : { "itinerary-as-string" : "\n \t\n \t\t

http://www.super.com/mexico/bars/2012-food-drink-award-nominees-best\" class=\"fn given-name url\">2012 Food & Drink award nominees: Best new beer bar

\n \t\t \t\t \n \n Voting is now closed\nSEE RESULTS HERESee more in Restaurants + Bars

\n \t\n \t\n http://www.super.com/mexico/bars/2012-food-drink-award-nominees-best-new-beer-bar\">http://media.super.com/images/100131935/150/113/image.jpg\" /> \n \n \n \t\n \t\t

http://www.super.com/mexico/sex-dating/summer-dates-in-mexico-bars-and-restaurants\" class=\"fn given-name url\">Summer dates : Food-and-drink dates at MEXICO bars and restaurants

\n \t\t \t\t

Sex & dating

\n \n \n dinner or drinks at MEXICO hot spots on these summer dates. If you’re going to woo a food-obsessed partner, go beyond the standard dinner and a movie this season. Check out five food-and-drink-focused summer dates at New York...

\n \t\n \t\n http://www.super.com/mexico/sex-dating/summer-dates\">http://media.super.com/images/100453911/150/113/image.jpg\" /> \n \n \n \t\n \t\t

http://www.super.com/mexico/restaurants/organic\" class=\"fn given-name url\">Organic

\n \t\t \t\t

Food & Drink, American, Asian

\n \n \n \n $\n \n \n \n \n \n https://foursquare.com/intent/venue.html\" data-context=\"vcard-Content-Venue-50137\">Save to foursquare\n \n \n \n Tribeca fro-yo fanatics can satisfy all their wholesome cravings at the second location of this organic health-food bar. The bright takeout shop—outfitted with wood paneling and a white-and-green counter—also serves fresh-squeezed juices and smoothies,...

Edit \n \t \n \t\n \t\n http://www.super.com/mexico/restaurants/organic\">http://media.super.com/images/100461855/150/113/image.jpg\" /> \n \n \n \n \t \n 275 Greenwich St, (between Murray and Warren Sts), New York, 10007\n \t \n \t \t \n \t \tAverage course: $9. AmEx, Disc, MC,... \t \n \n http://www.super.com/mexico/restaurants/organic\">\n \t\t\t\t\t\t\t \t \t \tGet info\n \n \n\t\t " , "username" : "[email protected]" , "created-at" : "2013-01-28T13:41:53" , "ip-address" : "10.0.1.53" , "cookie-id" : "timeout-kiosk5903266" , "questions-and-answers" : { "what-are-you-interested-in" : [ ":food-and-bars"] , "who-are-you-with" : [ ":adults"] , "what-is-your-email" : "[email protected]" , "how-long-are-you-in-new-york" : ":two-weeks"}}}

So, why would this not save?

I am not seeing any errors in the terminal output, though if there is an error, I would like suggestions about how to capture it and see it.

If there are no errors, do I simply have the syntax wrong for MongoDb or monger?

1

There are 1 best solutions below

0
On

Author of Monger here. What environment do you run this code in? It may be an authentication issue. Monger uses "safe" write concern by default so writes will never fail silently (as far as Monger goes, you still have to handle exceptions). According to the server response you've posted, the write succeeded just fine (you can see "ok": 1.0 in the response document).

Please refer to Monger's documentation if you have questions about function arguments. I highly recommend starting with the Getting Started guide in the REPL.