tutorial on using Python script to manipulate MongoDB _python

Source: Internet
Author: User
Tags mongoclient mongodb python script

Connecting to a database

Mongoclient VS Connection

Class Mongoclient (pymongo.common.BaseObject)
 | Connection to MongoDB.
 |
 | Method Resolution Order:
 |   Mongoclient
 |   Pymongo.common.BaseObject
 |   __builtin__.object
 |


Class Connection (pymongo.mongo_client. mongoclient)
 | Connection to MongoDB.
 |
 | Method Resolution Order:
 |   Connection
 |   Pymongo.mongo_client. Mongoclient
 |   Pymongo.common.BaseObject
 |   __builtin__.object


From the inheritance of these two classes, connection is inherited by Mongoclient, and it is recommended to use mongoclient instead of connection. (That is, mongoclient can be used with method connection)

From Pymongo import mongoclient
client = mongoclient (' 192.168.40.87 ', 27037)
db_name = ' tcl_useraction '
db = Client[db_name]
collection_useraction = db[' useraction ']

This is a dictionary access to the database and the collection, and you can also through the. (point number) of the way to access
Inserting Data

Save () VS insert ()

Both the Save and insert functions of the MONGODB can insert data into the collection, but there are two differences between them:

The Save function actually invokes the INSERT or update function based on the parameter conditions. If the data object you want to insert exists, the Insert function complains, and the Save function changes the original object, and if the object you want to insert does not exist, So they do the same insert. Here are a few words to sum up the difference between them, that is, the so-called "chance, no, plus."

Insert can be inserted one at a a list, without traversing, efficient, save you need to traverse the list, inserted.

Update data

For a single data, you can update and then use the Save method

Update (criteria, objnew, Upsert, mult)
Criteria: Conditional expressions that need to be updated
Objnew: Updating an expression
Upsert: If the target record does not exist, insert a new document.
Multi: Whether to update multiple documents.

Collection_useraction.update ({' gid ': last_gid, ' time ': L_date}, {' $set ': {' gid ': last_gid}, ' $set ': {' time ': L_date}, ' $ Addtoset ': {' categories ': Category_data}}, Upsert=true)


Delete Data

Db.users.drop () # Delete collection Remove

(self, spec_or_id=none, Safe=none, Multi=true, **kwargs)

# Remove () to delete a single or full document. The deleted document cannot be recovered.
id = db.users.find_one ({"Name": "User2"}) ["_id"]
db.users.remove (ID) # Deletes a record based on ID
db.users.remove () # Delete all records in the collection
db.users.remove ({' yy ': 5}) # Delete yy=5 Records

Inquire

5. Query

  # query for ' u in
  db.users.find ' ({' age ': {' $lt ':}} '): Print U

5.1 Query a record

  # query name equals user8 for
  u in Db.users.find ({' name ': ' User8 '}): Print U
  # gets a
  U2 = Db.users.find_one ({"Name": "User9"}) of the query returns the None print when it is not found
  U2

5.2 Query specific keys (fields)

  # select name, age from users ' where age =
  ' for u ' in Db.users.find ({' Age ':}, [' Name ', ' Age ']): Print u for
  u in Db.users.find (fields = ["Name", "Age"]): Print U

