在我工作的地方,我们使用 Ruby on Rails 创建后端和前端应用程序。通常,这些应用程序与同一个 MySQL 数据库交互。它适用于我们的大部分数据,但有一种情况我想迁移到 NoSQL 环境。
我们有客户,我们的客户也有我们所说的“库存”——其中一个或多个。一个库存可以有数千个项目。目前这是通过两个关系数据库表完成的,inventories
和 inventory_items
。
当两个不同的库存具有不同的参数时,问题就开始了:
# Inventory item from inventory 1, televisions
{
inventory_id: 1
sku: 12345
name: Samsung LCD 40 inches
model: 582903-4
brand: Samsung
screen_size: 40
type: LCD
price: 999.95
}
# Inventory item from inventory 2, accomodation
{
inventory_id: 2
sku: 48cab23fa
name: New York Hilton
accomodation_type: hotel
star_rating: 5
price_per_night: 395
}
由于我们显然不能使用 brand
或 star_rating
作为 inventory_items
中的列名,因此到目前为止我们的解决方案是使用泛型列名如text_a
、text_b
、float_a
、int_a
等,并引入第三张表inventory_schemas
。现在的表格如下所示:
# Inventory schema for inventory 1, televisions
{
inventory_id: 1
int_a: sku
text_a: name
text_b: model
text_c: brand
int_b: screen_size
text_d: type
float_a: price
}
# Inventory item from inventory 1, televisions
{
inventory_id: 1
int_a: 12345
text_a: Samsung LCD 40 inches
text_b: 582903-4
text_c: Samsung
int_a: 40
text_d: LCD
float_a: 999.95
}
这运作良好......在一定程度上。它笨重,不直观,缺乏可扩展性。我们必须投入资源来建立库存模式。不能使用单独的表格。
输入 NoSQL。有了它,我们可以让每个项目都有自己的参数,并且仍然将它们存储在一起。从我所做的研究来看,对于这种情况来说,这似乎是一个很好的替代方案。
具体来说,我看过 CouchDB 和 MongoDB。两者看起来都很棒。但是,我们还需要对库存进行一些其他处理:
- 我们需要能够从一个(或多个)库存中选择商品。
- 我们需要能够根据其参数过滤商品(例如,从库存 2 中获取类型为“酒店”的所有商品)。
- 我们需要能够根据参数对商品进行分组(例如,从库存 1 中品牌为“三星”的商品中获取最低价格)。
- 我们需要(可能)能够一次检索数千个项目。
- 我们需要能够访问来自多个应用程序的数据;后端(处理数据)和前端(显示数据)。
- 虽然不是必需的,但需要快速批量插入。
根据结构和需求,CouchDB 或 MongoDB 适合我们吗?如果是这样,哪一个最合适?
感谢阅读,提前感谢您的回答。
编辑:我喜欢 CouchDB 的原因之一是我们在前端应用程序中可以在页面加载后直接通过 JavaScript 从服务器请求数据,并显示结果而无需使用任何后端代码。这将导致更好的页面加载和更少的服务器压力,因为数据的获取/处理将在客户端完成。
最佳答案
我在 MongoDB 上工作,所以你应该对它持保留态度,但这看起来非常适合 Mongo。
- We need to be able to select items from only one (or several) inventories.
对任何字段进行临时查询都很容易。
- We need to be able to filter items based on its parameters (eg. get all items from inventory 2 where type is 'hotel').
对此的查询将是:{"inventory_id": 2, "type": "hotel"}
。
- We need to be able to group items based on parameters (eg. get the lowest price from items in inventory 1 where brand is 'Samsung').
再次, super 简单:db.items.find({"brand": "Samsung"}).sort({"price": 1})
- We need to (potentially) be able to retrieve thousands of items at a time.
没问题。
- Rapid bulk insertion is desired, though not required.
MongoDB 的批量插入比 CouchDB 快得多。
另外,还有一个用于 MongoDB 的 REST 接口(interface):http://github.com/kchodorow/sleepy.mongoose
您可能想阅读 http://chemeo.com/doc/technology ,他用 MongoDB 处理任意属性搜索问题。
关于mongodb - CouchDB 或 MongoDB 中的哪一个适合我的需求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3010794/