MongoDB:$ graphLookup以相反的顺序

时间:2017-05-04 13:57:09

标签: mongodb

我有以下数据集:

<?xml version="1.0" encoding="UTF-8"?>
<companies type="array">
<company>
<author-id type="integer">1151899</author-id>
<background></background>
<created-at type="datetime">2015-08-11T13:49:21Z</created-at>
<group-id type="integer" nil="true"></group-id>
<id type="integer">239684969</id>
<owner-id type="integer" nil="true"></owner-id>
<updated-at type="datetime">2017-05-03T17:46:46Z</updated-at>
<visible-to>Everyone</visible-to>
<name>Test Company</name>
<avatar_url></avatar_url>
<contact-data>
<twitter-accounts type="array"/>
<email-addresses type="array">
<email-address>
<address>info@test.com</address>
<id type="integer">126789186</id>
<location>Work</location>
</email-address>
</email-addresses>
<phone-numbers type="array">
<phone-number>
<id type="integer">192421740</id>
<location>Work</location>
<number>444 333 6490</number>
</phone-number>
</phone-numbers>
<web-addresses type="array">
<web-address>
<id type="integer">192421741</id>
<location>Work</location>
<url>http://www.123process.com/</url>
</web-address>
</web-addresses>
<addresses type="array">
<address>
<city>New York</city>
<country>United States</country>
<id type="integer">114818436</id>
<location>Work</location>
<state>NY</state>
<street>444 Winter Way</street>
<zip>10007</zip>
</address>
</addresses>
<instant-messengers type="array"/>
</contact-data>
<subject_datas type="array">
<subject_data>
<id type="integer">162387754</id>
<subject_field_id type="integer">1009158</subject_field_id>
<subject_field_label>Agency Extension</subject_field_label>
<value>ABCDF</value>
</subject_data>
<subject_data>
<id type="integer">162387755</id>
<subject_field_id type="integer">1009161</subject_field_id>
<subject_field_label>Agency ID</subject_field_label>
<value>68</value>
</subject_data>
<subject_data>
<id type="integer">162387756</id>
<subject_field_id type="integer">1009162</subject_field_id>
<subject_field_label>Edition</subject_field_label>
<value>Pro</value>
</subject_data>
<subject_data>
<id type="integer">162387757</id>
<subject_field_id type="integer">1009157</subject_field_id>
<subject_field_label>License Count</subject_field_label>
<value>1</value>
</subject_data>
<subject_data>
<id type="integer">215707824</id>
<subject_field_id type="integer">1161358</subject_field_id>
<subject_field_label>Exchange ID</subject_field_label>
<value>FBB52CA3-DC19-4992-AB2C-7235FABD9129</value>
</subject_data>
</subject_datas>
</company>
</companies>

我使用&#34; $ graphLookup&#34;看到所有与彼此有关系的客户。

{
  _id: ObjectId("asdasdasd..."),
  dependencies: {
    customers: [
      ObjectId("1..."),
      ObjectId("2...")
    ]
  }
}

这一切都很好。但现在我想以相反的顺序查找图表。依赖关系树中的最后一个客户与其他客户没有其他依赖关系。对于cource,否则我不会是该树中的最后一个客户。

以某种方式可以看到所有客户都对树中的最后一位客户有依赖吗?

e.g。 普通的GraphLookup: C1 =&gt; C2 =&gt; C3 =&gt; ...

反向GraphLookup: C3 =&gt; C2 =&gt; C1

也许我需要改变架构...但我不知道如何。

另一种选择是存储2种不同类型的依赖项:父项,子项。但这使得有必要对所有更改进行两次更新:对于在父级中具有依赖关系X的客户以及在子级中具有依赖关系X的客户。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的数据需要指出这种关系的方向(例如,父母=>孩子或孩子=>父母)。我看到的方式是通过将数据存储在表示关系的数组中。对于您来说,就像这样:

((Get-ADUser -filter {employeetype -eq "Employee"}).SamAccountName) | Sort-Object | Get-ADUser | ForEach-Object {$_.Name;$_.Department;$_mail}

这样,您就可以沿每个方向遍历数据

{
  _id: ObjectId("asdasdasd..."),
  dependencies: {
    customers: [
      ObjectId("1..."),
      ObjectId("2...")
    ],
    customersOf: [
      ObjectId("1..."),
      ObjectId("2...")
    ],
  }
}