Meteor.js:使用Collection来存储该集合的状态

时间:2022-08-24 17:02:25

Happy Friday!

I have a somewhat newbie question for the community here and couldn't find a previous answer. I don't necessarily have enough code to create a package yet, but I wanted to store some helper methods on my collection that can be re-used everywhere and also re-use the "Words" collection namespace. I'm storing the client side in my lib folder. When I do this, am I doing anything weird to Mongo DB? From what I understand, Words initializes the Mongo collection but the actual variable is just a regular JS variable that you can add methods and properties to - the Mongo side of things can only be accessed via your .find(), .insert(), etc. methods. Is this accurate? And also generally, in general is this a good practice?

我在这里对社区有一些新手问题,但是找不到以前的答案。我没有足够的代码来创建一个包,但是我想在我的集合中存储一些可以在任何地方重用的辅助方法,并且还可以重用“Words”集合命名空间。我将客户端存储在我的lib文件夹中。当我这样做时,我对Mongo DB做了什么奇怪的事吗?根据我的理解,Words初始化Mongo集合,但实际变量只是一个常规JS变量,你可以添加方法和属性 - 事物的Mongo方面只能通过.find(),。insert()访问,等方法。这准确吗?而且一般来说,这是一个很好的做法吗?

Words = new Mongo.Collection ('words');
Words.state = {};

Words.state.shuffle = function ( ) {
    return _.shuffle( Words.find().fetch() );
};
//find id of current set

Words.state.indexOf = function ( id ) {
    var i;
    var arr = Words.state.currentSet;
    for ( i = 0; i < arr.length ; i++ ) {
        if (arr[i]._id === id ) return i;
    }
}; 

2 个解决方案

#1


When I do this, am I doing anything weird to Mongo DB?

当我这样做时,我对Mongo DB做了什么奇怪的事吗?

No. Meteor communicates with mongo via a low-level mongodb driver, which is called only from the functions inherent to all collections: find, insert, remove, update, etc. Therefore, you are free to modify the collection instance without fear as long as you don't replace any of the aforementioned functions.

Meteor通过低级mongodb驱动程序与mongo进行通信,该驱动程序仅从所有集合固有的功能中调用:查找,插入,删除,更新等。因此,您可以*地修改集合实例,而不用担心因为您不替换任何上述功能。

In general is this a good practice?

一般来说这是一个好习惯吗?

This is a matter of opinion, but I think so. I like to add functions to both collection instances and to transformed documents. For example:

这是一个意见问题,但我想是的。我喜欢为集合实例和转换后的文档添加函数。例如:

Posts = new Mongo.Collection('posts');

Posts.fetchByAuthor = function(userId) {
  if (userId == null)
    userId = Meteor.userId();

  return Posts.find({authorId: userId}).fetch();
};

Now anywhere in your app, you can call Posts.fetchByAuthor() to get a list of posts by an author without needing to know how the collection data is stored under the hood.

现在,您可以在应用程序的任何位置调用Posts.fetchByAuthor()来获取作者的帖子列表,而无需知道收集数据是如何存储在引擎盖下的。

In cases where you need extra functions on a document instance (rather than on the collection as a whole), I'd recommend using collection helpers. For example:

如果您需要文档实例上的额外功能(而不是整个集合),我建议使用集合助手。例如:

Posts.helpers({
  author: function() {
    return Meteor.users.findOne(this.authorId);
  }
});

Now we can call post.author() anywhere in our app to get the author for a particular post. Again, we can invoke the function without needing to know the details of how an author is stored in the document.

现在我们可以在我们的应用程序中的任何地方调用post.author()来获取特定帖子的作者。同样,我们可以调用该函数,而无需知道作者如何存储在文档中的细节。

Although these examples are simple, the technique of centralizing your model-layer logic is extremely powerful because it helps separate the concerns of your app. On larger projects, we have found this greatly simplifies our work, particularly when writing template code.

虽然这些示例很简单,但集中模型层逻辑的技术非常强大,因为它有助于分离应用程序的问题。在较大的项目中,我们发现这极大地简化了我们的工作,特别是在编写模板代码时。

Recommended reading: How to add a model layer to your app

