如何在Mongo(meteor)中执行查找,找到数组匹配的文档?

时间:2015-11-22 16:59:41

标签: arrays mongodb meteor

我正在尝试在Meteor中返回一个Cursor(使用MongoDB)。

我正在寻找文件(MessageThreads集合),其中参与者的字段与我传入的数组完全匹配。

以下是MessageThread文档示例

TypeError("'str' object is not callable",)
TypeError("'str' object is not callable",)
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate
    req.respond()
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond
    self.server.gateway(self).respond()
INFO     2015-11-22 16:44:48,554 module.py:809] default: "POST /admin/filemanager HTTP/1.1" 500 -
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 2115, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 272, in __call__
    return app(environ, start_response)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/request_rewriter.py", line 314, in _rewriter_middleware
    response_body = iter(application(environ, wrapped_start_response))
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 154, in __call__
    response = self.handle_normal_request(environ)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 184, in handle_normal_request
    self._PYTHON_LIB_DIR)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/runtime.py", line 152, in HandleRequest
    error)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 329, in HandleRequest
    return WsgiRequest(environ, handler_name, url, post_data, error).Handle()
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 279, in Handle
    logging.exception('')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 1609, in exception
    error(msg, *args, **kwargs)
TypeError: 'str' object is not callable
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate
    req.respond()
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond
    self.server.gateway(self).respond()
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/cherrypy/cherrypy/wsgiserver/wsgiserver2.py", line 2115, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 272, in __call__
    return app(environ, start_response)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/request_rewriter.py", line 314, in _rewriter_middleware
    response_body = iter(application(environ, wrapped_start_response))
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 154, in __call__
    response = self.handle_normal_request(environ)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/request_handler.py", line 184, in handle_normal_request
    self._PYTHON_LIB_DIR)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/runtime.py", line 152, in HandleRequest
    error)
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 329, in HandleRequest
    return WsgiRequest(environ, handler_name, url, post_data, error).Handle()
  File "/Applications/GoogleAppEngineLauncher 2.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 279, in Handle
    logging.exception('')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 1609, in exception
    error(msg, *args, **kwargs)
TypeError: 'str' object is not callable
INFO     2015-11-22 16:44:48,647 module.py:809] default: "GET /favicon.ico HTTP/1.1" 200 15086

当我执行addMessage方法时,我首先尝试检查是否存在{ "_id": "YYSELCguhLurTeyNY", "creator": "RLmnidY6GypdqDXZu", "participants": [ "SNhRq4vQpwFBjnTSH", "RLmnidY6GypdqDXZu" ], } 与我传入的数组完全匹配的线程。此数组将根据新消息格式participants制定领域。

因此,只有当to的所有数组都在文档participants字段内而不是其他文件时,才会返回文档。例如:如果文档中存在不属于新消息participants字段的第三人,则不应返回该文档。

目前这是我的查询,显然过于简单。

to

任何指针?谢谢

编辑:我在使用提供的重复答案时遇到问题(但尚未允许对其他帖子发表评论)

由于某种原因,如果数组不同但existingThread = MessageThreads.findOne(participants: participants) 为真,existingThread仍在查找文档。

编辑2:

以下是我的整个方法的代码,如果它可以帮助破译我出错的地方。在coffeescript中(请原谅标签,不能让它在SO中工作,对不起)。

size

1 个答案:

答案 0 :(得分:0)

您需要在查询中使用 $all 运算符,该运算符选择字段值为包含所有指定元素的数组的文档。由于要返回游标, find() 方法更合适,因为它返回游标。它不会立即访问数据库或返回文档。要访问游标中的文档,游标会提供fetch()以返回所有匹配的文档,map()forEach()以迭代所有匹配的文档,observe()observeChanges()在匹配文档集更改时注册回调。

对于您的情况,示例实现看起来像这样:

var existingThreadsCursor = MessageThreads.find({ "participants": { "$all": participants} });
var count = 0;
existingThreadsCursor.forEach(function (thread){
    console.log("Thread with participants: " + JSON.stringify(thread.participants));
    count++;
});