繁体   English   中英

为什么$ elemMatch MongoDB查询在具有扩展RexExp EJSON类型的客户端和服务器上的行为不同?

[英]Why does a $elemMatch MongoDB query behave differently on client and server with an extended RexExp EJSON type?

这是一个非常专业的案例,但是与任何想要使用Meteor中的发布和订阅来利用MongoDB的RegExp支持的查找(从客户端(Mini-Mongo)到服务器(MongoDB))的人都非常相关。 我想了解为什么服务器和客户端MongoDB查询功能之间存在差异,并且如果需要A)和B)对其他开发人员有利,则可以向Meteor开发请求请求。

一,一般情况:

  1. aldeed:meteor-simple-schema使用aldeed:meteor-simple-schemaaldeed:collection2accounts-passwordejson运行Meteor安装。
  2. 扩展 EJSON通过增加RegExp使用类型EJSON.addType这些指示: 如何延长EJSON连载的正则表达式流星客户端-服务器的交互?
  3. 添加一个具有电子邮件地址"admin@foo.com"
  4. 在流星服务器代码上运行此命令(未预期结果)

     # anywhere on the server if Meteor.isServer user_selector = { 'emails': { '$elemMatch': { 'address': /^adm/ } } } user_count = Meteor.users.find(user_selector).count() console.log user_count # prints 0 WHY!? 
  5. 在MongoDB shell上运行此命令(绕过Meteor.users):

     # mongo shell > db.users.find({ 'emails': { '$elemMatch': { 'address': /^adm/ } } }).count() # prints 1 
  6. 尝试使用相同的数据进行独立的Mongo.Collection查找。

     # server MyUsers = new Mongo.Collection 'myusers' 
  7. 将用户文档从Meteor.users()复制到MyUsers集合中。

  8. 在Meteor服务器(ALSO UNEXPECTED)上运行此命令:

     # anywhere on the server if Meteor.isServer user_selector = { 'emails': { '$elemMatch': { 'address': /^adm/ } } } user_count = MyUsers.find(user_selector).count() console.log user_count # prints 0 WHY!? 

此外,如果我发布所有用户,但对客户端代码(例如,在模板帮助程序参数中)运行完全相同的查询,则该查询将返回预期的计数。

II。 发布/订阅模板帮助程序案例:

  • 发布和订阅用户。

     # Server Meteor.publish 'all_users', -> return Meteor.users.find({}) # Client Template.people.onCreated -> self = @ self.peopleReady = new ReactiveVar() self.autorun -> users = Meteor.subscribe 'all_users' self.peopleReady.set users.ready() Template.people.helpers user_count: -> user_selector = { 'emails': { '$elemMatch': { 'address': /^adm/ } } } user_count = Meteor.users.find(user_selector).count() console.log user_count # prints 1 return user_count 

为什么上面I.4中展示的服务器代码有所不同? 我想知道是因为...

  • EJSON已正确扩展。
  • MiniMongo会正确选择并计数客户端上的记录。
  • 使用MyUser集合(由Mongo.Collection实例化)也无法报告服务器上的正确计数但是使用此查询选择器中的扩展RegExp EJSON类型在客户端上成功运行{ 'emails': { '$elemMatch': { 'address': /^adm/ } } }包含Meteor.user范围之外的相同文档

我在哪里可以看到(并试验)Meteor源代码中Collection.find()的服务器端定义?

这看起来确实像个错误。 请在http://github.com/meteor/meteor/issues上提交问题。 请务必遵循我们的复制准则。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM