流星能否正确处理直接从外部更新到MongoDB数据库的数据? [英] Can Meteor correctly handle data updated externally directly to the MongoDB database?

查看:72
本文介绍了流星能否正确处理直接从外部更新到MongoDB数据库的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我已经有一个Nodejs服务器应用程序,该应用程序负责自动读写MongoDB数据库.该服务没有任何公共端点,因此与之通信的唯一方法是通过数据库本身.

Context: I already have a Nodejs server app that takes care of reading and writing to a MongoDB database autonomously. This service doesn't have any public endpoint, so the only way to communicate with it is through the database itself.

现在,我想构建一个独立的Web应用程序,该网站将使用前面提到的MongoDB数据库中的可用数据.这些数据中的大多数应实时显示给用户.经过一些框架研究,我正在考虑使用Meteor,但找不到有关它是否与现有服务很好地集成的有用信息.

Now I want to build an independent web application, a website that will consume the data available at the previously mentioned MongoDB database. Most of this data should be shown in real-time to the user. After some framework research, I'm considering to use Meteor, but I couldn't find any useful info on whether it integrates well with pre-existing services.

所以实际的问题是:我可以在直接和外部使用MongoDB数据库的同时,受益于Meteor的框架,特别是受益于所有实时客户端数据更新吗?

So the actual question is: Can I benefit from Meteor's framework, particularly from all the real-time client data updates, while directly and externally using the MongoDB database?

例如,如果我在网站上显示玩家"表,并且添加了新玩家或在外部MongoDB数据库中直接直接更新了玩家的得分 ,Meteor能否在所有已打开网站的客户中更新分数,还是此要求会使Meteor不适合我的项目?

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的框架中受益吗,尤其是从所有 实时客户端数据更新,而直接和外部使用 MongoDB数据库?

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配置为连接到外部mongo数据库.您已经将mongo打包在本地开发应用程序上,但这只是为了舒适和轻松集成.

Meteor is configured to connect to an external mongo database. You have mongo packaged on a local development app but that is just for comfort and easy integration.

在生产中,您将始终必须使用MONGO_URL 环境变量.

In production you will always have to connect Meteor to your running Mongo instance using the MONGO_URL environment variable.

奖金:即使在开发模式下,您也可以设置MONGO_URL以在开发模式下连接到特定的数据库.请注意,您可以在此数据库上添加所有内容,请谨慎使用.

Bonus: you can set the MONGO_URL even in dev mode to connect to a specific db in dev mode. Be aware that you can CRUD everything on this db, use with care.

