0

I have a mongoDB database having many documents. Its structure is:

  "Tenant_Details": {
  "$Tenant_Name": {
    "Tenant Name" : "$Tenant_Name",
    "Tenant Address" : "$Tenant Address",
    "Tenant Contact" : "$ Tenant number",
    "System_ID" : "$System_ID",
    "$System_ID": [
      "List of assigned device numbers for $System_ID"
    ]
  }
}

Here $ sign indicates the variable data. But I am confused about looping through the Tenant Details for getting the "List of assigned device numbers". I won't be knowing the Tenant_Name, so I need to look at all documents of Tenant details matching the device numbers for System_ID.

How do I loop without knowing the Tenant_Name?

EDIT1: I tried by storing the list of the Tenant Names in the DB and by looping through the list it worked. I tried like this:

TenantNames = db.find_one() # list of tenant Names
TenantDetailsObj = db.find_one()
SystemID = 1
deviceNumber = 2
for names in TenantNames:
   if deviceNumber in TenantDetailsObj.get(name).get(str(SystemID)):
           GotData = 1

But in this the Tenant name was known hence looping was easy, but I am not getting without knowing Tenant Name how do I loop?

Noob
  • 318
  • 1
  • 3
  • 11
  • 5
    A question on SO can not replace taking the time to get familiar with a topic, tool or library on your own. Please show us what you have tried or research so far! – Klaus D. Nov 19 '18 at 11:56
  • @KlausD. I have updated the question please check once. – Noob Nov 19 '18 at 12:06
  • Possible duplicate of: [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops). Tentatively not "dupehammering" myself just yet. But this is what the question looks like. – Neil Lunn Nov 19 '18 at 13:24
  • This document doesn't seem right. Why not have the following instead: `{ "Tenant_Name": "abc", "Tenant Address" : "$Tenant Address", "Tenant Contact" : "$ Tenant number", "System_ID" : "$System_ID", "$System_ID": [ "List of assigned device numbers for $System_ID" ] }` – Hagai Nov 19 '18 at 13:39

0 Answers0