Mongodb Find All Dictionaries In A List That Contains Specific Discipline_Name And Extract Individual Data Of That Dictionary
This is my data set. I have consumed a lot of time on this but cannot get the actual outcome I want. Please help me in finding my data
You can see the JSON
below I want to extract all the disciplines that contain specific discipline_Name like discipline_Name: "Computer Science"
then I want to extract only those dictionaries that contain (discipline_Name: "Computer Science")
Also, tell me how to fetch individual data of that particular dictionary
I doing this in python
I have tried the following code but getting the actual output
import requests
import re
import json
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["FYP_DataBase"]
mycol = mydb["balochistan_uni"]
data = mydb.balochistan_uni.find({"disciplines.discipline_name":"Computer Science","disciplines.degree_Name":"BE"},{ '_id':0 ,"disciplines":1,"discipline.discipline_Name" :1, })
for d in data:
print(d)
.........................................................
{
"_id" : ObjectId("5b98e604c0c4d54a5016e646"),
"uni_name" : "Balochistan University Of Engineering & Technology BUET, Khuzdar",
"location" : "KHUZDAR P.O Box No. 89100 [BALOCHISTAN] ",
"web_link" : "http://www.buetk.edu.pk ",
"province" : "Balochistan",
"category" : "Public",
"disciplines" : [
{
"discipline_name" : "Civil",
"degree_Name" : "BE",
"duration" : "4 Years",
"Fee_per_Year" : "NA",
"admission_date" : "15-09-2018",
"Last_Merit" : "50"
},
{
"discipline_name" : "Computer Science",
"degree_Name" : "BS",
"duration" : "4 Years",
"Fee_per_Year" : "NA",
"admission_date" : "31-01-2018",
"Last_Merit" : "NA"
},
{
"discipline_name" : "Computer System",
"degree_Name" : "BE",
"duration" : "4 Years",
"Fee_per_Year" : "NA",
"admission_date" : "15-09-2018",
"Last_Merit" : "NA"
}
]
}
{
"_id" : ObjectId("5b98e60dc0c4d54a5016e648"),
"uni_name" : "Lasbela University Of Agriculture, Water And Marine Sciences, Uthal LUAWMS, Lasbela",
"location" : "Lasbela ",
"web_link" : "http://www.luawms.edu.pk ",
"province" : "Balochistan",
"category" : "Public",
"disciplines" : [
{
"discipline_name" : "Agriculture",
"degree_Name" : "BS",
"duration" : "4 Years",
"Fee_per_Year" : "15000",
"admission_date" : "14-09-2018",
"Last_Merit" : "NA"
},
{
"discipline_name" : "Business Administration",
"degree_Name" : "BBA",
"duration" : "4 Years",
"Fee_per_Year" : "15000",
"admission_date" : "14-09-2018",
"Last_Merit" : "NA"
},
{
"discipline_name" : "Computer Science",
"degree_Name" : "BS",
"duration" : "4 Years",
"Fee_per_Year" : "15000",
"admission_date" : "14-09-2018",
"Last_Merit" : "NA"
}
]
}
Answer
Your query:
mydb.balochistan_uni.find({
"disciplines.discipline_name": "Computer Science",
"disciplines.degree_Name": "BE"
})
retrieves all universities that have a discipline "Computer Science" and a degree "BE". Your query doesn't specify that it should be the discipline "Computer Science" with a degree "BE".
If you want to do that you have to match an element within the array disciplines
. The query should look like this:
mydb.balochistan_uni.find({
"disciplines": {
"$elemMatch": {
"discipline_name": "Computer Science",
"degree_Name": "BE"
}
}
},
{
"_id": 0, "disciplines": 1
})
Now this query will retrieve only universities where the discipline name is "Computer Science" and the degree name for "Computer Science" is "BE".
Please check the official documentation of MongoDB for Array Query Operators.
When a field in MongoDB contains an array, you have to apply those operators.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module