0

Need help in understanding what is happening here and a suggestion to avoid this!

Here is my snippet:

      result = [list of dictionary objects(dictionary objects have 2 keys and 2 String values)]
      copyResults = list(results);
      ## Here I try to insert each Dict into MongoDB (Using PyMongo)
      for item in copyResults:
        dbcollection.save(item) # This is all saving fine in MongoDB.

But when I loop thru that original result list again it shows dictionary objects with a new field added automatically which is ObjectId from MongoDB!

Later in code I need to transform that original result list to json but this ObjectId is causing issues.No clue why this is getting added to original list.

I have already tried copy or creating new list etc. It still adds up ObjectId in the original list after saving.

Please suggest!

2 Answers 2

1

every document saved in mongodb requires '_id' field - which has to be unique among documents in the collection. if you don't provide one, mongodb will automatically create one with ObjectId (bson.objectid.ObjectId for pymongo)

If you need to export documents to json, you have to pop '_id' field before jsonifying it.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I just did not want to loose my original list. But I was thinking that I am doing something wrong. And You confirmed that there is nothing wrong and only way would be removing id so thanks!
And I did this now which works fine.Before inserting data = json.dumps(item) saveUserTweets(json.loads(data)) This kept my original item untouched :)
glad it works. If you've found my answer helpful you can accept it. also, just saving json dumps into database can be problematic if you have to query the db in future.
0

Or you could use:
rows['_id'] = str(rows['_id'])

Remember to set it back if you then need to update

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.