Size: 512
Comment:
|
← Revision 13 as of 2025-02-06 16:01:41 ⇥
Size: 2497
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
<<TableOfContents(2)>> ## page was renamed from pymongo |
|
Line 7: | Line 9: |
* cd /tmp * git clone git://github.com/mongodb/mongo-python-driver.git pymongo * cd pymongo/ * su * python setup.py install |
{{{#!highlight sh cd /tmp git clone git://github.com/mongodb/mongo-python-driver.git pymongo cd pymongo/ su python setup.py install }}} |
Line 22: | Line 26: |
== Queries == === Query by _id === {{{#!highlight python from bson import ObjectId import pymongo import datetime mongo = pymongo.Connection("mongodb://192.168.1.1:27017/?slaveok=true") #mongo = pymongo.Connection('192.168.1.1', 27017) dbx = mongo['dbx'] collx = dbx['collx'] nowx = datetime.datetime.now(); currDay = datetime.datetime(nowx.year,nowx.month,nowx.day,0,0,0,0) query={'_id':ObjectId(objx.getId()) } print query res = collx.find_one(query ) print res }}} == Driver windows Python 2.5 == https://pypi.python.org/packages/2.5/p/pymongo/pymongo-2.6.3.win32-py2.5.exe == datetime.datetime is not JSON serializable == {{{#!highlight python from bson import json_util import json json.dumps(anObject, default=json_util.default) }}} == Array of objects in pymongo without cursor == {{{#!highlight python import pymongo resList= list(collx.find({'keyx':1}) ) }}} == Query with regular expression == http://docs.mongodb.org/manual/reference/operator/query/regex/ {{{#!highlight python import pymongo db.collection.find( { field: /acme.*corp/i } ); db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } ); ############################################### import pymongo mongo = pymongo.Connection("mongodb://10.0.0.130:27017/?slaveok=true") dbx = mongo['dbx'] collx = dbx['collx'] res=list(collx.find({'type':'typex','sms':{'$regex':'param'}}).sort('_id',1)) }}} == pymongo 3.4.0 example == {{{#!highlight python import pymongo mongouri='mongodb://127.0.0.1:27017/?slaveok=true' print('pymongo version %s '%( pymongo.__version__) ) # 3.4.0 mongo = pymongo.MongoClient(mongouri) db = mongo.test print('Database name: %s '%( db.name )) print('Collections:') for col in db.collection_names(): print(' %s'%(col)) resList= list(db.collx.find({}) ) for item in resList: print( item ) }}} |
Contents
pymongo
PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python
http://api.mongodb.org/python/current/
Install from source on Slackware
Eclipse - pydev
- Window
- Preferences
- pydev
- interpreter - python
- Forced builtins
- new
- pymongo
- apply Ok
Queries
Query by _id
1 from bson import ObjectId
2 import pymongo
3 import datetime
4 mongo = pymongo.Connection("mongodb://192.168.1.1:27017/?slaveok=true")
5 #mongo = pymongo.Connection('192.168.1.1', 27017)
6 dbx = mongo['dbx']
7 collx = dbx['collx']
8
9 nowx = datetime.datetime.now();
10 currDay = datetime.datetime(nowx.year,nowx.month,nowx.day,0,0,0,0)
11
12 query={'_id':ObjectId(objx.getId()) }
13 print query
14 res = collx.find_one(query )
15 print res
Driver windows Python 2.5
https://pypi.python.org/packages/2.5/p/pymongo/pymongo-2.6.3.win32-py2.5.exe
datetime.datetime is not JSON serializable
Array of objects in pymongo without cursor
Query with regular expression
http://docs.mongodb.org/manual/reference/operator/query/regex/
1 import pymongo
2 db.collection.find( { field: /acme.*corp/i } );
3 db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } );
4 ###############################################
5
6 import pymongo
7
8 mongo = pymongo.Connection("mongodb://10.0.0.130:27017/?slaveok=true")
9 dbx = mongo['dbx']
10 collx = dbx['collx']
11 res=list(collx.find({'type':'typex','sms':{'$regex':'param'}}).sort('_id',1))
pymongo 3.4.0 example
1 import pymongo
2 mongouri='mongodb://127.0.0.1:27017/?slaveok=true'
3 print('pymongo version %s '%( pymongo.__version__) ) # 3.4.0
4 mongo = pymongo.MongoClient(mongouri)
5 db = mongo.test
6
7 print('Database name: %s '%( db.name ))
8
9 print('Collections:')
10 for col in db.collection_names():
11 print(' %s'%(col))
12
13 resList= list(db.collx.find({}) )
14 for item in resList:
15 print( item )