引擎盖下,Meteor使用节点mongo驱动程序.您基本上可以使用此驱动程序的所有API(

Under the hood Meteor uses the node mongo driver. You are basically able to use all the API of this driver (see this post for details on how to call native Mongo methods)

例如,如果我要在网站上显示玩家"表,并在其中显示一个新的 玩家被添加或玩家的比分在外部被更新, 直接在基础MongoDB数据库中,Meteor将能够 更新所有已打开该网站的客户的分数,或将 这个要求使Meteor不适合我的项目吗?

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的发布/订阅系统,您基本上可以控制将哪些数据发布到客户.每次数据更改时都会运行发布(类似于观察者模式).

With Meteor's publication / subscription system you basically control what data is published to the client. Publications are running each time the data changes (similar to observer pattern).

如果所有客户都订阅了集合的数据,则一旦集合更新,他们将获得更新.现在是您最想要的功能:如果此数据是由某些外部源更新的,则也可以使用此功能.

If all your clients subscribe to the data of a collection they will get the updates, once the collection updates. And now coming to your most wanted feature: this also works if this data is updated by some external source.

它也以非常快的更新间隔工作.我参与了一个最近的项目,该项目通过Mongo-DB集合上的python更新了大量数据. Meteor应用程序收听了此消息,并以实时"(或用户认为是实时的)的方式向客户端显示了数据.

It also works with very rapid update intervals. I worked on a recent project where massive amounts of data have been updated via python on a Mongo-DB collection. The Meteor app listened to it and displayed the data to the clients in "realtime" (or what users perceive as real time).

但是,这里有一些陷阱,很高兴提前知道它们.

However, there are some pitfalls and it is good to know them in advance.

  1. Meteor创建的文档的字符串类型为_id而不是Mongo.ObjectID.但是,它能够读取和编写如果正确使用它.

  1. Meteor creates documents with an _id of type string and not Mongo.ObjectID. However it is capable of reading it and writing it if you use it correctly.

Meteor使用自己的API 包装集合,从而最好地整合了集合基于fibers的环境.如果您需要使用其他功能,请在此处此处.

Meteor wraps collections with an own API that integrates the collection best with its fibers based environment. If you need to use functionality beyond please read here and here.

返回的游标的行为略有不同,但与那里的收藏夹一样如果您从rawCollection

Returned cursors behave slightly different but as with Collections there are also all native functionalities available if you receive the cursor from a rawCollection

仔细检查您在集合上使用的数据类型.例如,坚持使用相同的日期类型(例如ISODate),以便在进行更新后不会出现意外错误.还有一个名为simpl-schemamongoose流星( npm包)这是在您的收藏夹上保持结构的好方法.

Double check the data types you use on your collections. For example stick with the same Date types (like ISODate) so there are no unintended errors afeter an update. There is also a Meteor counterpart to mongoose named simpl-schema (npm package) which is a good way to keep structure on your collections.

总而言之,如果您考虑了大多数Meteor指南和API文档,那么您应该处于良好的轨道,因为外部更新的集合的集成通常运行得很好,并且主要是为了了解pub/sub系统以使其运行

Summarized, if you consider the most of the Meteor guide and API docs you should be on a good track as the integration of externally updated collections usually runs very well and it is mostly about understanding the pub/sub system to make it run.


但是考虑到陷阱1,如果Meteor使用自定义ID, 它真的能够处理添加到集合中的新文档吗? 外部(例如新玩家),如果此新文档的ID 是从外部设置的?

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?

是的,但是您需要知道其他查询.如果(外部创建的)文档具有以下值:

Yes it does but you need to be aware of a different query. If the (externally created) doc has the following value:

{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c") }

然后,您必须将(Meteor)查询更改为

Then you have to change your (Meteor) query from

collection.findOne("4ecc05e55dd98a436ddcc47c") // returns undefined

collection.findOne({ "_id" : new Mongo.ObjectID("4ecc05e55dd98a436ddcc47c") }) // returns { _id: ObjectID { _str: '4ecc05e55dd98a436ddcc47c' } }

的确,我能够指导Meteor创建文档 自定义ID与我已经创建的ID一致 外部服务?还是我需要让我的外部应用使用与Meteor相似的ID(即字符串)?

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)?

相同的模式可用于创建文档.代替

Same pattern works for creating documents. Instead of

collection.insert({ foo:'bar' })

您传递一个新创建的ObjectID:

you pass a newly created ObjectID:

collection.insert({ foo:'bar', _id: new Mongo.ObjectID() })

谈论潜在的陷阱时,Meteor是否将数据存储在任何 不寻常的方式?我的意思是,它是否将任何集合添加到数据库或任何其他 特殊字段,用于插入或类似文档以跟踪 这些吗?

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?

或者我可以期望除了文档之外的完全普通和简单的文档 自定义_id字段,它只是一个字符串?

Or can I expect totally normal and simple documents apart from the custom _id field which is simply a string?

如果使用上述方法在Meteor中创建文档,则不必担心_id是字符串.

If you create documents in Meteor with the method above, you should not need to worry about _id being a String.

除了文档是应有的方式(请参见数据格式桥 ).但是,如果您发现这里没有提到的例外情况,请随时发表评论,因为这对其他用户也可能很重要.

Besides that documents are the way they should be (see the data format bridge). However, if there is an exception you found that has not been mentioned here, feel free to comment as this may be important to other users as well.

这篇关于流星能否正确处理直接从外部更新到MongoDB数据库的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