[英]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开发请求请求。
一,一般情况:
aldeed:meteor-simple-schema
使用aldeed:meteor-simple-schema
, aldeed:collection2
, accounts-password
和ejson
运行Meteor安装。 EJSON
通过增加RegExp
使用类型EJSON.addType
这些指示: 如何延长EJSON连载的正则表达式流星客户端-服务器的交互? "admin@foo.com"
在流星服务器代码上运行此命令(未预期结果) :
# 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!?
在MongoDB shell上运行此命令(绕过Meteor.users):
# mongo shell > db.users.find({ 'emails': { '$elemMatch': { 'address': /^adm/ } } }).count() # prints 1
尝试使用相同的数据进行独立的Mongo.Collection查找。
# server MyUsers = new Mongo.Collection 'myusers'
将用户文档从Meteor.users()复制到MyUsers集合中。
在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中展示的服务器代码有所不同? 我想知道是因为...
{ 'emails': { '$elemMatch': { 'address': /^adm/ } } }
包含Meteor.user
范围之外的相同文档 我在哪里可以看到(并试验)Meteor源代码中Collection.find()的服务器端定义?
这看起来确实像个错误。 请在http://github.com/meteor/meteor/issues上提交问题。 请务必遵循我们的复制准则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.