0

I'm using Monger to connect to Mongo. I want to run

db.collection.find().forEach(function(x) { db.collection-two.insert(...) })

I can't see a relevant 'for each' entry in the Monger docs or a way to pass in JavaScript to insert into another collection. How do I do this?

EDIT this question was based on a misunderstanding (forEach lies in the client API not on the server API). I would delete it but I can't.

Joe
  • 46,419
  • 33
  • 155
  • 245
  • 5
    I asked this based on faulty information, the question is invalid. (Can't find an appropriate flagging category, sorry) – Joe Jan 08 '14 at 11:36

2 Answers2

5

You can process each document as a lazy sequence of Clojure maps:

(require 'monger.collection)

(doseq [x (monger.collection/find-maps "your-collection")]
  ;you can process x here, which is a vanilla clojure map
  (println x))

Why are you trying to use JavaScript? Are you using ClojureScript, or are you just thinking in terms of how you would write code from the mongodb shell?

Disclaimer: Using doseq here is not functional, and probably not idiomatic depending on what you are trying to accomplish. Using map or reduce is probably better, I was just trying to write something that most closely aligned with your forEach example code.

Related:

dbyrne
  • 59,111
  • 13
  • 86
  • 103
  • Thanks. Not quite what I want though. I want to run this in the mongod process. The reason for this is largely bottlenecks. I'm going to do this on approx 10 million records, so it matters. On closer inspection it looks like this is a shell command not a server one, which renders my question invalid. – Joe Jan 07 '14 at 19:56
  • @Joe are you sure its faster to run this in the mongod process? I have no clue, but I am curious if you've actually run any performance tests? Of course that assumes your Clojure process is running locally and not on a remote server. – dbyrne Jan 07 '14 at 20:08
  • Well it's not possible to do it, so a moot point. But yes, I'm dealing with a lot of small bits of data. I've seen the time difference between single updates and big batches. I'm confident that it's worth figuring out making what I want to do happen on the server. Thanks for your answer, but the question was mistaken and I'll delete it. – Joe Jan 07 '14 at 20:14
3

You can use command function from monger and eval mongodb command

(ns user
  (:require [monger.core :as mg]
            [monger.core :as mc]))

;; (command {:eval "function() { db.collection.find().forEach() }"}

(mg/connect!)
(mg/set-db! (mg/get-db "test"))
(mc/command {:eval "function() { db.fs.files.find().forEach(function(x) { })}"})
edbond
  • 3,921
  • 19
  • 26