草庐IT

mongodb - Meteor.Collection 和 Meteor.Collection.Cursor

coder 2023-05-05 原文

什么是

Meteor.Collection 

Meteor.Collection.Cursor

这两者是如何相互关联的?做到了:

new Meteor.Collection("name") 

用参数名创建一个MONGODB集合?

最佳答案

Did new Meteor.Collection("name") create a MONGODB collection with the parameter name?

不完全是。 Meteor.Collection 表示一个可能存在也可能不存在的 MongoDB 集合,但实际的 MongoDB 集合在您插入文档之前不会真正创建。

Meteor.Collection.Cursor 是一种响应式(Reactive)数据源,它表示存在于 MongoDB 集合中的文档的变化子集。此文档子集由您传递给 Meteor.Collection.find(selector, options) 方法的 selectoroptions 参数指定。这个 find() 方法返回游标对象。我认为 Meteor Docs很好地解释游标:

find returns a cursor. It does not immediately access the database or return documents. Cursors provide fetch to return all matching documents, map and forEach to iterate over all matching documents, and observe and observeChanges to register callbacks when the set of matching documents changes.

Collection cursors are not query snapshots. If the database changes between calling Collection.find and fetching the results of the cursor, or while fetching results from the cursor, those changes may or may not appear in the result set.

Cursors are a reactive data source. The first time you retrieve a cursor's documents with fetch, map, or forEach inside a reactive computation (eg, a template or autorun), Meteor will register a dependency on the underlying data. Any change to the collection that changes the documents in a cursor will trigger a recomputation. To disable this behavior, pass {reactive: false} as an option to find.

游标的 react 性很重要。如果我有一个游标对象,我可以通过调用 fetch() 来检索它所代表的当前文档集。如果数据在两次调用之间发生变化,fetch() 方法实际上会返回一个不同的文档数组。 Meteor 中的许多东西本身就理解游标的 react 性。这就是为什么我们可以从模板帮助函数返回一个游标对象:

Template.foo.documents = function() {
  return MyCollection.find(); // returns a cursor object, rather than an array of documents
};

在幕后,Meteor 的模板系统知道在这个游标对象上调用 fetch()。当服务器向客户端发送更新通知它集合已更改时,游标会收到此更改的通知,这会导致重新计算模板帮助程序,从而导致重新呈现模板。

关于mongodb - Meteor.Collection 和 Meteor.Collection.Cursor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21952898/

