我有一个json文件叫做学生.json内容如下:
{
"Student": [
{
"fees": "$21.00"
},
{
"date": "19/11/2018"
}
]
}
现在我试图使用PyMongo来提取“fees”和“date”的值,以便进行进一步的分析,但它不起作用。请建议。你知道吗
import json
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client['student_db']
collection_student = db['student']
with open('student.json') as f:
file_data = json.load(f)
collection_student.insert_one(file_data)
with client:
db = client.student_db
a = db.student.find({"Student.fees": 1})
print(a.next())
在Mongo控制台上,我试着这样做:
db.student.aggregate([
{
$unwind : "$Student"
},
{
$project : {
_id : 0,
Student : 1
}
}
])
输出:
{ "Student" : { "fees" : "$21.00" } }
{ "Student" : { "date" : "19/11/2018" } }
编辑(1): 根据我的建议,我试着如下,但它打印{}为'日期'。如何避免这种情况?你知道吗
db.student.find({},{"Student.fees":1});
{ "_id" : ObjectId("5df3656fb3439edbccd2cca9"), "Student" : [ { "fees" : "$21.00" }, { } ] }
编辑(2):尝试了一些它在控制台上的工作:
db.getCollection('student').find().forEach(function(student){
... ... print("Student _id : " + student._id);
... ... print("Student Key : " + JSON.stringify(student.Student[0]));
... ... print("Student Fees : " + student.Student[0].fees)
... ... print("Student Date : " + student.Student[1].date)
... ... })
Student _id : 5df3656fb3439edbccd2cca9
Student Key : {"fees":"$21.00", "date":"19/11/2018"}
Student Fees : $21.00
Student Date : 19/11/2018
目前没有回答
相关问题 更多 >
编程相关推荐