Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何指定应使用哪个Meteor订阅在React中进行渲染?_Mongodb_Meteor_Reactjs - Fatal编程技术网

Mongodb 如何指定应使用哪个Meteor订阅在React中进行渲染?

Mongodb 如何指定应使用哪个Meteor订阅在React中进行渲染?,mongodb,meteor,reactjs,Mongodb,Meteor,Reactjs,我对同一集合的不同视图有多个订阅和发布。其中一个视图用于返回Mongo集合上的文本搜索,而其他视图则返回整个集合 我的问题是,当我尝试在客户端上查看结果时,我不确定如何指定使用搜索结果订阅。当前,我的客户端视图显示的是整个集合,而不是受限的搜索结果。如何指定应使用的订阅?还是我误解了酒吧/酒吧模式?可能我应该只使用一个订阅?欢迎任何意见 // Server side publication Meteor.publish("search", function(searchValue){ if

我对同一集合的不同视图有多个订阅和发布。其中一个视图用于返回Mongo集合上的文本搜索,而其他视图则返回整个集合

我的问题是,当我尝试在客户端上查看结果时,我不确定如何指定使用搜索结果订阅。当前,我的客户端视图显示的是整个集合,而不是受限的搜索结果。如何指定应使用的订阅?还是我误解了酒吧/酒吧模式?可能我应该只使用一个订阅?欢迎任何意见

// Server side publication
Meteor.publish("search", function(searchValue){
  if (!searchValue) {
    console.log("there is no search value");
    return remoteData.find({});
  }

  console.log("there is a search value and it is " + searchValue);
  return remoteData.find({$text:{$search: searchValue}});
});

Meteor.publish("allData", function (){
  return remoteData.find();
});

// Client Side subscription
var searchSubscription =  Meteor.subscribe("search", searchQuery);
var allDataSubscription =  Meteor.subscribe("allData");

// inside React Component 
// this returns everything, so I think it's using the allDataSubscription
filteredData() {
  return (
    remoteData.find({}).fetch();
  )
}

无法指定来自同一集合的不同订阅的数据。这是因为Meteor将来自后端集合的所有订阅的数据合并到前端的同一个Minimongo集合,并且不同订阅的数据之间没有区别

不幸的是,对于您的情况,这意味着没有可靠的方法来反应性地使用Minimongo当前不支持的
$text
运算符。我建议将其替换为在前端工作的
$regex
运算符,或者编写Meteor方法返回所有匹配文档的ID,并在前端使用此列表,如:

remoteData.find({u id:{$in:matchingIds}).fetch();

当搜索参数更改时,您必须重新调用此方法,因为它不是被动的。

无法指定来自同一集合的不同订阅的数据。这是因为Meteor将来自后端集合的所有订阅的数据合并到前端的同一Minimongo集合,来自不同订阅的数据之间没有区别

不幸的是,对于您的情况,这意味着没有可靠的方法来反应性地使用Minimongo当前不支持的
$text
运算符。我建议将其替换为在前端工作的
$regex
运算符,或者编写Meteor方法返回所有匹配文档的ID,并在前端使用此列表,如:

remoteData.find({u id:{$in:matchingIds}).fetch();

当搜索参数更改时,您必须重新调用此方法,因为它不是被动的。

在您的情况下,您实际上不需要有两个单独的订阅。只要将内容合并到一个订阅中,就不会有任何疑问。例如:

// Server
Meteor.publish('myData', function (searchValue) {
  const selector = {};
  if (searchValue) {
    selector.$text = {
      $search: searchValue,
    };
  }
  return remoteData.find(selector);
});

// Client
const myDataHandle =  Meteor.subscribe('myData', searchQuery);
...
remoteData.find({}).fetch();

在您的情况下,实际上不需要有两个单独的订阅。只要将内容合并到一个订阅中,就不会有任何疑问。例如:

// Server
Meteor.publish('myData', function (searchValue) {
  const selector = {};
  if (searchValue) {
    selector.$text = {
      $search: searchValue,
    };
  }
  return remoteData.find(selector);
});

// Client
const myDataHandle =  Meteor.subscribe('myData', searchQuery);
...
remoteData.find({}).fetch();

如果订阅了所有数据,所有数据都将以minimongo格式提供。因此,搜索可以在本地执行。问题是minimongo目前不支持$text,据我所知,唯一的解决方法是在服务器端执行$text搜索。如果所有数据都已订阅,所有数据都将在minimongo中可用。因此,搜索可以在本地执行。问题是minimongo目前不支持$text,据我所知,唯一的解决方法是在服务器端进行$text搜索。这非常聪明。谢谢,这是一个非常优雅的解决方案。这非常聪明。谢谢,这是一个非常优雅的解决方案,我没有考虑使用$regex,我将对此进行研究。谢谢我没有考虑过使用$regex,我将对此进行调查。谢谢