Search code examples
pythonmongodbpython-2.7pymongopython-idle

How To Suppress Display of ObjectIDs when Inserting Documents into MongoDB Through IDLE


When you insert a single document or set of documents to a MongoDB instance using Pymongo via IDLE, the ObjectIDs of all the successfully inserted documents are echoed to the standard output.

>>> new_posts = [{"author": "Mike",
...               "text": "Another post!",   
...               "tags": ["bulk", "insert"],
...               "date": datetime.datetime(2009, 11, 12, 11, 14)},
...              {"author": "Eliot",
...               "title": "MongoDB is fun",
...               "text": "and pretty easy too!",
...               "date": datetime.datetime(2009, 11, 10, 10, 45)}]
>>> posts.insert(new_posts)
[ObjectId('...'), ObjectId('...')]

Is there an option I can run to stop these ObjectIDs from being displayed? The problem is that IDLE gets buggy when it has to render large bits of text (say, tens of thousands of added objectids).

FWIW, I'm using Python 2.7 and the most recent version of PyMongo. Happy to provide more details if necessary.

Thanks,

Ben


Solution

  • You can assign the result to a variable and then it won't be echoed.

    >>> echo=d.echo
    >>> echo.insert({"new":"test"})
    ObjectId('50647520594bc0f223000000')
    >>> new=echo.insert({"new":"test"})
    >>>