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.

4

2 回答 2

5

您可以将每个文档处理为 Clojure 映射的惰性序列:

(require 'monger.collection)

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

你为什么要尝试使用 JavaScript?您是在使用 ClojureScript,还是只是在考虑如何从 mongodb shell 编写代码?

免责声明:在此处 使用doseq不起作用,并且可能不是惯用的,具体取决于您要完成的工作。使用maporreduce可能更好,我只是想写一些与您的forEach示例代码最接近的东西。

有关的:

于 2014-01-07T18:36:14.810 回答
3

您可以使用来自 monger 和eval mongodb 命令的命令功能

(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) { })}"})
于 2014-01-07T21:36:06.990 回答