在流星服务器上的mongodbdistinct()实现? [英] mongodb distinct() implementation in Meteor on the server?

查看:47
本文介绍了在流星服务器上的mongodbdistinct()实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Meteor的mongodb中获取不同的值(基本上,实现mongodb本机的distinct()调用).在客户端, Meteor是否有独特的集合查询?-这就像一个魅力.但是我不知道如何在服务器端获得类似的工作.感谢任何帮助.谢谢!

I need to fetch distinct values from the mongodb in Meteor (basically, implementing mongodb native distinct() call). On client side, Does Meteor have a distinct query for collections? - this works like a charm. But I can't figure out how to get something similar to work on the server side. Appreciate any help. Thanks!

推荐答案

好吧,在深入研究代码并意识到mongo lib包含所有必需方法的本机实现之后,我重用了

Ok after some digging around the code and realizing mongo lib contains native implementations of all the needed methods I reused the aggregate() solution from https://github.com/meteor/meteor/pull/644

直接更改并将其翻译为coffeescript可以将以下代码段添加到服务器端代码中:

Straightforward changes and translation to coffeescript gives the following snippet to put into your server side code:

path = __meteor_bootstrap__.require("path")
MongoDB = __meteor_bootstrap__.require("mongodb")
Future = __meteor_bootstrap__.require(path.join("fibers", "future"))

myCollection = new Meteor.Collection "my_collection"

#hacky distinct() definition from https://github.com/meteor/meteor/pull/644
myCollection.distinct = (key)->
  future = new Future
  @find()._mongo.db.createCollection(@_name,(err,collection)=>
    future.throw err if err
    collection.distinct(key, (err,result)=>
      future.throw(err) if err
      future.ret([true,result])
      )
    )
  result = future.wait()
  throw result[1] if !result[0]
  result[1]

缺点是您必须为每个新集合定义它,但这很容易通过_.extend或我猜到的东西来修复另一个黑客.

Downside is you have to define it for every new collection but that's pretty straightforward to fix with another hack via _.extend or something I guess...

PS它现在也是一个智能软件包-mrt add mongodb-aggregation

PS It's also now a smart package - mrt add mongodb-aggregation

这篇关于在流星服务器上的mongodbdistinct()实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