

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to insert a Python object in Mongodb?
You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo. For example,
Example
from pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")
Output
This will give the output −
Object inserted!
Note − Always use datetime.datetime.utcnow(), which returns the current time in UTC, instead of datetime.datetime.now(), which returns the current local time.
- Related Questions & Answers
- How to insert a Python object in MySQL?
- How to insert date object in MySQL using Python?
- How to insert a boolean field in MongoDB?
- How to insert Date in MongoDB?
- How to insert an object in a list at a given position in Python?
- How to insert a document with date in MongoDB?
- How to insert LONG numbers in MongoDB?
- How to insert an item to an array that is inside an object in MongoDB?
- How to retrieve a nested object in MongoDB?
- How to get a saved object in MongoDB?
- How to prepare a Python date object to be inserted into MongoDB?
- How to update MongoDB Object?
- How to insert a row into a ResultSet object using JDBC?
- How to insert a DATALINK object into a table using JDBC?
- How to insert a document into a MongoDB collection using Java?
Advertisements