Why is PyMongo literally inserting into collection? - python

I would like the data to be inserted in mycollection, but it'll literally insert into a collection called 'collection' when I use the collection variable before insert_one.
client = MongoClient()
db = client['mydb']
collection = db['mycollection']
db.collection.insert_one({"id": "hello"})

I didn't realize I had to remove the db part. This worked:
collection = db['mycollection']
collection.insert_one({"id": "hello"})

Related

How to print the result of my pymongo.cursor.Cursos object?

This is my code so far:
from pymongo import MongoClient
client = MongoClient()
client = MongoClient('localhost', 27017)
db = client.local
collection = db.orderbook_update
orderbook = collection.find({
"lastUpdated": {"$lt": ts}
}).sort("position",pymongo.DESCENDING).limit(1)
print(orderbook)
When I do that, my print(orderbook) gives me that: <pymongo.cursor.Cursor object at 0x7ff7defef828>
How am I able to print my result in order to use it? My json file on my database has three main components: lastUpdated, asks, bids.
Thank you!
order = list(orderbook)
Note: Once you do do this cursor object will not be available

How to create collection with index if not exists in pymongo?

I was using automatic collection creation in pymongo:
client = MongoClient(url)
db = client.get_default_database()
my_collection = db['my_collection']
I was thinking it created automatically on last statement and added
index_name = 'my_index'
if index_name not in my_collection.index_information():
my_collection.create_index(..., name=index_name, unique=False)
Unfortunately, this led to error
pymongo.errors.OperationFailure: Collection ... doesn't exist
This made me think, that collection is created on first save. This gives me no place to put index creation code.
So, the question is: how to create collection with index, but only if it doesn't exist?
I have read this answer https://stackoverflow.com/a/9826294/258483 but don't like how it implies to write check of existence twice:
client = MongoClient(url)
db = client.get_default_database()
if 'my_collection' not in db.collection_names():
db.createCollection('my_collection')
my_collection = db['my_collection']
index_name = 'my_index'
if index_name not in my_collection.index_information():
my_collection.create_index(..., name=index_name, unique=False)
As you see, calling index_information on a collection that doesn't exist yet throws OperationFailure.
Just call create_index without checking first:
client = MongoClient(url)
db = client.get_default_database()
my_collection = db['my_collection']
index_name = 'my_index'
my_collection.create_index(..., name=index_name, unique=False)
If the index already exists, the MongoDB server ignores the command. If the index does not exist, MongoDB creates the collection (if necessary) and the index on the collection.

How can I connect to two different databases using MongoClient() in Pymongo?

I need to access two different collection each in their respective databases on the same server. For example i need the collection "dummy" in the database "dummy" and collection "foo" in database "bar". To connect to single database I have been using this code
client = MongoClient()
db = client.dummy()
collection = db['dummy']
But if I also add
db1 = client.bar
collection = db1['foo']
This is not working.

How to run raw mongodb commands from pymongo

In a mongo command line I can run
db.my_collection.stats()
I need to get my collections stats from Python so I tried
from pymongo import MongoClient
client = MongoClient()
db = client.test_database
collection = db.test_collection
collection.stats()
But I get
TypeError: 'Collection' object is not callable.
If you meant to call the 'stats' method on a 'Collection' object it is failing because no such method exists.
This is because pymongo does not support this method. How do I send raw mongoDB commands to mongo through Python?
from pymongo import MongoClient
client = MongoClient()
db = client.test_database
print(db.command("collstats", "test_collection"))
Approach 1 with PyMongo:
client = pymongo.MongoClient(host = "127.0.0.1", port = 27017)
db = client.test_database
db.command("dbstats") # prints database stats for "test_db"
db.command("collstats", "test_collection") # prints collection-level stats
This can be done with this approach in Django.
from django.db import connections
database_wrapper = connections['my_db_alias']
eggs_collection = database_wrapper.get_collection('eggs')
eggs_collection.find_and_modify(...)
From django-mongodb-engine docs:
django.db.connections is a dictionary-like object that holds all
database connections – that is, for MongoDB databases,
django_mongodb_engine.base.DatabaseWrapper instances.
These instances can be used to get the PyMongo-level Connection,
Database and Collection objects.

how to determine whether a field exists?

I'm connecting to my mongodb using pymongo:
client = MongoClient()
mongo = MongoClient('localhost', 27017)
mongo_db = mongo['test']
mongo_coll = mongo_db['test'] #Tweets database
I have a cursor and am looping through every record:
cursor = mongo_coll.find()
for record in cursor: #for all the tweets in the database
try:
msgurl = record["entities"]["urls"] #look for URLs in the tweets
except:
continue
The reason for the try/except is because if ["entities"]["urls"] does not exist, it errors out.
How can I determine whether ["entities"]["urls"] exists?
Record is a dictionary in which the key "entities" links to another dictionary, so just check to see if "urls" is in that dictionary.
if "urls" in record["entities"]:
If you just want to proceed in any case, you can also use get.
msgurl = record["entities"].get("urls")
This will cause msgurl to equal None if there is no such key.
I'm not familiar with pymongo, but why don't you change your query so it only returns results that contain "urls"? Something like:
mongo_coll.find({"entities.urls": {$exists:1}})
http://docs.mongodb.org/manual/reference/operator/exists/

Categories