Meteor 能否正确处理外部直接更新到 MongoDB 数据库的数据?
Can Meteor correctly handle data updated externally directly to the MongoDB database?
上下文: 我已经有一个 Nodejs 服务器应用程序,它负责自主读取和写入 MongoDB 数据库。此服务没有任何 public 端点,因此与其通信的唯一方法是通过数据库本身。
现在我想构建一个独立的 Web 应用程序,一个将使用前面提到的 MongoDB 数据库中可用数据的网站。大多数此类数据应实时显示给用户。经过一些框架研究,我正在考虑使用 Meteor,但我找不到任何有用的信息来说明它是否与现有服务很好地集成。
所以实际的问题是: 我能否从 Meteor 的框架中受益,特别是从所有实时客户端数据更新中受益,同时直接和外部使用 MongoDB数据库?
例如,如果我在我的站点中显示 "players" 的 table,并且添加了新玩家或在外部 更新了玩家的分数,并且直接在底层 MongoDB 数据库 中,Meteor 是否能够更新所有打开站点的客户端的分数,或者此要求是否会使 Meteor 不适合我的项目table?
Can I benefit from Meteor's framework, particularly from all the
real-time client data updates, while directly and externally using the
MongoDB database?
Meteor 配置为 connect to an external mongo database。您已将 mongo 打包到本地开发应用程序中,但这只是为了方便和轻松集成。
在生产中,您将始终必须使用 MONGO_URL
environment variable.
将 Meteor 连接到您的 运行ning Mongo 实例
好处:即使在开发模式下,您也可以设置 MONGO_URL
以在开发模式下连接到特定的数据库。请注意,您可以在该数据库上增删改查所有内容,请谨慎使用。
在引擎盖下,Meteor 使用节点 mongo 驱动程序。您基本上可以使用此驱动程序的所有 API(see this post 了解如何调用本机 Mongo 方法的详细信息)
For example, if I'm showing a table of "players" in my site, and a new
player is added or the score of a player is updated externally and
directly in the underlying MongoDB database, will Meteor be able to
update the score in all the clients that have the site open, or will
this requirement make Meteor unsuitable for my project?
使用 Meteor 的 publication / subscription system 您基本上可以控制向客户端发布的数据。每次数据更改时发布 运行ning(类似于观察者模式)。
如果您的所有客户都订阅了一个集合的数据,他们将在集合更新后获得更新。现在来到您最想要的功能:如果此数据由某些外部源更新,这也适用。
它也适用于非常快的更新间隔。我在最近的一个项目中工作,其中大量数据已通过 python 在 Mongo-DB 集合上更新。 Meteor 应用程序听取它并在 "realtime" 中向客户端显示数据(或者用户认为是实时的)。
不过,也有一些陷阱,提前了解一下是有好处的。
Meteor 使用字符串类型 _id
而不是 Mongo.ObjectID
创建文档。但是它能够读取和写入 if you use it correctly.
Meteor 用 own API that integrates the collection best with its fibers
based environment. If you need to use functionality beyond please read here and here.
包装集合
Returned cursors 行为略有不同,但与 Collections 一样,如果您从 rawCollection
仔细检查您在集合中使用的数据类型。例如,坚持使用相同的日期类型(如 ISODate),这样更新后就不会出现意外错误。还有一个名为 simpl-schema
(npm package) 的 mongoose
对应的 Meteor,这是保持集合结构的好方法。
总结一下,如果你考虑了大部分的 Meteor 指南和 API 文档,你应该走在一个好的轨道上,因为外部更新集合的集成通常 运行s 非常好,而且大部分都是关于理解 pub/sub 系统使其成为 运行.
编辑:
However and considering pitfall number 1, if Meteor uses custom ids,
will it really be able to handle new documents added to a collection
externally (for example a new player), if the id of this new document
is set externally?
是的,但您需要注意不同的查询。如果(外部创建的)文档具有以下值:
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c") }
然后你必须改变你的 (Meteor) 查询
collection.findOne("4ecc05e55dd98a436ddcc47c") // returns undefined
到
collection.findOne({ "_id" : new Mongo.ObjectID("4ecc05e55dd98a436ddcc47c") }) // returns { _id: ObjectID { _str: '4ecc05e55dd98a436ddcc47c' } }
And indeed, will I be able to instruct Meteor to create the documents
with custom IDs that are consistent with the IDs already created by my
external service? Or will I need to make my external app use IDs similar to Meteor's (i.e. strings)?
相同的模式适用于创建文档。而不是
collection.insert({ foo:'bar' })
您传递了一个新创建的 ObjectID:
collection.insert({ foo:'bar', _id: new Mongo.ObjectID() })
And talking about potential pitfalls, does Meteor store data in any
unusual way? I mean, does it add any collection to the DB or any
special field to documents that are inserted or alike to keep track of
these?
Or can I expect totally normal and simple documents apart from the
custom _id field which is simply a string?
如果您使用上述方法在 Meteor 中创建文档,则无需担心 _id
是一个字符串。
除此之外,文档应该是这样的(请参阅 data format bridge)。但是,如果您发现此处未提及的异常,请随时发表评论,因为这可能对其他用户也很重要。
上下文: 我已经有一个 Nodejs 服务器应用程序,它负责自主读取和写入 MongoDB 数据库。此服务没有任何 public 端点,因此与其通信的唯一方法是通过数据库本身。
现在我想构建一个独立的 Web 应用程序,一个将使用前面提到的 MongoDB 数据库中可用数据的网站。大多数此类数据应实时显示给用户。经过一些框架研究,我正在考虑使用 Meteor,但我找不到任何有用的信息来说明它是否与现有服务很好地集成。
所以实际的问题是: 我能否从 Meteor 的框架中受益,特别是从所有实时客户端数据更新中受益,同时直接和外部使用 MongoDB数据库?
例如,如果我在我的站点中显示 "players" 的 table,并且添加了新玩家或在外部 更新了玩家的分数,并且直接在底层 MongoDB 数据库 中,Meteor 是否能够更新所有打开站点的客户端的分数,或者此要求是否会使 Meteor 不适合我的项目table?
Can I benefit from Meteor's framework, particularly from all the real-time client data updates, while directly and externally using the MongoDB database?
Meteor 配置为 connect to an external mongo database。您已将 mongo 打包到本地开发应用程序中,但这只是为了方便和轻松集成。
在生产中,您将始终必须使用 MONGO_URL
environment variable.
好处:即使在开发模式下,您也可以设置 MONGO_URL
以在开发模式下连接到特定的数据库。请注意,您可以在该数据库上增删改查所有内容,请谨慎使用。
在引擎盖下,Meteor 使用节点 mongo 驱动程序。您基本上可以使用此驱动程序的所有 API(see this post 了解如何调用本机 Mongo 方法的详细信息)
For example, if I'm showing a table of "players" in my site, and a new player is added or the score of a player is updated externally and directly in the underlying MongoDB database, will Meteor be able to update the score in all the clients that have the site open, or will this requirement make Meteor unsuitable for my project?
使用 Meteor 的 publication / subscription system 您基本上可以控制向客户端发布的数据。每次数据更改时发布 运行ning(类似于观察者模式)。
如果您的所有客户都订阅了一个集合的数据,他们将在集合更新后获得更新。现在来到您最想要的功能:如果此数据由某些外部源更新,这也适用。
它也适用于非常快的更新间隔。我在最近的一个项目中工作,其中大量数据已通过 python 在 Mongo-DB 集合上更新。 Meteor 应用程序听取它并在 "realtime" 中向客户端显示数据(或者用户认为是实时的)。
不过,也有一些陷阱,提前了解一下是有好处的。
Meteor 使用字符串类型
_id
而不是Mongo.ObjectID
创建文档。但是它能够读取和写入 if you use it correctly.Meteor 用 own API that integrates the collection best with its
fibers
based environment. If you need to use functionality beyond please read here and here. 包装集合
Returned cursors 行为略有不同,但与 Collections 一样,如果您从
rawCollection
仔细检查您在集合中使用的数据类型。例如,坚持使用相同的日期类型(如 ISODate),这样更新后就不会出现意外错误。还有一个名为
simpl-schema
(npm package) 的mongoose
对应的 Meteor,这是保持集合结构的好方法。
总结一下,如果你考虑了大部分的 Meteor 指南和 API 文档,你应该走在一个好的轨道上,因为外部更新集合的集成通常 运行s 非常好,而且大部分都是关于理解 pub/sub 系统使其成为 运行.
编辑:
However and considering pitfall number 1, if Meteor uses custom ids, will it really be able to handle new documents added to a collection externally (for example a new player), if the id of this new document is set externally?
是的,但您需要注意不同的查询。如果(外部创建的)文档具有以下值:
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c") }
然后你必须改变你的 (Meteor) 查询
collection.findOne("4ecc05e55dd98a436ddcc47c") // returns undefined
到
collection.findOne({ "_id" : new Mongo.ObjectID("4ecc05e55dd98a436ddcc47c") }) // returns { _id: ObjectID { _str: '4ecc05e55dd98a436ddcc47c' } }
And indeed, will I be able to instruct Meteor to create the documents with custom IDs that are consistent with the IDs already created by my external service? Or will I need to make my external app use IDs similar to Meteor's (i.e. strings)?
相同的模式适用于创建文档。而不是
collection.insert({ foo:'bar' })
您传递了一个新创建的 ObjectID:
collection.insert({ foo:'bar', _id: new Mongo.ObjectID() })
And talking about potential pitfalls, does Meteor store data in any unusual way? I mean, does it add any collection to the DB or any special field to documents that are inserted or alike to keep track of these?
Or can I expect totally normal and simple documents apart from the custom _id field which is simply a string?
如果您使用上述方法在 Meteor 中创建文档,则无需担心 _id
是一个字符串。
除此之外,文档应该是这样的(请参阅 data format bridge)。但是,如果您发现此处未提及的异常,请随时发表评论,因为这可能对其他用户也很重要。