Skip to main content

Posts

PYTHON MONGODB LIMIT

M ONGODB LIMIT Limit the Result To limit the result in MongoDB, we use the limit() method. The limit() method takes one parameter, a number defining how many documents to return. Limit the result to only return 5 documents: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] myresult = mycol.find().limit(5) for x in myresult:   print(x) {'_id': ObjectId('6325d5001d9e9d4c027643b8'), 'name': 'Kartik'} {'_id': ObjectId('6325e1ef7e4337dbd6d8b9a1'), 'name': 'Kartik'} {'_id': ObjectId('6325e20d0bea1e2841fcdd06'), 'name': 'Kartik'} {'_id': ObjectId('6325e2381039de66af625c07'), 'name': 'Kartik'} {'_id': ObjectId('6325e26da4ef8259d33e1a33'), 'name': 'Kartik'}

PYTHON UPDATE COLLECTION

U PDATE COLLECTION Update Collection You can update a record, or document as it is called in MongoDB, by using the update_one() method. The first parameter of the update_one() method is a query object defining which document to update. Note: If the query finds more than one record, only the first occurrence is updated. The second parameter is an object defining the new values of the document. Change the address from "Valley" to "Canyon": import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] myquery = { "address": "Valley 345" } newvalues = { "$set": { "address": "Canyon 123" } } mycol.update_one(myquery, newvalues) #print "customers" after the update: for x in mycol.find():   print(x) Update Many To update all documents that meets the criteria of the query, use the update_

PYTHON MONGODB DROP COLLECTION

M ONGODB DROP COLLECTION Delete Collection You can delete a table, or collection as it is called in MongoDB, by using the drop() method. Delete the "customers" collection: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mycol.drop() The drop() method returns true if the collection was dropped successfully, and false if the collection does not exist.

PYTHON MONGODB DELETE

M ONGODB DELETE Delete Document To delete one document, we use the delete_one() method. The first parameter of the delete_one() method is a query object defining which document to delete. Note: If the query finds more than one document, only the first occurrence is deleted. Delete the document with the address "Mountain 21": import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] myquery = { "address": "Mountain 21" } mycol.delete_one(myquery) Delete Many Documents To delete more than one document, use the delete_many() method. The first parameter of the delete_many() method is a query object defining which documents to delete. Delete all documents were the address starts with the letter S: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol =

PYTHON MONGODB SORT

M ONGODB SORT Sort the Result Use the sort() method to sort the result in ascending or descending order. The sort() method takes one parameter for "fieldname" and one parameter for "direction" (ascending is the default direction). Sort the result alphabetically by name: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mydoc = mycol.find().sort("name") for x in mydoc:   print(x) Sort Descending Use the value -1 as the second parameter to sort descending. sort("name", 1) #ascending sort("name", -1) #descending Sort the result reverse alphabetically by name: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mydoc = mycol.find().sort("name", -1) for x in mydoc:   print(x)

PYTHON MONGODB QUERY

M ONGODB QUERY Filter the Result When finding documents in a collection, you can filter the result by using a query object. The first argument of the find() method is a query object, and is used to limit the search. import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] for x in mycol.find({"address": "California"},{ "_id": 0 }):   print(x) {'name': 'Kartik', 'address': 'California'} Advanced Query To make advanced queries you can use modifiers as values in the query object. E.g. to find the documents where the "address" field starts with the letter "S" or higher (alphabetically), use the greater than modifier: {"$gt": "S"}: Find documents where the address starts with the letter "S" or higher: import pymongo myclient = pymongo.MongoClient(&

PYTHON MONGODB FIND

M ONGODB FIND In MongoDB we use the find() and find_one() methods to find data in a collection. Just like the SELECT statement is used to find data in a table in a MySQL database. Find One To select data from a collection in MongoDB, we can use the find_one() method. The find_one() method returns the first occurrence in the selection. Find the first document in the customers collection: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] x = mycol.find_one() print(x) Find All To select data from a table in MongoDB, we can also use the find() method. The find() method returns all occurrences in the selection. The first parameter of the find() method is a query object. In this example we use an empty query object, which selects all documents in the collection. No parameters in the find() method gives you the same result as SELECT * in MySQL. Return all