from-db-object returns WriteResult

116 views
Skip to first unread message

lawrenc...@gmail.com

unread,
Aug 22, 2014, 3:05:38 AM8/22/14
to clojure...@googlegroups.com
At the repl:

(require '[monger.conversion :refer :all])

(require '[monger.core :as mg]
            '[monger.collection :as mc])

(clojure.pprint/pprint (from-db-object (mc/insert database-object "User" { :hey "who"}) true))

returns:

#<WriteResult { "serverUsed" : "localhost:27017" , "n" : 0 , "connectionId" : 610 , "err" :  null  , "ok" : 1.0}>

Using Monger 2.0.0. 

I thought from-db-object was suppose to convert WriteResult to a Clojure data structure? That is what I read here: 




Michael Klishin

unread,
Aug 22, 2014, 3:28:34 AM8/22/14
to clojure...@googlegroups.com, lawrenc...@gmail.com
On 22 August 2014 at 11:05:38, lawrenc...@gmail.com (lawrenc...@gmail.com) wrote:
> > I thought from-db-object was suppose to convert WriteResult
> to a Clojure data structure? That is what I read here:
>
> https://github.com/michaelklishin/monger/issues/21

It converts DBObject and collections but not write results.

We have never been asked about converting WriteResults. 
--
@michaelklishin, github.com/michaelklishin

lawrenc...@gmail.com

unread,
Aug 22, 2014, 8:36:55 PM8/22/14
to clojure...@googlegroups.com, lawrenc...@gmail.com
>We have never been asked about converting WriteResults.  

I am confused. If I look here: 


Then, on that page, you wrote:

"WriteResults are indeed DBObject instances. You can convert them to Clojure maps using monger.conversion/from-db-object, just like with DBObjects, map/reduce results and command results."

But, as I showed in my first post, up above, when I try this at the repl, even after I run the return value through monger.conversion/from-db-object, it is still a WriteResult. 

I really only want to check the value of "ok". You have written that the Monger function ok? is deprecated and we should not use it. I don't really mind using the Java interop to deal directly with the WriteResult. I see the API here: 


I assume I can use getField("ok") to get the value of "ok". But is this what you recommend as best practice? 

lawrenc...@gmail.com

unread,
Aug 22, 2014, 8:44:09 PM8/22/14
to clojure...@googlegroups.com, lawrenc...@gmail.com
Testing the Java interop at the repl and all seems well:

(clojure.pprint/pprint (from-db-object (mc/insert database-object "User" { :hey "who"}) true))
#<WriteResult { "serverUsed" : "localhost:27017" , "n" : 0 , "connectionId" : 610 , "err" :  null  , "ok" : 1.0}>
nil

(def written (from-db-object (mc/insert database-object "User" { :hey "who"}) true))
#'user/written

(println (str (.getField written "ok")))
1.0

I guess this is how I'll proceed. 
Reply all
Reply to author
Forward
0 new messages