5.3 Sort (sort)

  Pymongo. Ascending # can also be used to replace
  Pymongo. Descending # can also be used-to replace
  the for U in Db.users.find (). Sort ([(' Age ', Pymongo. Ascending)]: Print u  # select * FROM collection name order by key 1 for
  u in Db.users.find (). Sort ([(' Age ', Pymongo. Descending)]: Print u # select * FROM collection name ORDER by key 1 desc
  for u in Db.users.find (). Sort ([("Key 1", Pymongo. Ascending), ("Key 2", Pymongo. Descending)]: Print u # select * FROM collection name ORDER by key 1 ASC, key 2 desc for
  u in db.users.find (sort = [("Key 1", PYMONGO.ASC Ending), ("Key 2", Pymongo. Descending)]: Another way of writing the print U # sort for
  u in Db.users.find ({"Name": "User9"}, sort=[[' name ', 1],[' sex ', 1]], fields = [ ' Name ', ' age ', ' sex ']: print U # combination

5.4 Reading (SLICE) from the first few lines, and how many rows to read (LIMIT)

# SELECT * FROM collection name Skip 2 limit 3
  # MySQL: SELECT * from set name limit 2, 3 for
  u in Db.users.find (). Skip (2). Limi T (3): Print u
  for u in db.users.find (skip = 2, limit = 3): Print u

  # can be sliced instead of skip & limit (the $slice in MONGO seems to have Point of issue).
  for u in Db.users.find () [2:5]: Print u

  # write for
  u in Db.users.find () alone. Skip (2): Print u for
  u in db.users . Find (Skip=1): Print u with
  u in Db.users.find (). Limit (5): Print U for
  u in db.users.find (limit = 3): Print U

More than 5.5 pieces of query (Conditional Operators) # like, you can use regular expression query

 # SELECT * from the users where name = ' User3 ' and ' > ' and age < for
  u in Db.users.find ({' age ': {' $gt ': 12, ' $lt ':}, ' name ': ' User3 '}: Print u
  # SELECT * from users where name = ' user1 ' and '/age = To '
  u in db.users.f IND ({"Age":, "name": "User1"}): Print U

5.6 In

For u in Db.users.find ({"Age": {"$in":(,)}}): Print u  # SELECT * from Users where
  U in Db.users.find ({"Age": {"$nin":(, Num)}): Print u # SELECT * from users where age is not in (23, 26, 32)

5.7 Total Statistics (count)

 Print (Db.users.count ()) # Select COUNT (*) from the Users
  print (Db.users.find ({"Age": {"$GT":}). Count ()) # Select Count (*) from the users where age > 30

5.8 OR

 For u in Db.users.find ({"$or": [{' Age ':}, {' Age ']}]: Print u # select * FROM collection name where key 1 = value 1 or key 1 = value 2 for
  u i N Db.users.find ({"$or": [{' age ': {"$lte":}}, {"Age": {"$gte":}}]): Print u # select * FROM collection name where key 1 <= value 1 or key 1 & gt;= Value 2



6. Existence (exists)

  Db.users.find ({' sex ': {' $exists ': True}}) # select * from collection name where exists key 1
  db.users.find ({' sex ': {' $exists ': False} }) # SELECT * from collection name where NOT EXISTS key 1

7. Regular expression Query

  For u in Db.users.find ({"name": {"$regex": R "(? i) user[135]"}}, ["Name"]): Print u # query out name user1, User3, User5 

8. Element value matching for multilevel paths
Document takes the Json-like hierarchy, so we can use embedding (Embed) directly instead of the association reference (Reference) of the traditional relational database.
MongoDB supports the namespace path of ".", where multiple paths in conditional expressions must be quoted

# If the key contains an array, simply match the array property to see if it contains the element to query the db. Find_one ({' Address ': ' Address1 '}) # address is a number of groups, matching only need to include the # query results such as: {"_id"  : ObjectId ("4c479885089df9b53474170a"), "name": "User1", "Address": ["Address1", "Address2"]} # The multilevel paths in the conditional expression must be enclosed in quotation marks to "." Split u = db. collection name. Find_one ({"IM.QQ: 12345678}) # Query results such as: {" _id ": ObjectId (" 4c479885089df9b53474170a ")," name ":" User1 "," Im ": {" MSN ":" User1@hotmail.com "," QQ ": 12345678}} print u[' im ' [' MSN '] #显示: user1@hotmail.com # Update db for multilevel paths. Collection name . Update ({"IM.QQ": 12345678}, {' $set ': {"im.qq": 12345}}) # Query contains a for u in Db.users.find ({"im.qq": {' $exists ': True}}}, { "Im.qq": 1}): Print U # displays such as: {"_id": ObjectId ("4c479885089df9b53474170a"), "im": {"QQ": 12345}} for u in db.us Ers.find ({' Data ': "ABC"}): Print U # displays as: {"_id": ObjectId ("4c47a481b48cde79c6780df5"), "name": "User8", "data": [{ "A": 1, "B": Ten}, 3, "abc"]} for U in Db.users.find ({' data ': {' $elemMatch ': {' A ': 1, ' B ': {' $gt ': 5}}}}): Print U # display such as: {"_id": ObjectId ("4C47a481b48cde79c6780df5 ")," name ":" User8 "," data ": [{" A ": 1," B ": Ten}, 3," abc "]}

 

{data: "abc"} simply matches whether the array property contains the element. $elemMatch can handle more complex element lookup conditions. Of course, it can be written in the following ways:
Db. Collection name. Find ({"DATA.A": 1, "data.b": {' $gt ': 5}})

Arrays, you can also operate directly by using ordinal numbers:
Db. Collection name. Find ({"Data.1": 3}) # Numbering starts at 0


 # such as a column of the collection
  {"Classifyid": "Test1",
     "keyword": [
        {"name": ' Test1 ', # will modify this value to Test5 (array subscript starting from 0, subscript also with dots)
        " Frequence ":",
        },
        {"name": ' Test2 ', # Child table's query, will match to this value
        "Frequence": A,
        },
     ]
  }
  # child table modification ( The rest of the child table does not change)
  db. Collection name. Update ({"Classifyid": "Test1"}, {"$set": {"keyword.0.name": ' Test5 '}})
  # child table Query
  Db. Collection name. Find ({"Classifyid": "Test1", "Keyword.0.name": "Test2"})



Operation

(1) $all: Determines whether the array property contains all the conditions.

 Db.users.insert ({' name ': ' User3 ', ' Data ': [1,2,3,4,5,6,7]})
  Db.users.insert ({' name ': ' User4 ', ' Data ': [1,2,3]}) For

  u in Db.users.find ({' data ': {' $all ': [2,3,4]}}): Print u
  # display: {"_id": ObjectId ("4c47a133b48cde79c6780df0") , "name": "User3", "Data": [1, 2, 3, 4, 5, 6, 7]}

Notice the difference with $in. $in is to check that the target property value is a member of a conditional expression, and $all requires that the property value contain all the conditional elements.

(2) $size: Matches the number of array property elements.

For u in Db.users.find ({' data ': {' $size ': 3}}}): Print u
  # only displays matching number of this array: {"_id": ObjectId ("4c47a13bb48cde79c6780df1"), ' Name ': ' User4 ', ' Data ': [1, 2, 3]}

(3) $type: Determine the property type.

 For u in Db.users.find ({' t ': {' $type ': 1}}}): Print u # query numeric type for
  u in Db.users.find ({' t ': {' $type ': 2}}): Print u # query character String type of

Type value:
Double:1
String:2
Object:3
Array:4
Binary Data:5
Object Id:7
Boolean:8
Date:9
Null:10
Regular expression:11
JavaScript code:13
Symbol:14
JavaScript code with SCOPE:15
32-bit integer:16
Timestamp:17
64-bit integer:18
Min key:255
Max key:127

(4) $not: Reverse, indicating the return of the condition of the document.
It seems to be used only with the regular and $mod???
# and I don't know how to use it #

(5) $unset: In contrast to $set, indicates the removal of document properties.

  For u in Db.users.find ({' name ': ' User1 '}): Print u
  # displays as: {"_id": ObjectId ("4c479885089df9b53474170a"), "name": "Use R1 ", Age": "Address": ["Address1", "Address2"]}

  db.users.update ({' Name ': "User1"}, {' $unset ': {' address ': 1, ' A GE ': 1}}) for
  u in Db.users.find ({' name ': ' User1 '}): Print u
  # displays as: {"_id": ObjectId ("4c479885089df9b53474170a") , "name": "User1"}

(6) $push: and $ pushall are all adding elements to an array property. # It's no different from the two

 For u in Db.users.find ({' name ': ' User1 '}): Print u
  # displays as: {"_id": ObjectId ("4c479885089df9b53474170a"), "age": 15, " Name ': ' User1 '}

  db.users.update ({' name ': ' User1 '}, {' $push ': {' data ': 1}}}) for
  u in Db.users.find ({' name ': ') User1 "}): Print u
  # displays such as: {" _id ": ObjectId (" 4c479885089df9b53474170a ")," Age ":" Data ": [1]," name ":" User1 " }

  db.users.update ({' name ': ' User1 '}, {' $pushAll ': {' data ': [2,3,4,5]}}) for
  u in Db.users.find ({' Name ': "User1 "}": Print u
  # displays such as: {"_id": ObjectId ("4c479885089df9b53474170a"), "Age": "Data": [1, 2, 3, 4, 5], "name": " User1 "}

(7) $addToSet: Similar to $push, but added only if the element does not exist (set represents a collection of not repeating elements).

Db.users.update ({' name ': ' User2 '}, {' $unset ': {' data ': 1}}})
  db.users.update ({' name ': ' User2 '}, {' $addToSet ': {' Data ': 1}}}
  db.users.update ({' name ': ' User2 '}, {' $addToSet ': {' data ': 1}}}) for
  u in Db.users.find ({' name ': ') User2 "}): Print u
  # display: {" _id ": ObjectId (" 4c479896089df9b53474170b ")," Data ": [1]," name ":" User2 "}

  Db.user S.update ({' name ': ' User2 '}, {' $push ': {' data ': 1}}}) for
  u in Db.users.find ({' name ': ' User2 '}): Print u
  # shows: {"_id ": ObjectId (" 4c479896089df9b53474170b ")," Data ": [1, 1]," name ":" User2 "}

  to add more than one element, use $each.
  db.users.update ({' name ': ' User2 '}, {' $addToSet ': {' data ': {' $each ': [1,2,3,4]}}}) for
  u in Db.users.find ({' Name ': "User2"}): Print u
  # shows: {u ' age ': A, U ' _id ': ObjectId (' 4c479896089df9b53474170b '), U ' data ': [1, 1, 2, 3, 4], u ' Name ': U ' user2 '}
  # doesn't seem to automatically remove duplicates

(8) $each add multiple elements to use.

  Db.users.update ({' name ': ' User2 '}, {' $unset ': {' data ': 1}}}) Db.users.update ({' name ': ' User2 '}, {' $addToSet ': {' data ': 1  For u in Db.users.find ({' name ': ' User2 '}): Print U # display: {"_id": ObjectId ("4c479896089df9b53474170b"), "Data": [ 1], "name": "User2"} db.users.update ({' name ': ' User2 '}, {' $addToSet ': {' data ': {' $each ': [1,2,3,4]}}}) for u in db.us Ers.find ({' name ': ' User2 '}): Print U # shows: {u ' age ': A, U ' _id ': ObjectId (' 4c479896089df9b53474170b '), U ' data ': [1, 2, 3, 4], U ' name ': U ' user2 '} db.users.update ({' name ': ' User2 '}, {' $addToSet ': {' data ': [1,2,3,4]}}) for u in Db.users.find ({' N Ame ': "User2"}): Print U # shows: {"_id": ObjectId ("4c479896089df9b53474170b"), "Data": [1, 2, 3, 4, [1, 2, 3, 4]], " Name ': ' User2 '} db.users.update ({' name ': ' User2 '}, {' $unset ': {' data ': 1}}}) Db.users.update ({' name ': ' User2 '}, {' $addTo Set ': {' data ': [1,2,3,4]}}) for u in Db.users.find ({' name ': ' User2 '}): Print U # shows: {"_id": ObjectId ("4c47a133b48cde79 C6780df0 ")," Data ": [[1, 2, 3, 4]], "name": "User2"}

 

  (9) $pop: The element that removes an array property (removed by the index subscript) $pull is removed by value, $pullAll removes all eligible committed elements.
    

Db.users.update ({' name ': ' User2 '}, {' $unset ': {' data ': 1}}}) Db.users.update ({' name ': ' User2 '}, {' $addToSet ': {' data ': {' ' $each ': [1, 2, 3, 4, 5, 6, 7, 2, 3]}}) for u in Db.users.find ({' name ': ' User2 '}): Print U # display: {"_id": ObjectId ("4  C47a133b48cde79c6780df0 ")," Data ": [1, 2, 3, 4, 5, 6, 7, 2, 3], ' name ': ' User2 '} db.users.update ({' name ': ' User2 '}, {' $pop ': {' data ': 1}}] # Remove the last element for u in Db.users.find ({' name ': ' User2 '}): Print U # display: {"_id": ObjectId ("4c47a133b48cde79c6780df0"), "Da
  Ta ": [1, 2, 3, 4, 5, 6, 7, 2]," name ":" User2 "} db.users.update ({' name ': ' User2 '}, {' $pop ': {' data ':-1}}}) # Remove the first element For u in Db.users.find ({' name ': ' User2 '}): Print U # shows: {"_id": ObjectId ("4c47a133b48cde79c6780df0"), "Data": [2, 3, 4, 5, 6, 7, 2], "name": "User2"} db.users.update ({' name ': ' User2 '}, {' $pull ': {' Data ': 2}}}) # Remove All 2 for u in DB.U Sers.find ({' name ': ' User2 '}): Print U # shows: {"_id": ObjectId ("4c47a133b48cde79c6780df0"), "Data": [3, 4, 5, 6, 7], " Name ":' User2 '} db.users.update ({' name ': ' User2 '}, {' $pullAll ': {' data ': [3,5,6]}}) # Remove 3,5,6 for u in Db.users.find ({' Name ':

 "User2"}): Print U # display: {"_id": ObjectId ("4c47a133b48cde79c6780df0"), "Data": [4, 7], "name": "User2"}

(Ten) $where: Use JS code to replace some ugly $lt, $gt.
MongoDB has a built-in Javascript Engine (SpiderMonkey). can use JS Expression directly, even use JS Function to write more complex Code block.

 Db.users.remove () # deletes all records in the collection for I in range: Db.users.insert ({' Name ': ' User ' + str (i), ' age ': i}) for u in D B.users.find (): Print U # appears as follows: {"_id": ObjectId ("4c47b3372a9b2be866da226e"), "name": "User0", "Age": 0} {"_id ": ObjectId (" 4c47b3372a9b2be866da226f ")," name ":" User1 "," Age ": 1} {" _id ": ObjectId (" 4c47b3372a9b2be866da2270 "), ' Name ': ' User2 ', ' Age ': 2} {' _id ': ObjectId (' 4c47b3372a9b2be866da2271 '), ' name ': ' User3 ', ' Age ': 3} {' _id ': ObjectId ("4c47b3372a9b2be866da2272"), "name": "User4", "Age": 4} {"_id": ObjectId ("4c47b3372a9b2be866da2273"), "Nam E ': ' User5 ', ' Age ': 5} {' _id ': ObjectId (' 4c47b3372a9b2be866da2274 '), ' name ': ' User6 ', ' Age ': 6} {' _id ': obje  Ctid ("4c47b3372a9b2be866da2275"), "name": "User7", "Age": 7} {"_id": ObjectId ("4c47b3372a9b2be866da2276"), "name": ' User8 ', ' Age ': 8} {' _id ': ObjectId (' 4c47b3372a9b2be866da2277 '), ' name ': ' User9 ', ' Age ': 9} for u in Db.users . Find ({"$where": "This.agE > 7 | | This.age < 3 "}": Print U # appears as follows: {u ' age ': 0.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da226e '), U ' name ': U ' User0 '} { U ' age ': 1.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da226f '), U ' name ': U ' user1 '} {u ' age ': 2.0, U ' _id ': ObjectId (' 4c47b3372 a9b2be866da2270 '), U ' name ': U ' user2 '} {u ' age ': 8.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da2276 '), U ' name ': U ' user8 '} { U ' age ': 9.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da2277 '), U ' name ': U ' user9 '} to U in Db.users.find (). where ("This.age > 7 | | This.age < 3 "): Print U # displays as follows: {u ' age ': 0.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da226e '), U ' name ': U ' User0 '} {u ' Age ': 1.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da226f '), U ' name ': U ' user1 '} {u ' age ': 2.0, U ' _id ': ObjectId (' 4c47b3372a 9b2be866da2270 '), U ' name ': U ' user2 '} {u ' age ': 8.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da2276 '), U ' name ': U ' user8 '} {u ' Age ': 9.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da2277 '), U ' name ': U ' user9 '} # using a custom function, javascript syntax for u i N db. Users.find (). where ("function () {return this.age > 7 | |
  This.age < 3; "): Print U # appears as follows: {u ' age ': 0.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da226e '), U ' name ': U ' User0 '}
  {u ' age ': 1.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da226f '), U ' name ': U ' user1 '}
  {u ' age ': 2.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da2270 '), U ' name ': U ' user2 '}
  {u ' age ': 8.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da2276 '), U ' name ': U ' user8 '}

 {u ' age ': 9.0, U ' _id ': ObjectId (' 4c47b3372a9b2be866da2277 '), U ' name ': U ' user9 '}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.