def create_index(self, sort_fields, **kwargs): def wrapper(result, name): return name if not isinstance(sort_fields, qf.sort): raise TypeError("sort_fields must be an instance of filter.sort") if "name" not in kwargs: name = self._gen_index_name(sort_fields["orderby"]) else: name = kwargs.pop("name") key = SON() for k,v in sort_fields["orderby"]: key.update({k:v}) index = SON(dict( ns=str(self), name=name, key=key )) if "drop_dups" in kwargs: kwargs["dropDups"] = kwargs.pop("drop_dups") if "bucket_size" in kwargs: kwargs["bucketSize"] = kwargs.pop("bucket_size") index.update(kwargs) d = self._database.system.indexes.insert(index, safe=True) d.addCallback(wrapper, name) return d
def find_and_modify(self, query={}, update=None, upsert=False, **kwargs): def wrapper(result): no_obj_error = "No matching object found" if not result['ok']: if result["errmsg"] == no_obj_error: return None else: raise ValueError("Unexpected Error: %s" % (result,)) return result.get('value') if (not update and not kwargs.get('remove', None)): raise ValueError("Must either update or remove") if (update and kwargs.get('remove', None)): raise ValueError("Can't do both update and remove") cmd = SON([("findAndModify", self._collection_name)]) cmd.update(kwargs) # No need to include empty args if query: cmd['query'] = query if update: cmd['update'] = update if upsert: cmd['upsert'] = upsert no_obj_error = "No matching object found" d = self._database["$cmd"].find_one(cmd) d.addCallback(wrapper) return d
def find_and_modify(self, query={}, update=None, upsert=False, **kwargs): def wrapper(result): no_obj_error = "No matching object found" if not result['ok']: if result["errmsg"] == no_obj_error: return None else: raise ValueError("Unexpected Error: %s" % (result, )) return result.get('value') if (not update and not kwargs.get('remove', None)): raise ValueError("Must either update or remove") if (update and kwargs.get('remove', None)): raise ValueError("Can't do both update and remove") cmd = SON([("findAndModify", self._collection_name)]) cmd.update(kwargs) # No need to include empty args if query: cmd['query'] = query if update: cmd['update'] = update if upsert: cmd['upsert'] = upsert no_obj_error = "No matching object found" d = self._database["$cmd"].find_one(cmd) d.addCallback(wrapper) return d
def create_index(self, sort_fields, **kwargs): def wrapper(result, name): return name if not isinstance(sort_fields, qf.sort): raise TypeError("sort_fields must be an instance of filter.sort") if "name" not in kwargs: name = self._gen_index_name(sort_fields["orderby"]) else: name = kwargs.pop("name") key = SON() for k,v in sort_fields["orderby"]: key.update({k:v}) index = SON(dict( ns=str(self), name=name, key=key )) if "drop_dups" in kwargs: kwargs["dropDups"] = kwargs.pop("drop_dups") if "bucket_size" in kwargs: kwargs["bucketSize"] = kwargs.pop("bucket_size") index.update(kwargs) d = self._database.system.indexes.insert(index, safe=True) d.addCallback(wrapper, name) return d
def findAndModify(self, query={}, update=None, upsert=False, **kwargs): '''@see: http://api.mongodb.org/python/1.10.1%2B/api/pymongo/collection.html#pymongo.collection.Collection.find_and_modify''' cmd = SON([("findAndModify", self._collection_name), ("query", query), ("update", update), ("upsert", upsert), ]) cmd.update(**kwargs) d = self._database["$cmd"].find_one(cmd) return d
def runCommand(self, command, value=1, **kwargs): ''' @see: http://www.mongodb.org/display/DOCS/Commands @deprecated: use database.command() instead, as in pymongo http://api.mongodb.org/python/1.10.1%2B/api/pymongo/database.html#pymongo.database.Database.command ''' warnings.warn("collection.runCommand: use database.command() instead", DeprecationWarning) cmd = SON([ (command, value) ]) cmd.update(**kwargs) d = self._database["$cmd"].find_one(cmd) return d
def map_reduce(self, map, reduce, full_response=False, **kwargs): def wrapper(result, full_response): if full_response: return result return result.get("result") cmd = SON([("mapreduce", self._collection_name), ("map", map), ("reduce", reduce)]) cmd.update(**kwargs) d = self._database["$cmd"].find_one(cmd) d.addCallback(wrapper, full_response) return d
def command(self, command, value=1, **kwargs): ''' @see: http://www.mongodb.org/display/DOCS/Commands @see: http://api.mongodb.org/python/1.10.1%2B/api/pymongo/database.html#pymongo.database.Database.command ''' if isinstance(command, basestring): command = SON([(command, value)]) command.update(kwargs) d = self["$cmd"].find_one(command) return d
def aggregate(self, pipeline): if not isinstance(pipeline, (dict, list, tuple)): raise TypeError("pipeline must be a dict, list or tuple") if isinstance(pipeline, dict): pipeline = [pipeline] command = SON([("aggregate", self._collection_name)]) command.update({"pipeline": pipeline}) return self._database["$cmd"].find_one(command)
def findAndModify(self, query={}, update=None, upsert=False, **kwargs): '''@see: http://api.mongodb.org/python/1.10.1%2B/api/pymongo/collection.html#pymongo.collection.Collection.find_and_modify''' cmd = SON([ ("findAndModify", self._collection_name), ("query", query), ("update", update), ("upsert", upsert), ]) cmd.update(**kwargs) d = self._database["$cmd"].find_one(cmd) return d
def map_reduce(self, map, reduce, full_response=False, **kwargs): def wrapper(result, full_response): if full_response: return result return result.get("result") cmd = SON([("mapreduce", self._collection_name), ("map", map), ("reduce", reduce)]) cmd.update(**kwargs) d = self._database["$cmd"].find_one(cmd) d.addCallback(wrapper, full_response) return d
def runCommand(self, command, value=1, **kwargs): ''' @see: http://www.mongodb.org/display/DOCS/Commands @deprecated: use database.command() instead, as in pymongo http://api.mongodb.org/python/1.10.1%2B/api/pymongo/database.html#pymongo.database.Database.command ''' warnings.warn("collection.runCommand: use database.command() instead", DeprecationWarning) cmd = SON([(command, value)]) cmd.update(**kwargs) d = self._database["$cmd"].find_one(cmd) return d
def create_collection(self, name, options={}): def wrapper(result, deferred, collection): if result.get("ok", 0.0): deferred.callback(collection) else: deferred.errback(RuntimeError(result.get("errmsg", "unknown error"))) deferred = defer.Deferred() collection = Collection(self, name) if options: if "size" in options: options["size"] = float(options["size"]) command = SON({"create": name}) command.update(options) d = self["$cmd"].find_one(command) d.addCallback(wrapper, deferred, collection) else: deferred.callback(collection) return deferred
def create_collection(self, name, options={}): def wrapper(result, deferred, collection): if result.get("ok", 0.0): deferred.callback(collection) else: deferred.errback(RuntimeError(result.get("errmsg", "unknown error"))) deferred = defer.Deferred() collection = Collection(self, name) if options: if "size" in options: options["size"] = float(options["size"]) command = SON({"create": name}) command.update(options) d = self["$cmd"].find_one(command) d.addCallback(wrapper, deferred, collection) else: deferred.callback(collection) return deferred
def runCommand(self, command, value, **kwargs): '''@see: http://www.mongodb.org/display/DOCS/Commands''' cmd = SON([ (command, value) ]) cmd.update(**kwargs) d = self._database["$cmd"].find_one(cmd) return d