这是我的数据集。我花了很多时间在此上,但无法获得所需的实际结果。请帮助我找到我的数据
您可以在下面的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"
        }
    ]
}

最佳答案

您的查询:

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
})


现在,此查询将仅检索学科名称为“计算机科学”且学位名称为“计算机科学”的大学。

请检查MongoDB的官方documentation数组查询运算符。
当MongoDB中的字段包含数组时,您必须应用这些运算符。

08-07 06:26