Due to the natural mistrust of MySQL (don't know), ready to try something new, Redis is a memory database some understanding, this time try to have more storage feeling of MongoDB
From Pymongo import Connection
con = Connection ()
Connection = Pymongo. Connection (' localhost ', 27017)
db = con.test # database name is test
Posts = Db.post
2 >>> post1 = {"title": "I Love Python",
3 "slug": "I-love-python",
4 "Author": "Serho",
5 "Content": "I lovepython ...",
6 "tags": ["Love", "Python"],
7 "Time":d Atetime.datetime.now ()}
A record is a {} dic with various keys, corresponding value can be a function value
Posts.insert (POST1)
Keep records in the database
Post = Posts.find ()
Find All records
Post.count ()
Get all of the records
Posts.find_one ({"slug": "python-mongodb"})
Find a record
Posts.update ({"_id":p ost["_id"]},post)
Update a record post
1 >>> posts.update ({"_id":p ost["_id"]},{"$set":
2 {"Content": "Testupdate SET ..."})
Update the value of a Key-value pair in a record with a $set
>>> posts.update ({"_id":p ost["_id"]},{"$inc": {"views": 1}})
$inc to the value of the views in the record plus 1, if there is no views this key is created, and assigned a value of 1?
>>> posts.update ({"_id":p ost["_id"]},{"$push": {"tags": "Test"}})
Append a value to the value of an array type
2 >>> posts.update ({"_id":p ost["_id"]},{"$addToSet":
3 {"tags": {"$each": ["Python", "Each"]}})
Append a series of values to the value of an array type, while ensuring that no duplicate values are available
1 >>> posts.update ({"_id":p ost["_id"]},{"$pop": {"tags": 1}})
The value of the array type pops out at the end of a value, if {"$pop": {"Tags":-1}} then pops out the first
You can use "$pull" to delete the value specified in the array, which deletes all matching values in the array. How do I change one of these values? Can be deleted first, and then add a go in, there is a direct location modification. For example tags array, "Python" is the first, want to change it to "Python", can be directly selected by subscript, that is tags[0], and then use the above "$set" and other modifiers, if not sure you can use $ to locate:
1 >>> posts.update ({"tags": "MongoDB"},{"$set": {"tags.$": "Hello"}})
This will first search tags to meet the "MongoDB", if found, modify it to "Hello". Can see the above update this function has two parameters, it also has a 3rd parameter Upsert, if set to "True", if no matching document is found, the match will be based on a new document, the actual instance will not speak.
BULK INSERT
New_posts =[{"name": "A.payment.fakeinst.a", "Family": "Fakeinst", "category": "Malicious deduction fee", "behavior": "Background send charge SMS"},{"name" : "A.payment.umeng.a", "Family": "Umeng", "category": "Malicious deduction fee", "Behavior": "1. Backstage to get instructions from the server side, automatically send SMS, custom charge service, and intercept the specified number of SMS. 2. Background get instructions from the server, automatically simulate access to ads, consumption of user traffic "}"
Malinfo.insert (new_posts)
OUT[13]: [ObjectId (' 527281323387e31671aa91b2 '), ObjectId (' 527281323387e31671aa91b3 ')]
Python Operation MongoDB