2015-09-20 32 views
0

我嘗試使用Meteor.publish(服務器端)與此查詢:當我做與Meteor.subscribe客戶端的查詢MeteorJS發佈查詢不能

return Meteor.users.find({_id:{$ne:this.userId}}); 

,它的工作原理:

return Meteor.users.find({_id:{$ne:Meteor.userId()}}); 

爲什麼它不能在服務器端工作...似乎我只能在客戶端查詢一次它......問題是,我不想下載整個集合,因爲我將擁有超過20,000名用戶。發佈方法不允許「$」查詢嗎?

而且我怎麼能重視這個我下面的查詢語句:

return Meteor.users.find({"profile.loc":{ $near: [ to[0].profile.loc.lat, to[0].profile.loc.lon ], $maxDistance: (1/111.2)*250}}); 
+0

一切按預期工作。如果您不希望在客戶端上發佈所有20000個用戶,則不會發布它。閱讀更多關於發佈/訂閱的信息。 – ZuzEL

回答

0

publish方法確實允許$查詢。您的出版物正在做的是發佈所有ID不等於this.userId的用戶。如果您有20,000個用戶,則此方法將爲19,999個用戶發佈數據。

如果你只想發佈則當前用戶的數據嘗試:

Meteor.publish('userData', function() { return Meteor.users.find(this.userId) }; 
1

如上州的答案,如果你做你寫的,你無論如何都會發布19,999用戶。

你的問題是兩個部分,雖然,你應該有一個查詢同時滿足:要做到這一點,你應該有帶參數的發佈:

Meteor.publish('users', function(location) { 
     return Meteor.users.find(
         {_id: {$ne: this.userId}, 
          "profile.loc":{ 
          $near: [ location.lat, location.lon ], 
          $maxDistance: (1/111.2)*250} 
          } 
         }); 

這將篩選符合誰的服務器上的用戶位置標準。

在您訂閱其與客戶端:

location = {lon: 12.123, lat: 110.2}; 
Meteor.subscribe('users', location); 

或您所選擇的對象。