有关mongodb - Meteor.Collection 和 Meteor.Collection.Cursor的更多相关文章

  1. ruby-on-rails - rails 上的 ruby : radio buttons for collection select - 2

    我有一个集合选择:此方法的单选按钮是什么?谢谢 最佳答案 Rails3中没有这样的助手。在Rails4中,它是collection_radio_buttons. 关于ruby-on-rails-rails上的ruby:radiobuttonsforcollectionselect,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18525986/

  2. ruby-on-rails - rails : render a collection of models using an specific html view - 2

    我有以下关于rails的简单问题。假设我有一个模型用户。在View中,如果我这样做:views/user/_user.html.erb中的文件View将为每个用户调用和打印。如何更改它以使用特定View?我需要这样的东西:User.all:template=>"user/_user_2ndview.html"%>有什么帮助吗?提前致谢 最佳答案 您可以使用collection选项:User.all,:partial=>"users/user2ndview",:as=>:user%>View必须放在views/users/_user2

  3. ruby-on-rails - ruby rails : How to sort a collection_select - 2

    我想按数据库表列“plays”对其进行排序/排序(按我想要的方式降序或升序)我完全糊涂了。刚刚找到了select而不是collection_select的解决方案?我的一些代码不知道如何排序/排序数据库表中还有一些列,如“plays”、“goals”... 最佳答案 只需将实际排序的集合传递给collection_select助手:collection_select(:post,:author_id,Author.order('created_atDESC'),:id,:name_with_initial,:prompt=>true

  4. ruby-on-rails - 如何使用 grouped_collection_select 显示多选? - 2

    我正在使用以下代码来显示类别的TreeView选择框:grouped_collection_select(:categories,:category_id,Category.top_level,:children,:name,:id,:name,:include_blank=>true)如何更改它以允许多选?此外,是否可以让它显示复选框而不是选择框? 最佳答案 尝试grouped_collection_select(:categories,:category_id,Category.top_level,:children,:name

  5. ruby-on-rails - Rails 3.2 ActiveAdmin 'Collection is not a paginated scope.' 错误 - 2

    我正在使用Rails3.2和ActiveAdmin0.4.4开发应用程序。我有一个名为Teaser的模型(/app/models/teaser.rb):classTeasertruemount_uploader:img,TeaserUploaderend然后我向其中添加了ActiveAdmin(/app/admin/teaser.rb):#encoding:UTF-8ActiveAdmin.registerTeaserdoformdo|f|f.inputs"Teaser"dof.input:name,:label=>'Текст'f.input:url,:label=>'Ссылка'

  6. ruby-on-rails - Rails Impressionist Gem - WillPaginate::Collection 不令人印象深刻 - 2

    有什么方法可以将印象派gem与分页一起使用?我尝试将印象派用于will_paginate集合,如下所示:posts=Post.all.paginate(:page=>params[:page])impressionist(posts)但是它引发了这个错误:WillPaginate::Collectionisnotimpressionable!有什么方法可以直接在View上使用印象派方法? 最佳答案 will_paginate生成的集合不易受影响意味着您正在阅读,它的类没有调用is_impressionable,正如您的模型应该调用的

  7. ruby-on-rails - 在一个 Rails 应用程序中使用 PostgreSQL 的 MongoDB - 2

    我可以在一个Rails应用程序中同时使用MongoDB和PostgreSQL吗?具体来说,我最终会想要使用像MongoHQ这样的东西。到目前为止,我未能在实验中进行这项工作。令我担心的是,MongoDB文档特别指出我必须禁用ActiveRecord。任何建议将不胜感激。 最佳答案 您无需禁用ActiveRecord即可使用MongoDB。查看Mongoid只需将gem加上任何模型与您现有的任何ActiveRecord模型一起添加。您应该注意到MongoHQ只是MongoDB的托管服务,可以与任何对象文档映射器(ODM)一起使用。更多

  8. ruby - 在 collection_select rails 中选择多个选项 - 2

    我知道如何组合一个从模型中获取值的简单选择框"PleaseSelectaSector")%>我的问题是如何允许用户选择多个选项,然后将它们存储在模型中。我知道我需要使用:multiple=>true但不确定语法通常对于一个模型的多个条目,我会使用accepts_nested_attributes_for,但我认为我不需要对这个例子这样做是否正确?谢谢 最佳答案 经过一些尝试和错误后确定"PleaseSelectaSector"},{:multiple=>true})%>让我选择多个选项

  9. ruby - 使用 mongodb/mongoid 运行时更改模型 - 2

    我必须在mongoid模型中添加几个字段,我知道MongoDB没有迁移,但如果我继续而不删除数据库,使rails完全“重新生成”数据库,它不会显示或使用新的领域!去这里最好的方法是什么?有比删除/重新打开mongodb更软的东西吗?提前致谢卢卡 最佳答案 一般来说,应该可以在运行时用新字段更新旧文档。MongoDB中不需要迁移。您可能想编写rake任务以使用新字段和默认值更新旧文档。您可以通过检查那些默认值为nil的新字段来找到这些文档。更新简单风格:如果您使用默认值定义一个新字段,只要您设置了一个新值,就应该始终使用该值:应用程序

  10. ruby-on-rails - 我如何从 Ruby 代码连接到 mongodb? - 2

    我如何从Ruby代码连接到mongodb? 最佳答案 首先,您必须安装MongoDbgem:geminstallmongo然后运行代码:require'rubygems'#notnecessaryforRuby1.9require'mongo'db=Mongo::Connection.new.db("mydb")#ORdb=Mongo::Connection.new("localhost").db("mydb")#ORdb=Mongo::Connection.new("localhost",27017).db("mydb")

随机推荐