Pymongo bulk inserts not working - mongodb

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).

Related

Morphia Upserting Field as int64 instead of in32

I've written an operation upsert a document to Mongo using Morphia.
I have a field which I want to save as an int32, but after the upsert it is inserted as int64.
I have made sure that I convert the long as an int using Long.intValue() and the object that Morphia serializes back to, the member field is int. I have also checked the UpdateOperations.ops to see what morphia is upserting.
The upsert operation is:
UpdateOperations<Test> ops = datastore.createUpdateOperations(Test.class)
.set("test_field", testField.intValue())
The current version of Mongo I am using is 3.0.
Any help would be appreciated!
Thanks!
EDIT:
Looking at the update query operation in Morphia it is:
{$set={test_field=11}}
I managed to find out the solution. The query for the upsert was querying on the test_field as a long rather than an int. Mongo 3.0 sees this as the type to insert - however running on Mongo 3.2, there is no issue and it will upsert with the type specified in the upsert operation, not the query.

Determine whether collection in MongoDB exists in Python

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")

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>);

Updating array in mongodb in safe mode

I'd like to update an array element in mongodb. In the mongodb shell this works:
db.ipolls.update({_id:"5Qu9fXG84tNSZo7sv","players.label":"R1"},{$inc:{"players.$.score":1}});
But when I run this in meteor:
Ipolls.update( {_id:pollster,"players.label":notChosen.label},{$inc:{"players.$.comparisons":1}});
I get the error: Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]
Is it possible to run this query on the client side?
On the client you can only use the _id field as a selector. You've used {_id:pollster,"players.label":notChosen.label}
This is a meteor thing, its just to make it a bit safer. You could theoretically create a weird selector and get information out of the .allow rule checks were this not the case.
Query for the document first, then use that to update it:
var doc_to_update = Ipolls.findOne({_id:pollster,"players.label":notChosen.label});
Ipolls.update( {_id: doc_to_update._id},{$inc:{"players.$.comparisons":1}});

mongoDB Object DBCursor has no method 'sort'

so i created a collection called food with 2 objects that were saved no problem. Using find() yielded no issues either. However, when I entered the following:
db.food.find().sort({averageRating:-1}).select({_id: 1}).limit(2)
I get the error:
JS Error: TypeError: Object DBCursor has no method 'sort'
What am i doing wrong?
Is this what you are looking for?
db.food.find({},{_id:1}).sort({"averageRating":-1}).limit(2);
It selects only 2 id fields ordered by average rating descending.The fields that are to be returned are specified by the second parameter in find(),which in this case is _id.
select is not a valid command in mongoDb as far as I know.
It should be selector, not select. See if that fixes it.
As per shargors' comment, it looks like try.mongodb.org doesn't support sort(). I would recommend downloading and installing mongodb itself, and playing around with the real shell.