mongodb - Meteor.Collection 和 Meteor.Collection.Cu

什么是

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/

相关文章:

node.js - 是否可以将额外的参数传递给 Mongoose 更新回调

mongodb - 在 mongo shell 中加载和运行聚合

mongodb - 使用聚合框架按子文档字段分组

mongodb - NoSQL 与关系数据库与可能的混合

javascript - 在 MongoDB 更新语句中使用变量

python - 有人知道 pymongo 中 2dsphere 索引的工作示例吗?

java - MongoDB Java 驱动程序 : distinct with sort

python - Mongodb复制集自动重新连接在nginx + uwsgi上下运行后无法正常工作

node.js - NodeJS ExpressJS PassportJS - 仅用于管理页面

c# - 使用 MongoDB 的 C# 驱动程序进行多字段查询