Determine whether collection in MongoDB exists in Python - mongodb

I want to know whether collections of specific names exists in the MongoDB. How can I achieve this programmatically in Python. On searching about the same, I got to know how to do that from MongoDB shell but nothing useful for doing the same in Python.

You can use the method to retrieve and check if your collection exists or not from the comment given by the #Alex like this:
Method 1:
import pymongo
connection = pymongo.MongoClient('localhost', 27017) # Connect to mongodb
db = connection['test_db']
list_of_collections = db.list_collection_names() # Return a list of collections in 'test_db'
print("posts" in list_of_collections) # Check if collection "posts" exists in db (test_db)
Or, you can validate a collection with validate_collection() (documentation) This returns an error (pymongo.errors.OperationFailure) if the collection doesn't exist. With this method, you can also catch that exception and do whatever you want.
Method 2:
import pymongo
connection = pymongo.MongoClient('localhost', 27017) # Connect to mongodb
db = connection['test_db']
try:
db.validate_collection("random_collection_name") # Try to validate a collection
except pymongo.errors.OperationFailure: # If the collection doesn't exist
print("This collection doesn't exist")

Related

Unable to pull ALL documents from MongoDB collection in jmeter

Unable to pull all the documents from MongoDB collection using Jmeter JSR233 sampler.
I’m trying to pull all the documents from MongoDB collection to jmeter to pass it as a test case for my performance test execution.
The below works -
myDoc = collection.find(eq("ABCValue", "ABC")).first();
log.info myDoc.toJson();
collection.find(...query...).last(); also works.
I’m able to pull the first and last value from MongoDB collection for that query. However unable to pull all the documents from the collection when I try to use the following -
myDoc = collection.find();
log.info myDoc.toJson();
This does not work only in Jmeter. Please help!
To print all documents returned from find(), you need to iterate over the documents returned
for (Document cur : collection.find()) {
log.info cur.toJson();
}
The find() method returns a FindIterable() instance that provides a fluent interface for chaining other methods.

MongoDB import to different collections set by a field

I have a file called data.json and extracted with mongoexport, with the following structure:
{"id":"63","name":"rcontent","table":"modules"}
{"id":"81","name":"choicegroup","table":"modules"}
{"id":"681","course":"1242","name":"Requeriments del curs","timemodified":"1388667164","table":"page"}
{"id":"682","course":"1242","name":"Guia d'estudi","timemodified":"1374183513","table":"page"}
What I need is to import this file into my local mongodb with a command like mongoimport or with pymongo, but storing every line in the collection named after the table value.
For example, the collection modules would contain the documents
{"id":"63","name":"rcontent"} and {"id":"81","name":"choicegroup"}
I've tried with mongoimport but I haven't seen any option which allows that. Does anyone know if there is a command or a method to do that?
Thank you
The basic steps for this using python are:
parse the data.json file to create python objects
extract the table key value pair from each document object
insert the remaining doc into a pymongo collection
Thankfully, pymongo makes this pretty straightforward, as below:
import json
from pymongo import MongoClient
client = MongoClient() # this will use default port and host
db = client['test-db'] # select the db to use
with open("data.json", "r") as json_f:
for str_doc in json_f.readlines():
doc = json.loads(str_doc)
table = doc.pop("table") # remove the 'table' key
db[table].insert(doc)

How to create a collection in MongoDB using SPRING data

Am using Spring-Data-Mongo to access do CRUD operations on my mongo database. I execute the below line
DB db = mongoTemplate.getDb()
When am in debug mode I can see that db._collections properties has 4 values (collections that I inserted). But when I query for
db.getCollectionNames()
I get zero collections back. Why is that? Same is also true when I do
db.getCollection("collectionName")
But I know the collections do exists because when I do something like
mongoTemplate.createCollection("collectionName");
I get an exception saying that collection already exists. Can anyone please explain what I might be missing
MongoTemplate provides a few methods for managing collections. The following example demonstrates some of the methods:
DBCollection collection = null;
if (!mongoTemplate.getCollectionNames().contains("collectionName")) {
collection = mongoTemplate.createCollection("collectionName");
}
mongoTemplate.dropCollection("collectionName");
In the above, getCollectionNames() returns a set of collection names and dropCollection() drops the collection.
Use MongoClient, MongoDatabase, and MongoIterable of com.mongodb.client and com.mongodb package.
MongoClient client = MongoClient(<host>, port);
MongoDatabase db = client.getDatabase(<Name of the database>);
MongoIterable<String> = db.listCollectionNames();
And now you can iterate over all the names of the collections.
Additionally, you even can use MongoCollection class to get the Document from the specified collection. The getCollection() will create collection if not present.
MongoCollection<Document> collection = db.getCollection(<Collection name>);

Pymongo bulk inserts not working

I am following the tutorial http://api.mongodb.org/python/current/tutorial.html for bulk inserts. However, I am getting the error that I have listed below. What am I missing? The reviews_array is a json_array
client = MongoClient()
client = MongoClient('localhost', 27017)
db = client.is_proj
db_handle = db.reviews
self.db_handle.insert_many(reviews_array)
The Error:
TypeError: 'Collection' object is not callable. If you meant to call the 'insert_many' method on a 'Collection' object it is failing because no such method exists.
In pymongo, before V3.0, you use insert for both single-doc and bulk insert. If you pass a document, it performs a single-insert, and if you pass a different iterable (list, generator), it performs bulk insert.
insert_many() does not exist in pymongo before V3.0.
In pymongo V3.0, they introduced insert_many and insert_one, and the use of the plain insert is deprecated.
I also got this error. While shx2's answer is right here, i got the error because i was using mongoengine and tried insert_many on the database, and it is a collection method, so it makes sense to do conn[database][collection].insert_many(iterable) and not conn[database].insert_many(iterable).

why mongo is fetching results even after deleting the collection?

using pymongo i have deleted a particular collection. when i query the database using mongoengine x.objects.get(y="something") is still showing up with the deleted result.
anyone please help me with this.. Thanks in advance.
You could try using the drop method:
from pymongo import Connection
connection = Connection('localhost', 27017) #Connect to mongodb
print(connection.database_names()) #Return a list of db, equal to: > show dbs
db = connection['testdb1'] #equal to: > use testdb1
print(db.collection_names()) #Return a list of collections in 'testdb1'
print("posts" in db.collection_names()) #Check if collection "posts"
# exists in db (testdb1)
collection = db['coll']
print(collection.count() == 0) #Check if collection named 'coll' is empty
collection.drop() #Delete (drop) collection named 'coll' from db