Python operations on mongodb

Source: Internet
Author: User
Importpymongo, sysfrompy?import=clientclient=client (host & amp; quot; localhost: 27017 & amp; quot;, role) dbclient. database... import pymongo, sys
From pymongo import MongoClient
Client = paiclient (host = "localhost: 27017", read_preference = pymongo. read_preferences.ReadPreference.PRIMARY_PREFERRED)
Db = client. database
Db. authenticate ("database", "passwd ")

1. search
Pi = db. locinfo. find ({"con": p}) # search
Pi = db. locinfo. find_one ({"con": p })
Print pi
{U'city': u' \ u56fe \ u6728 \ u8212 \ u514b \ u5e02 ', u'wid': u'000000', u'zip': u'000000 ', u'pro': u' \ u65b0 \ u7586 \ u7ef4 \ u543e \ u5c14 \ u81ea \ u6cbb \ u533a', u 'wkey ': u' \ u56fe \ u6728 \ u8212 \ u514b \ u5e02 ', u'acode': u'000000', u' _ id': u'txpcx5', u'fix ': true, u'con ': u' \ u56fe \ u6728 \ u8212 \ u514b \ u5e02 '}


2. update
Db. locinfo. update ({"con": p}, {'$ set': {"con": zc}) # update one item
×××××××××××××× Batch update ××× multi = True ×××××××××××××××××××××× *
Db. locinfo. update ({"con": lon}, {'$ set': {"wkey": sht }}, multi = True) # OK, # update multiple items
××××××××××××××××××××××××××××××××××××××××××× *

3. Insert # insert an array and general character seek
Db. wbinfo. insert ({"city": cityname, "jiaojing": [jiaojing], "lukuang": "lukuang ,"})

4. Insert arrays by push and pop to delete array elements
Db. wbinfo. update ({"city": "LU 'an city" },{ '$ push': {"jiaojing": "two", "lukuang": "lk "}})
Db. wbinfo. update ({"city": "LU 'an city" },{ '$ pop': {"jiaojing": "two", "lukuang": "lk "}})

5. Regular expression query
Res = db. devices. find ({'$ and': [{'stats. devcaps ': {' $ exists': True }}, {'stats. fwver ': {' $ regex ':'. * N91 | N92. * '}]}) # stats. the fwver field contains the character 'n91 'or 'n92'

6. dictionary
If devcap. has_key ('dev') and devcap. has_key ('Cap '):

7.
Python code used to query data by _ id in mongodb
ObjectId pymongo python
If the Python version is earlier than 2.2, use the following statement to import ObjectId.
From pymongo. objectid import ObjectId

If the version number of pymongo is greater than 2.2, use the following statement.
From bson. objectid import ObjectId
The query code is as follows:
Collection. find_one ({'_ id': ObjectId ('50f0d76109f4ec148890ef1e ')})

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.