Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:44:48+00:00 2026-05-28T01:44:48+00:00

Using the pymongo driver bare to connect python to mongodb, why is it that

  • 0

Using the pymongo driver bare to connect python to mongodb, why is it that using an ObjectId instance as the key for an embedded document raises an InvalidDocument error?

I am trying to link documents using objectids and cant seem to understand why I would want to convert them to strings when the ones created automatically for the driver are ObjectId instances.

item = collection.find({'x':'foo'})
item['otherstuff'] = {pymongo.objectid.ObjectId() : 'data about this link'}
collection.update({'x':'foo'}, item)
bson.errors.InvalidDocument: documents must have only string keys, key was ObjectId('4f0b5d4e764df61c67000000')

In practice the linked ids represent documents that contain questions, and the values in the dictionary here keyed as ‘otherstuff’ for example would represent this individual document’s responses to that particular question.

Is there a reason applying objectids like this won’t encode into bson and then fails? Is it impossible to nest ObjectIds within documents like this to cross-reference? Have I misunderstood the purpose of them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:44:49+00:00Added an answer on May 28, 2026 at 1:44 am

    The BSON spec dictates that keys must be strings, so PyMongo is right to reject this as an invalid document (and would be regardless of at what level an ObjectId was used as a key, whether at the top level or in an embedded document). This is necessary, among other reasons, so that the query language can be unambiguous. Imagine you had this document (and that it were a valid BSON document):

    { _id: ...,
      "4f0cbe6d7f40d36b24a5c4d7":           true,
      ObjectId("4f0cbe6d7f40d36b24a5c4d7"): false
    }
    

    And then you attempted to query with:

    db.foo.find({"4f0cbe6d7f40d36b24a5c4d7": false})
    

    Should this return this document? Should that string be auto-boxed into an ObjectId? How would Mongo know when that can be auto-boxed, and how to disambiguate in cases like this document?

    A possible alternative solution to your problem is to have an array of embedded documents like:

    { answers: [
        { answer_id: ObjectId("..."), summary: "Good answer to this question" },
        { answer_id: ObjectId("..."), summary: "Bad answer to this question" }
      ]
    }
    

    This is valid BSON, and will also be indexable more efficiently. If you add an index on answers, you can search efficiently for exact matches on these subdocuments; if you add an index on answers.answer_id, then you can search efficiently by the ObjectId of the answer you’re looking for (and so on).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to implement a login method for mongodb , using python (pymongo) and
I'm working on creating scripts using python, mongodb and the pymongo module to fetch
I'm using pymongo: http://api.mongodb.org/python/1.9%2B/index.html for post in db.datasets.find({set:flickr}).sort(num_favs,-1): ... How come I can't sort
I am trying to perform a regex query using PyMongo against a MongoDB server.
I am unable to install pymongo using easy_install on python 3.2. c:\Python32\Scripts>easy_install pymongo ...
I've recently started testing MongoDB via shell and via PyMongo. I've noticed that returning
I am using pymongo driver. Supposedly, one can use a string to query the
Using Pymongo for this scenario. I have User that has email, first_name, last_name. I
I'm trying to update a row in a mongoDB using pymongo. It seems to
Using PyObjC , you can use Python to write Cocoa applications for OS X.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.