mongodb - Meteor.Collection 和 Meteor.Collection.Cursor

标签 mongodb meteor

什么是

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/

相关文章:

meteor - 别人可以覆盖我的myapp.meteor.com部署吗?

2台服务器上的mongoDB复制+分片合理吗?

node.js - 如何从对象数组中拉出一定范围的对象并将它们重新插入到数组中的新位置?

node.js - 如何让heroku使用DB的环境配置

javascript - Meteor.js Summernote 编辑器。从数据库 MongoDB 读取数据

javascript - 加载订单 : Meteor. js 和 Coffeescript

mongodb - 计算符合条件的数组的大小

MongoDB 需要很长时间才能建立索引

javascript - 如何在非 Meteor 项目中使用 Meteor 包

javascript - 显示 Meteor.user()