首页 > 代码库 > newlisp通过JavaScript查询MongoDB记录

newlisp通过JavaScript查询MongoDB记录

因为MongoDB支持执行JavaScript文件查询,所以newlisp就可以通过这种方式获得查询结果。

下面是我的newlisp模块:

(context ‘MONGO_JS_TOOL)


(define (query db-name js-file)
  (set ‘cmd (format "mongo localhost/%s %s" db-name js-file))
  (set ‘r (exec cmd))
  (set ‘r (STRING_LIST:remove r "^MongoDB.+" 1))
  (set ‘r (STRING_LIST:remove r "^connecting.+" 1))
  (set ‘rl ‘())
  (set ‘line "")
  (dolist (e r)
    (set ‘line (append line (replace "\t" e "")))
    (if (= "}" e)
	(begin
	  (push line rl -1)
	  (set ‘line ""))))
  rl
  )

下面是调用命令:

(load "mongo.lsp")

(set ‘rl (MONGO_JS_TOOL:query "kfc" "strategy.js"))
(println rl)
执行结果是:

./schedule.lsp 
("{\"_id\" : ObjectId(\"53bbf667b78d338d48d0f86f\"),\"send_time\" : \"21:47\",\"text\" : \"afdfd\"}" 
 "{\"_id\" : ObjectId(\"53cb4c625f94f3ac8af92b46\"),\"send_time\" : \"06:29\",\"text\" : \"66gg\"}")



newlisp通过JavaScript查询MongoDB记录