Initial commit
* Examples work * setup.py kinda updasted * Fork of txmongo but with new pymongo embedded
This commit is contained in:
30
examples/aggregate.py
Normal file
30
examples/aggregate.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
from asyncio import coroutine
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
|
||||
@coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
yield from test.insert({"src":"Twitter", "content":"bla bla"}, safe=True)
|
||||
yield from test.insert({"src":"Twitter", "content":"more data"}, safe=True)
|
||||
yield from test.insert({"src":"Wordpress", "content":"blog article 1"}, safe=True)
|
||||
yield from test.insert({"src":"Wordpress", "content":"blog article 2"}, safe=True)
|
||||
yield from test.insert({"src":"Wordpress", "content":"some comments"}, safe=True)
|
||||
|
||||
# Read more about the aggregation pipeline in MongoDB's docs
|
||||
pipeline = [
|
||||
{'$group': {'_id':'$src', 'content_list': {'$push': '$content'} } }
|
||||
]
|
||||
result = yield from test.aggregate(pipeline)
|
||||
|
||||
print("result:", result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
31
examples/dbref.py
Normal file
31
examples/dbref.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
from asyncio_mongo._bson import DBRef
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
doc_a = {"username":"foo", "password":"bar"}
|
||||
result = yield from test.insert(doc_a, safe=True)
|
||||
|
||||
doc_b = {"settings":"foobar", "owner":DBRef("test", result)}
|
||||
yield from test.insert(doc_b, safe=True)
|
||||
|
||||
doc = yield from test.find_one({"settings":"foobar"})
|
||||
print("doc is:", doc)
|
||||
|
||||
if isinstance(doc["owner"], DBRef):
|
||||
ref = doc["owner"]
|
||||
owner = yield from foo[ref.collection].find_one(ref.id)
|
||||
print("owner:", owner)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
18
examples/drop.py
Normal file
18
examples/drop.py
Normal file
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
result = yield from test.drop(safe=True)
|
||||
print(result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
26
examples/group.py
Normal file
26
examples/group.py
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
yield from test.insert({"src":"Twitter", "content":"bla bla"}, safe=True)
|
||||
yield from test.insert({"src":"Twitter", "content":"more data"}, safe=True)
|
||||
yield from test.insert({"src":"Wordpress", "content":"blog article 1"}, safe=True)
|
||||
yield from test.insert({"src":"Wordpress", "content":"blog article 2"}, safe=True)
|
||||
yield from test.insert({"src":"Wordpress", "content":"some comments"}, safe=True)
|
||||
|
||||
result = yield from test.group(keys=["src"],
|
||||
initial={"count":0}, reduce="function(obj,prev){prev.count++;}")
|
||||
|
||||
print("result:", result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
47
examples/index.py
Normal file
47
examples/index.py
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
from asyncio_mongo import filter
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
idx = filter.sort(filter.ASCENDING("something") + filter.DESCENDING("else"))
|
||||
print("IDX:", idx)
|
||||
|
||||
result = yield from test.create_index(idx)
|
||||
print("create_index:", result)
|
||||
|
||||
result = yield from test.index_information()
|
||||
print("index_information:", result)
|
||||
|
||||
result = yield from test.drop_index(idx)
|
||||
print("drop_index:", result)
|
||||
|
||||
# Geohaystack example
|
||||
geoh_idx = filter.sort(filter.GEOHAYSTACK("loc") + filter.ASCENDING("type"))
|
||||
print("IDX:", geoh_idx)
|
||||
result = yield from test.create_index(geoh_idx, **{'bucketSize':1})
|
||||
print("index_information:", result)
|
||||
|
||||
result = yield from test.drop_index(geoh_idx)
|
||||
print("drop_index:", result)
|
||||
|
||||
# 2D geospatial index
|
||||
geo_idx = filter.sort(filter.GEO2D("pos"))
|
||||
print("IDX:", geo_idx)
|
||||
result = yield from test.create_index(geo_idx, **{ 'min':-100, 'max':100 })
|
||||
print("index_information:", result)
|
||||
|
||||
result = yield from test.drop_index(geo_idx)
|
||||
print("drop_index:", result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
21
examples/insert.py
Normal file
21
examples/insert.py
Normal file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
import time
|
||||
import asyncio
|
||||
import asyncio_mongo
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
# insert some data
|
||||
for x in range(10000):
|
||||
result = yield from test.insert({"something":x*time.time()}, safe=True)
|
||||
print(result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
20
examples/query.py
Normal file
20
examples/query.py
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
# fetch some documents
|
||||
docs = yield from test.find(limit=10)
|
||||
for doc in docs:
|
||||
print(doc)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
29
examples/query_fields.py
Normal file
29
examples/query_fields.py
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
import asyncio_mongo.filter
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
# specify the fields to be returned by the query
|
||||
# reference: http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields
|
||||
whitelist = {'_id': 1, 'name': 1}
|
||||
blacklist = {'_id': 0}
|
||||
quickwhite = ['_id', 'name']
|
||||
|
||||
fields = blacklist
|
||||
|
||||
# fetch some documents
|
||||
docs = yield from test.find(limit=10, fields=fields)
|
||||
for n, doc in enumerate(docs):
|
||||
print(n, doc)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
26
examples/query_filter.py
Normal file
26
examples/query_filter.py
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
import asyncio_mongo.filter
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
# create the filter
|
||||
f = asyncio_mongo.filter.sort(asyncio_mongo.filter.DESCENDING("something"))
|
||||
#f += asyncio_mongo.filter.hint(asyncio_mongo.filter.DESCENDING("myindex"))
|
||||
#f += asyncio_mongo.filter.explain()
|
||||
|
||||
# fetch some documents
|
||||
docs = yield from test.find(limit=10, filter=f)
|
||||
for n, doc in enumerate(docs):
|
||||
print(n, doc)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
22
examples/update.py
Normal file
22
examples/update.py
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
import asyncio
|
||||
|
||||
import asyncio_mongo
|
||||
|
||||
@asyncio.coroutine
|
||||
def example():
|
||||
mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)
|
||||
|
||||
foo = mongo.foo # `foo` database
|
||||
test = foo.test # `test` collection
|
||||
|
||||
# insert
|
||||
yield from test.insert({"foo":"bar", "name":"bla"}, safe=True)
|
||||
|
||||
# update
|
||||
result = yield from test.update({"foo":"bar"}, {"$set": {"name":"john doe"}}, safe=True)
|
||||
print("result:", result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(example())
|
||||
Reference in New Issue
Block a user