je mag pyMongo
. gebruiken zoals onderstaande code
from pymongo import MongoClient
class MongoConnection(object):
def __init__(self):
client = MongoClient('localhost', 27017)
self.db = client['database_name']
def get_collection(self, name):
self.collection = self.db[name]
we creëren een verbinding volgens onze behoefte.
class MyCollection(MongoConnection):
def __init__(self):
super(MyCollection, self).__init__()
self.get_collection('collection_name')
def update_and_save(self, obj):
if self.collection.find({'id': obj.id}).count():
self.collection.update({ "id": obj.id},{'id':123,'name':'test'})
else:
self.collection.insert_one({'id':123,'name':'test'})
def remove(self, obj):
if self.collection.find({'id': obj.id}).count():
self.collection.delete_one({ "id": obj.id})
Nu hoef je alleen maar te bellen zoals hieronder.
my_col_obj = MyCollection()
obj = Mymodel.objects.first()
my_col_obj.update_and_save(obj)
my_col_obj.remove(obj)