How do I loop through documents in MongoDB using Python?
up vote
0
down vote
favorite
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?
python mongodb
New contributor
add a comment |
up vote
0
down vote
favorite
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?
python mongodb
New contributor
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 at 11:56
@KlausD. I have updated the question please check once.
– MrAlpha
Nov 19 at 12:06
Possible duplicate of: Iterating over dictionaries using 'for' loops. Tentatively not "dupehammering" myself just yet. But this is what the question looks like.
– Neil Lunn
Nov 19 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 at 13:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
python mongodb
New contributor
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?
python mongodb
python mongodb
New contributor
New contributor
edited Nov 19 at 12:06
New contributor
asked Nov 19 at 11:52
MrAlpha
216
216
New contributor
New contributor
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 at 11:56
@KlausD. I have updated the question please check once.
– MrAlpha
Nov 19 at 12:06
Possible duplicate of: Iterating over dictionaries using 'for' loops. Tentatively not "dupehammering" myself just yet. But this is what the question looks like.
– Neil Lunn
Nov 19 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 at 13:39
add a comment |
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 at 11:56
@KlausD. I have updated the question please check once.
– MrAlpha
Nov 19 at 12:06
Possible duplicate of: Iterating over dictionaries using 'for' loops. Tentatively not "dupehammering" myself just yet. But this is what the question looks like.
– Neil Lunn
Nov 19 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 at 13:39
5
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 at 11:56
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 at 11:56
@KlausD. I have updated the question please check once.
– MrAlpha
Nov 19 at 12:06
@KlausD. I have updated the question please check once.
– MrAlpha
Nov 19 at 12:06
Possible duplicate of: Iterating over dictionaries using 'for' loops. Tentatively not "dupehammering" myself just yet. But this is what the question looks like.
– Neil Lunn
Nov 19 at 13:24
Possible duplicate of: Iterating over dictionaries using 'for' loops. Tentatively not "dupehammering" myself just yet. But this is what the question looks like.
– Neil Lunn
Nov 19 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 at 13:39
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 at 13:39
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
MrAlpha is a new contributor. Be nice, and check out our Code of Conduct.
MrAlpha is a new contributor. Be nice, and check out our Code of Conduct.
MrAlpha is a new contributor. Be nice, and check out our Code of Conduct.
MrAlpha is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374075%2fhow-do-i-loop-through-documents-in-mongodb-using-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 at 11:56
@KlausD. I have updated the question please check once.
– MrAlpha
Nov 19 at 12:06
Possible duplicate of: Iterating over dictionaries using 'for' loops. Tentatively not "dupehammering" myself just yet. But this is what the question looks like.
– Neil Lunn
Nov 19 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 at 13:39