简体   繁体   English

ArangoDb从所有收藏中获取最新文档

[英]ArangoDb get latest document from all collections

I'm beginning to think I've modeled my data a bit incorrectly, since I'm having trouble querying it. 我开始认为我已经对我的数据建模有点不正确,因为我在查询时遇到了麻烦。

Currently what I have is a bunch of Customers (modeled as a Database per customer) These Customers have a bunch of Devices: Device1...n (modeled as a collection per device) These devices generate messages (modeled as documents within the device collection). 目前我拥有的是一堆客户(每个客户建模为数据库)这些客户有一堆设备:Device1 ... n(建模为每个设备的集合)这些设备生成消息(在设备集合中建模为文档) )。

In order to give good feedback to customers, I now want to support retrieving a customers latest messages (one message per device). 为了给客户提供良好的反馈,我现在希望支持检索客户的最新消息(每个设备一条消息)。

I'm having trouble to find documentation describing how to query over multiple collections, as there can be 1000s of devices for a customer, I'd rather not do 1000s of queries. 我很难找到描述如何查询多个集合的文档,因为客户可以有1000个设备,我宁愿不做1000次查询。

Thanks! 谢谢!

If there can be 1000s of devices per customer, and device messages are stored in device-specific collections, searching for the latest message for a customer would require you to find the latest record in a variable number of collections, which will not only hard to express in a query but also inefficient. 如果每个客户可以有1000个设备,并且设备消息存储在特定于设备的集合中,则为客户搜索最新消息将要求您在可变数量的集合中查找最新记录,这不仅难以在查询中表达但效率低下。

Is it possible to put the messages of all devices for a given customer into a single customer-specific collection, and store the device id as an attribute in each document? 是否可以将给定客户的所有设备的消息放入单个特定于客户的集合中,并将设备ID作为属性存储在每个文档中?

For example: 例如:

// create customer-specific collections
db._create("messages_customer1");
db._create("messages_customer2");

// create an index on `dt` attribute in each collection 
// so messages can be queried efficiently sorted by date
db.messages_customer1.ensureIndex({ type: "skiplist", fields: [ "dt" ]});
db.messages_customer2.ensureIndex({ type: "skiplist", fields: [ "dt" ]});

// insert some messages for customer 1
db.messages_customer1.insert({ device: 123, dt: Date.now(), message: "foo" });
db.messages_customer1.insert({ device: 123, dt: Date.now(), message: "bar" });
db.messages_customer1.insert({ device: 456, dt: Date.now(), message: "baz" });

// insert some messages for customer 2
db.messages_customer2.insert({ device: 999, dt: Date.now(), message: "qux" });
db.messages_customer2.insert({ device: 888, dt: Date.now(), message: "wut" });

Now it will be relatively easy to find the latest message for a given customer: 现在,找到给定客户的最新消息会相对容易:

  • determine customer id via request and or business logic 通过请求和/或业务逻辑确定客户ID
  • with customer id (eg id 1 ), query customer-specific collection 使用客户ID(例如id 1 ),查询客户特定的集合

For example: 例如:

var query = "FOR m IN @@messages SORT m.dt DESC LIMIT 1 RETURN m";
var id = 1;
var params = { "@messages": "messages_customer" + id }
latestMessage = db._query(query, params).toArray()[0];

If the messages are all that's customer-specific, then there's also no need to create separate databases per customer, as all customer-specific collections could go into the same database. 如果消息都是客户特定的消息,那么也不需要为每个客户创建单独的数据库,因为所有客户特定的集合都可以进入同一个数据库。 You should of course care about access control to the data, either via application business logic or Foxx . 您当然应该关心数据的访问控制,通过应用程序业务逻辑或Foxx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM