In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail the method of Python calling MongoDB for you. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Use pymongo to do various operations on MongoDB, download the version of the corresponding platform, and decompress it. For ease of use, add the bin path to the system path environment variable. Where mongod is the server, mongo is the customer shell, and then create the data file directory: create the data folder under disk c, and create the db folder in it.
How does Python call MongoDB?
Install the Driver,Python installation pymongo for the corresponding language
$easy_installpymongo
Summary of usage, excerpted from the official tutorial
Create a connection
> importpymongo > connection=pymongo.Connection ('localhost',27017)
Switch database
> db=connection.test_database
Get collection
> collection=db.test_collection
Both db and collection are created with a delay, and are actually created when the Document is added
Document addition, _ id automatically created
> importdatetime > post= {"author": "Mike",... "text": "Myfirstblogpost!",... "tags": ["mongodb", "python", "pymongo"],... "date": datetime.datetime.utcnow ()} > > posts=db.posts > posts.insert (post) ObjectId ('...')
Batch insert
> > new_posts= [{"author": "Mike",... "text": "Anotherpost!",... "tags": ["bulk", "insert"],... "date": datetime.datetime (2009 author: "Eliot",... "title": "MongoDBisfun",... "text": "andprettyeasytoo!" "date": datetime.datetime (2009 posts.insert 11, 10 10, 45)} > posts.insert (new_posts) [ObjectId ('...'), ObjectId ('...')]
Get all collection (equivalent to SQL's showtables)
> db.collection_names () [upright postswriting journal upright system. Indexes']
Get a single document
> > posts.find_one () {u'date':datetime.datetime (...), ubiquitous textbook misture upristblogpostural recording, ubiquitous authoritative mixing, ubiquitous tagswriting: [upright monotonous database purveyingutilisation pythonology
Query multiple documents
> forpostinposts.find ():... post (...), upright textbook purge ubiquitous Myfirstblogpostcards, ubiquitous textbook authorizers, ubiquitous mikeboxes, upright tagsmakers: {u'date':datetime.datetime (2009 paramons 11, 12, 11, 14), upright textbook authorizations, utensils, etc. U'_id':ObjectId ('...'), ubiquitous authorship charge upright tags: [upright bulkweights relegated to upright insert`]} {u'date':datetime.datetime (2009, 11, 10, 10, and 10) 45), upright textbook, purge, and prettyeasytooothers, upright textbook, utensils, utilisation, utopia, etc.), upright authorship authorship, utopia, etc.
Conditional query
> > posts.find_one ({"author": "Mike"})
Advanced query
> posts.find ({"date": {"$lt": d}}) .sort ("author")
Statistical quantity
> posts.count () 3
Indexing
> frompymongoimportASCENDING,DESCENDING > posts.create_index ([("date", DESCENDING), ("author", ASCENDING)])
View the performance of query statements
> > posts.find ({"date": {"$lt": d}}). Sort ("author"). Explain () ["cursor"] upright BtreeCursordated copyright 1 author 1'> posts.find ({"date": {"$lt": d}}) .sort ("author"). Explain () ["nscanned"] 2
Attach my own summary and be careful, for reference only.
Shortcoming
Not completely replace the traditional database (NoSQLFan: whether it can be replaced by the application scenario)
Complex transactions are not supported (NoSQLFan:MongoDB only supports atomic operations on a single document)
The whole tree in the document is not easy to search, 4MB limit? (the NoSQLFan:1.8 version has been modified to 16m)
Features (NoSQLFan: many of the features listed by the author here are just superficial features):
Document database, table structure can be embedded
No pattern, avoid empty field overhead (SchemaFree)
Distributed support
Query supports regularity
Dynamic extension architecture
The 32-bit version can only store up to 2.5GB data (NoSQLFan: the maximum file size is 2G, 64-bit is recommended for production environments)
This is the end of this article on "the method of Python calling MongoDB". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.