这是我的数据集。我花了很多时间在此上,但无法获得我想要的实际结果。请帮助我找到我的数据
您可以在下面的JSON
中看到我要提取的所有包含特定学科名称的学科,例如学科名称:“计算机科学”
那么我只想提取包含(discipline_Name:“计算机科学”)的那些字典
另外,请告诉我如何获取该特定词典的单个数据
我在python中这样做
我尝试了以下代码,但获得了实际输出
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"
}
]
}
答案 0 :(得分:0)
您的查询:
mydb.balochistan_uni.find({
"disciplines.discipline_name": "Computer Science",
"disciplines.degree_Name": "BE"
})
收回所有拥有“计算机科学”学科和“ BE”学位的大学。您的查询未指定其为学位“ BE”的学科“计算机科学”。
如果要这样做,必须匹配数组disciplines
中的元素。查询应如下所示:
mydb.balochistan_uni.find({
"disciplines": {
"$elemMatch": {
"discipline_name": "Computer Science",
"degree_Name": "BE"
}
}
},
{
"_id": 0, "disciplines": 1
})
现在,该查询将仅检索学科名称为“计算机科学”且学位名称为“ BE”的大学。
请检查MongoDB的官方documentation,以了解数组查询运算符。
当MongoDB中的字段包含数组时,您必须应用这些运算符。
答案 1 :(得分:0)
要仅过滤符合条件的学科,必须使用聚合框架。一种方法:
mydb.balochistan_uni.aggregate(
[
{
$redact: {
$cond: {
if: { $or:[
{$eq: [ "$discipline_name", "Computer Science" ] },
{ "$ifNull": [ "$disciplines", false] }
]},
then: "$$DESCEND",
else: "$$PRUNE"
}
}
},
{
$unwind: {
path : "$disciplines",
}
},
{
$replaceRoot: {
newRoot: "$disciplines"
}
},
]
);
将返回:
{
"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 Science",
"degree_Name" : "BS",
"duration" : "4 Years",
"Fee_per_Year" : "15000",
"admission_date" : "14-09-2018",
"Last_Merit" : "NA"
}