1. Establish the connection
#encoding =utf8from Pymongo Import mongoclienturl= ' mongodb://user_name:[email protected]_ip[:p ort]/datebase ' # Administrator to access the admin database, due to MongoDB authentication mode, other databases may not have permission! Con=mongoclient (URL)
2. Select DB and Connect collection
db=con[' Datebase ']user=db.user #collection命名若有非法字符 ('-' etc), Error! user=db[' user ']
3. Query "Find or Find_one"
User.find ({' key ': value}) #返回一个迭代器user. Find_one ({' key ': value}) #返回一个字典, match only one matching record
4. Complex query
Db.order.find ({' created_time ': {' $gte ': startTime, ' $lte ': endTime}, ' status ': {' $in ': status_paid}},{' items ': 1, ' _id ' : 0}) queries a time period, status in the Status_paid list, and results as long as the key--value of items
Experience sharing
Pymongo is the Python library, the MongoDB shell is JavaScript, some syntax is different (such as sorting)
Native install MongoDB, command line input MONGO, into JavaScript shell
Basic operation of the database: adding and deleting changes
Note the timestamp, the time stamp of the JavaScript is "actual Time *1000"
Kanji can be entered directly, encoded as Unicode, output (print or write) to be converted to UTF8 "name.encode (' UTF8 ')"
This article from the "Internet of Things" blog, reproduced please contact the author!
Extracting tips for MongoDB data with Pymongo