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?










share|improve this question









New contributor




MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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

















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?










share|improve this question









New contributor




MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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















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?










share|improve this question









New contributor




MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 19 at 12:06





















New contributor




MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 11:52









MrAlpha

216




216




New contributor




MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






MrAlpha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 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




    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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






MrAlpha is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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






























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.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen