EzDevInfo.com

congomongo

Clojure wrapper for the mongo-db java api

Congomongo fetch returns nil

I have simple app that should return single record from Mongo database.

(def movie (m/fetch-one :movie
                        :where {:_id id}))

id is correct but i keep getting nil as a return from this. Here is how my :_id looks like

:_id #<ObjectId 5245ca7d44aed3e864a1c830>

I guess my problem is here somewhere, but I just don't have enough experience with Clojure to find an error

In this case id passed to where is 5245ca7d44aed3e864a1c830


Source: (StackOverflow)

Congomongo "in" query

How can I use the MongoDB $in query operator in a Congomongo query? This was my unsuccessful guess—I couldn't find docs on the query syntax anywhere.

(fetch :solutions
       :where {:user {:in [1 2 3]}})

Source: (StackOverflow)

Advertisements

Regex in :where clause Congomongo

I am trying to query Mongodb for documents containing part of title that is passed through form.

(defn findmovie
 "Find movie by query"
 [query]
 (def search (str "#.*" (url-decode query) ".*"))
 (println search)
 (def movies (m/fetch :movie :where {:name {:$regex search}}))
 (println movies)
 (returnall movies)
)

query here is a parameter. Above code returns empty collection () no matter what I type in.

Example value for :name is Gladiator (2000) and :name in :where is something like #.*gladiator.*


Source: (StackOverflow)

How to fetch data from multiple MongoDB Collections using Clojure Map

I am using clojure to fetch data from multiple MongoDB collections.

(ns mongofetch
  (:require [somnium.congomongo :as m]))

(m/with-mongo (m/make-connection "testdb") (mapv m/fetch coll))

Here, coll is the vector where collection-names are stored. This is working fine as it fetches data from multiple MongoDB collections and returns a vector comprising of sequences (returns sequences for each collection) with documents. But, I'm getting an error while using map instead of mapv (for following code-snippet).

(m/with-mongo (m/make-connection "testdb") (map m/fetch coll))

The error is - java.lang.AssertionError: Assert failed: (connection? conn)

Why is it throwing an AssertionError and how it can be resolved using map only?


Source: (StackOverflow)

Latest 2.0.6 MongoDB silently ignores $set modifiers, but only for certain objects

I'm using the 1.0.6 Congomongo library to update objects in MongoDB 2.0.6. An application failure was traced to a silent failure of a $set command to update an object.

Take an object with a field:

(fetch :test {:_id "myid"})
=> {:sad false :_id "myid"}

(update! :test
         {:_id "myid"}
         {:$set {:sad true}})
=> Writeresult { "serverUsed" : "127.0.0.1:27017" , "updatedExisting" : true , "n" : 1 , "connectionId" : 10 , "wtime" : 0 , "err" :  null  , "ok" : 1.0}

(fetch :test {:_id "myid"})
=> {:sad false :_id "myid"}

This simple scenario only fails on objects that have undergone a fair number of modifications (e.g. dozens of small objects and changes to slots via sets of path updates), so I haven't found a reliable way to reproduce it on a fresh object yet. To work around this problem I have to do fetch / modify / write operations on the whole object instead.

One other interesting observation is I can get the $set to work above if I add an $inc to the modifier, but then the $inc doesn't work. I can also reproduce this in the mongo console, so it's unlikely a Congomongo adapter or Java driver problem.

e.g. same object from mongo command line

db.user.findAndModify({ query: {username: 'foo'}, update: {$set: {happy: 20}}, new: true}).happy
=> false

db.user.findAndModify({ query: {username: 'foo'}, update: {$set: {happy: 20}, $inc: {counter: 1}}, new: true}).happy
=> 20

I'm at a loss. Any clues as to whether this is a known bug (didn't find anything in JIRA) or what I might have done to the object in question to cause this behavior?

Edit/Update

Upgrading to 2.1.0 unstable resolved this particular bug.


Source: (StackOverflow)