推荐阅读:如何向您的应用添加模型图层

#2


Meteor community packages are best resource to learn how to extend Mongo Collections. One of my favorites is collection-hooks but there are other like aldeed:collection2 that force your insert and update operations to be compliance with a schema provided by the package aldeed:simple-schema.

Meteor社区包是学习如何扩展Mongo Collections的最佳资源。我最喜欢的一个是collection-hooks,但还有其他像aldeed:collection2,它强制你的插入和更新操作符合包aldeed提供的模式:simple-schema。

You should also google for Extending Objects in Javascript and you'll understand the package's code. Check jQuery and/or Underscore.js methods, you'll find relevant methods that make you life easier on this matter.

您还应该谷歌扩展Javascript中的对象,你会理解包的代码。检查jQuery和/或Underscore.js方法,你会找到相关的方法,让你在这件事上更轻松。

#1


When I do this, am I doing anything weird to Mongo DB?

当我这样做时,我对Mongo DB做了什么奇怪的事吗?

No. Meteor communicates with mongo via a low-level mongodb driver, which is called only from the functions inherent to all collections: find, insert, remove, update, etc. Therefore, you are free to modify the collection instance without fear as long as you don't replace any of the aforementioned functions.

Meteor通过低级mongodb驱动程序与mongo进行通信,该驱动程序仅从所有集合固有的功能中调用:查找,插入,删除,更新等。因此,您可以*地修改集合实例,而不用担心因为您不替换任何上述功能。

In general is this a good practice?

一般来说这是一个好习惯吗?

This is a matter of opinion, but I think so. I like to add functions to both collection instances and to transformed documents. For example:

这是一个意见问题,但我想是的。我喜欢为集合实例和转换后的文档添加函数。例如:

Posts = new Mongo.Collection('posts');

Posts.fetchByAuthor = function(userId) {
  if (userId == null)
    userId = Meteor.userId();

  return Posts.find({authorId: userId}).fetch();
};

Now anywhere in your app, you can call Posts.fetchByAuthor() to get a list of posts by an author without needing to know how the collection data is stored under the hood.

现在,您可以在应用程序的任何位置调用Posts.fetchByAuthor()来获取作者的帖子列表,而无需知道收集数据是如何存储在引擎盖下的。

In cases where you need extra functions on a document instance (rather than on the collection as a whole), I'd recommend using collection helpers. For example:

如果您需要文档实例上的额外功能(而不是整个集合),我建议使用集合助手。例如:

Posts.helpers({
  author: function() {
    return Meteor.users.findOne(this.authorId);
  }
});

Now we can call post.author() anywhere in our app to get the author for a particular post. Again, we can invoke the function without needing to know the details of how an author is stored in the document.

现在我们可以在我们的应用程序中的任何地方调用post.author()来获取特定帖子的作者。同样,我们可以调用该函数,而无需知道作者如何存储在文档中的细节。

Although these examples are simple, the technique of centralizing your model-layer logic is extremely powerful because it helps separate the concerns of your app. On larger projects, we have found this greatly simplifies our work, particularly when writing template code.

虽然这些示例很简单,但集中模型层逻辑的技术非常强大,因为它有助于分离应用程序的问题。在较大的项目中,我们发现这极大地简化了我们的工作,特别是在编写模板代码时。

Recommended reading: How to add a model layer to your app

推荐阅读:如何向您的应用添加模型图层

#2


Meteor community packages are best resource to learn how to extend Mongo Collections. One of my favorites is collection-hooks but there are other like aldeed:collection2 that force your insert and update operations to be compliance with a schema provided by the package aldeed:simple-schema.

Meteor社区包是学习如何扩展Mongo Collections的最佳资源。我最喜欢的一个是collection-hooks,但还有其他像aldeed:collection2,它强制你的插入和更新操作符合包aldeed提供的模式:simple-schema。

You should also google for Extending Objects in Javascript and you'll understand the package's code. Check jQuery and/or Underscore.js methods, you'll find relevant methods that make you life easier on this matter.

您还应该谷歌扩展Javascript中的对象,你会理解包的代码。检查jQuery和/或Underscore.js方法,你会找到相关的方法,让你在这件事上更轻松。