在服务器,db或json上存储简单数据?

时间:2021-11-27 16:56:22

I have an app and it needs to store a few simple objects that change, and it needs to be able to persist them. Should I save the data to a db like mongo and read it in on startup or should I just save to a json file?

我有一个应用程序,它需要存储一些变化的简单对象,它需要能够持久化它们。我应该将数据保存到像mongo这样的数据库并在启动时读取它还是应该保存到json文件?

Thanks for any tips.

谢谢你的任何提示。

2 个解决方案

#1


7  

Either way can work. 'It depends' which is best in your situation.

无论哪种方式都可以。 '这取决于'哪种情况最适合你的情况。

Things to consider:

需要考虑的事项:

  • Flat files are not (or not easily) queriable.
  • 平面文件不是(或不容易)可查询的。

  • You will have to manage flat files (e.g. folders, file names, etc)
  • 您必须管理平面文件(例如文件夹,文件名等)

  • Is it possible that there will be a collision? Are you going to read & write often?
  • 是否可能发生碰撞?你经常读写吗?

  • If you are only persisting a few objects, is a database like mongo overkill?
  • 如果你只是持久化一些对象,那么像mongo这样的数据库是否过度杀伤?

  • Is something like redis a better solution?
  • 像redis这样的东西是更好的解决方案吗?

I'm currently working on a project that involves several data stores. MySQL, flat-files, mongo, et cetera.

我目前正在开发一个涉及多个数据存储的项目。 MySQL,平面文件,mongo等等。

The flat files work really well for storing data that will be pulled up later. It's suitable for data that isn't read or written often.

平面文件非常适合存储稍后将被提取的数据。它适用于不经常读取或写入的数据。

Mongo has modules like Mongoose that take away a lot of complexities. But the documentation for both Mongoose and Mongo can be a bit dry and confusing.

Mongo拥有像Mongoose这样的模块,它们带来了许多复杂性。但Mongoose和Mongo的文档可能有点干燥和令人困惑。

Think about how the data will be used, how it will evolve, and if you need to query it. If it just needs to be pulled up at the app's startup, then a flat file can work. If it is going to grow into larger objects then flat files can still be the solution. But if there's a chance for additional objects in the future, then flat-files will become harder to manage, and it will be a pain to add them into a working system.

考虑如何使用数据,如何进化以及是否需要查询数据。如果它只是需要在应用程序的启动时拉起来,那么平面文件可以工作。如果它将成长为更大的对象,那么平面文件仍然可以作为解决方案。但是如果将来有机会获得其他对象,那么平面文件将变得难以管理,将它们添加到工作系统中会很麻烦。

And overall, if you want any type of querying (other than manually opening files and inspecting them) then go with Mongoose or a similar database.

总的来说,如果你想要任何类型的查询(除了手动打开文件并检查它们),那么请使用Mongoose或类似的数据库。

#2


0  

It depends mongo will be easier if u want to alter the data. It is basically a storage engine for json lol. Depends on your use case. If you think this will grow, you might as well use mongo. If your storage needs will never change use a json file.

如果你想改变数据,这取决于mongo会更容易。它基本上是json lol的存储引擎。取决于您的用例。如果你认为这会增长,你也可以使用mongo。如果您的存储需求永远不会改变,请使用json文件。

#1


7  

Either way can work. 'It depends' which is best in your situation.

无论哪种方式都可以。 '这取决于'哪种情况最适合你的情况。

Things to consider:

需要考虑的事项:

  • Flat files are not (or not easily) queriable.
  • 平面文件不是(或不容易)可查询的。

  • You will have to manage flat files (e.g. folders, file names, etc)
  • 您必须管理平面文件(例如文件夹,文件名等)

  • Is it possible that there will be a collision? Are you going to read & write often?
  • 是否可能发生碰撞?你经常读写吗?

  • If you are only persisting a few objects, is a database like mongo overkill?
  • 如果你只是持久化一些对象,那么像mongo这样的数据库是否过度杀伤?

  • Is something like redis a better solution?
  • 像redis这样的东西是更好的解决方案吗?

I'm currently working on a project that involves several data stores. MySQL, flat-files, mongo, et cetera.

我目前正在开发一个涉及多个数据存储的项目。 MySQL,平面文件,mongo等等。

The flat files work really well for storing data that will be pulled up later. It's suitable for data that isn't read or written often.

平面文件非常适合存储稍后将被提取的数据。它适用于不经常读取或写入的数据。

Mongo has modules like Mongoose that take away a lot of complexities. But the documentation for both Mongoose and Mongo can be a bit dry and confusing.

Mongo拥有像Mongoose这样的模块,它们带来了许多复杂性。但Mongoose和Mongo的文档可能有点干燥和令人困惑。

Think about how the data will be used, how it will evolve, and if you need to query it. If it just needs to be pulled up at the app's startup, then a flat file can work. If it is going to grow into larger objects then flat files can still be the solution. But if there's a chance for additional objects in the future, then flat-files will become harder to manage, and it will be a pain to add them into a working system.

考虑如何使用数据,如何进化以及是否需要查询数据。如果它只是需要在应用程序的启动时拉起来,那么平面文件可以工作。如果它将成长为更大的对象,那么平面文件仍然可以作为解决方案。但是如果将来有机会获得其他对象,那么平面文件将变得难以管理,将它们添加到工作系统中会很麻烦。

And overall, if you want any type of querying (other than manually opening files and inspecting them) then go with Mongoose or a similar database.

总的来说,如果你想要任何类型的查询(除了手动打开文件并检查它们),那么请使用Mongoose或类似的数据库。

#2


0  

It depends mongo will be easier if u want to alter the data. It is basically a storage engine for json lol. Depends on your use case. If you think this will grow, you might as well use mongo. If your storage needs will never change use a json file.

如果你想改变数据,这取决于mongo会更容易。它基本上是json lol的存储引擎。取决于您的用例。如果你认为这会增长,你也可以使用mongo。如果您的存储需求永远不会改变,请使用json文件。