Representing many-to-many relationships in couchDB

Say I'm writing a log analysis application. The primary domain object will be LogEntry. Also. application users define LogTopic, which describes which log entries they are interested in. Since the application receives journal entries, it adds them to couchDB, and also checks them on all LogTopics in the system to see if they meet the criteria in the topic. If this happens, the system should record that the record matches the topic. Thus, there is a many-to-many relationship between LogEntries and LogTopics.

If I stored this in a DBMS, I would do something like:

CREATE TABLE Entry (
 id int,
 ...
)

CREATE TABLE Topic (
 id int,
 ...
)

CREATE TABLE TopicEntryMap (
 entry_id int,
 topic_id int
)

Using CouchDB, at first I tried to have only two types of document. I will have a LogEntry type that looks something like this:

{
  'type': 'LogEntry',
  'severity': 'DEBUG',
  ...
}

LogTopic, :

{
  'type': 'LogTopic',
  'matching_entries': ['log_entry_1','log_entry_12','log_entry_34',....],
  ...
}

, , matching_entries LogTopic LogEntry. , , . , . , , , RDBMS , :

{
  'type':'LogTopicToLogEntryMap',
  'topic_id':'topic_12',
  'entry_id':'entry_15'
}

, :

  • , , . , couchDB- (?) .
  • . ( include_docs).

- ? , , ?

+3
3

. CouchDB , . , , "". SQL- , SQL .

CouchDB , :

  • , .
  • CRUD , .
  • SQL-
  • SQL-, CouchDB

, couchdb-lucene .

+4

couchdb Nathan Stott

+11

, LogEntrys , LogTopics . , LogEntry , LogTopics .

Then a simple mapping function will generate a LogEntry once for each LogTopic to which it belongs, basically creating your ThemeEntryMap on the fly:

"map": function (doc) {
    doc.topics.map(function (topic) {
        emit(topic, doc);
    });
}

Thus, a view request with an argument ?key=<topic>will give you all the entries related to the topic.

0
source

Source: https://habr.com/ru/post/1724332/


All Articles