Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

python - How do you iterate over a set or a list in Flask and PyMongo?

I have produced a set of matching IDs from a database collection that looks like this:

{ObjectId('5feafffbb4cf9e627842b1d9'), ObjectId('5feaffcfb4cf9e627842b1d8'), ObjectId('5feb247f1bb7a1297060342e')}

Each ObjectId represents an ID on a collection in the DB.

I got that list by doing this: (which incidentally I also think I am doing wrong, but I don't yet know another way)

    # Find all question IDs
    question_list = list(mongo.db.questions.find())
    all_questions = []
    for x in question_list:
        all_questions.append(x["_id"])

    # Find all con IDs that match the question IDs
    con_id = list(mongo.db.cons.find())
    con_id_match = []
    for y in con_id:
        con_id_match.append(y["question_id"])

    matches = set(con_id_match).intersection(all_questions)
    
    print("matches", matches)   
    print("all_questions", all_questions)
    print("con_id_match", con_id_match)

And that brings up all the IDs that are associated with a match such as the three at the top of this post. I will show what each print prints at the bottom of this post.

Now I want to get each ObjectId separately as a variable so I can search for these in the collection.

mongo.db.cons.find_one({"con": matches})

Where matches (will probably need to be a new variable) will be one of each ObjectId's that match the DB reference.

So, how do I separate the ObjectId in the matches so I get one at a time being iterated. I tried a for loop but it threw an error and I guess I am writing it wrong for a set. Thanks for the help.

Print Statements:

**matches** {ObjectId('5feafffbb4cf9e627842b1d9'), ObjectId('5feaffcfb4cf9e627842b1d8'), ObjectId('5feb247f1bb7a1297060342e')}

**all_questions** [ObjectId('5feafb52ae1b389f59423a91'), ObjectId('5feafb64ae1b389f59423a92'), ObjectId('5feaffcfb4cf9e627842b1d8'), ObjectId('5feafffbb4cf9e627842b1d9'), ObjectId('5feb247f1bb7a1297060342e'), ObjectId('6009b6e42b74a187c02ba9d7'), ObjectId('6010822e08050e32c64f2975'), ObjectId('601d125b3c4d9705f3a9720d')]

**con_id_match** [ObjectId('5feb247f1bb7a1297060342e'), ObjectId('5feafffbb4cf9e627842b1d9'), ObjectId('5feaffcfb4cf9e627842b1d8')]
question from:https://stackoverflow.com/questions/66064842/how-do-you-iterate-over-a-set-or-a-list-in-flask-and-pymongo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Usually you can just use find method that yields documents one-by-one. And you can filter documents during iterating with python like that:

# fetch only ids
question_ids = {question['_id'] for question in mongo.db.questions.find({}, {'_id': 1})}

matches = []
for con in mongo.db.cons.find():
    con_id = con['question_id']
    if con_id in question_ids:
        matches.append(con_id)
        # you can process matched and loaded con here
    
print(matches)

If you have huge amount of data you can take a look to aggregation framework


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...