java objectid_需要一种解决方法来查找objectID foreignField的字符串

博主作为MongoDB新手,在对医疗记录集合和疾病集合进行聚合查询时,因foreignField与localField类型不同导致$lookup失败。尝试使用$toObjectId转换类型时,查询返回Unrecognized expression '$toObjectId'错误,急需解决该问题。

我是mongodb的新手,目前我正面临着这个问题,

db.medical_records.aggregate([

{

"$group": {

"_id": {

"disease_id": "$disease_id" //a string

}, "count": { "$sum": 1 }

}

},

{

"$addFields": {

"disease_id": { "$toObjectId": "$disease_id" }

// i tried to change it into objectID so i could $lookup it

}

},

{

"$lookup": {

"from": "diseases",

"localField": "disease_id",

"foreignField": "_id",

"as": "disease"

}

}

])

这是我的医疗记录收集的一个例子

{

"_id" : ObjectId("5989c8f13f3958120800682e"),

"disease_id" : "5989c8f13f3958120800682f",

"patient_id" : "5989c8f13f3958120800681f"

}

疾病收集

{

"_id" : ObjectId("5989c8f13f3958120800682f"),

"name" : "Culpa autem officia.",

"code" : "Est aperiam."

}

而我期望的结果是那种,

{

"_id" : {disease_id: 5989c8f13f3958120800682f},

"count" : 20,

"disease" : {

"_id" : ObjectId("5989c8f13f3958120800682f"),

"name" : "Culpa autem officia.",

"code" : "Est aperiam."

}

}

如上所述,我需要将我的医疗记录收集到疾病收集中 .

当我尝试将其查找到疾病集合时,它失败了,因为foreignField与localField的类型不同 . 我一直在努力寻找解决这个问题的方法 . 上面的查询返回了另一个错误,

Unrecognized expression '$toObjectId'

这个问题可能已被问过几次,但我真的需要解决这个问题,请帮助

class MongoUtil(object): def __init__(self): db = pymongo.MongoClient().qa_system self.question = db.question self.answer = db.answer def query_question(self): question_iter_obj = self.question.aggregate([ {'$lookup': { 'from': 'answer', 'localField': '_id', 'foreignField': 'question_id', 'as': 'answer_list'}}]) question_list = [] for question in question_iter_obj: question_list.append( {'title': question['title'], 'detail': question['detail'], 'author': question['author'], 'vote_up': question['vote_up'] - question['vote_down'], 'answer_number': len(question['answer_list']), 'question_id': str(question['_id']) } ) return question_list def query_answer(self, question_id): answer_iter_obj = self.question.aggregate([ {'$match': {'_id': ObjectId(question_id)}}, {'$lookup': { 'from': 'answer', 'localField': '_id', 'foreignField': 'question_id', 'as': 'answer_list'}}]) question_answer = list(answer_iter_obj)[0] question_answer_dict = { 'question_id': str(question_answer['_id']), 'question_title': question_answer['title'], 'question_detail': question_answer['detail'], 'question_author': question_answer['author'], 'answer_num': len(question_answer['answer_list']) } answer_list = [] for answer in question_answer['answer_list']: answer_list.append( {'answer_detail': answer['answer'], 'answer_author': answer['author'], 'answer_id': str(answer['_id']), 'answer_vote': answer['vote_up'] - answer['vote_down']}) question_answer_dict['answer_list'] = answer_list return question_answer_dict
最新发布
11-28
# 学号+姓名 from bson import ObjectId from pymongo import MongoClient import datetime class MongoUtil(object): def __init__(self): self.client = MongoClient() self.db = self.client['qa_system'] # 数据库名:qa_system self.question = self.db['question'] # 问题集合 self.answer = self.db['answer'] # 回答集合 self.user = self.db['user'] # 用户集合 def save_user_info(self, username, password_hash): """保存用户信息到user集合""" now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') user_data = { 'username': username, 'password_hash': password_hash, 'register_time': now } result = self.user.insert_one(user_data) return str(result.inserted_id) # 返回用户ID def get_user_info(self, username): """查询用户信息""" user_info = self.user.find_one({'username': username}) if not user_info: return {'success': False, 'reason': '用户不存在!'} return {'success': True, 'user_info': user_info} def update_question(self, question_id, title, text): """更新问题""" self.question.update_one( {'_id': ObjectId(question_id)}, {'$set': {'title': title, 'detail': text}} ) return True def update_answer(self, answer_id, text): """更新回答""" self.answer.update_one( {'_id': ObjectId(answer_id)}, {'$set': {'answer': text}} ) return True def query_question(self): question_iter_obj = self.question.aggregate([ { '$lookup': { 'from': 'answer', 'localField': '_id', 'foreignField': 'question_id', 'as': 'answer_list' }}]) question_list = [] for question in question_iter_obj: question_list.append( { 'title': question['title'], 'detail': question['detail'], 'author': question['author'], 'vote_up': question['vote_up']-question['vote_down'], 'answer_number': len(question['answer_list']), 'question_id': str(question['_id']) } ) return question_list def insert_question(self, title, detail, author, ask_time): """插入新问题""" question_data = { 'title': title, 'detail': detail, 'author': author, 'ask_time': ask_time, 'vote_up': 0, 'vote_down': 0, 'answer_count': 0 } result = self.question.insert_one(question_data) return str(result.inserted_id) def insert_answer(self, question_id, answer, author, answer_time): """插入新回答""" answer_data = { 'question_id': question_id, 'answer': answer, 'author': author, 'answer_time': answer_time, 'vote_up': 0, 'vote_down': 0 } self.answer.insert_one(answer_data) # 更新问题的回答数量 self.question.update_one( {'_id': ObjectId(question_id)}, {'$inc': {'answer_count': 1}} ) def vote_for_question(self, object_id, value): self.question.update_one( {'_id': ObjectId(object_id)}, {'$inc': {value: 1}} ) # 0432230437曾敏 def vote_for_answer(self, object_id, value): self.answer.update_one( {'_id': ObjectId(object_id)}, {'$inc': {value: 1}} )修改代码
07-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值