def put_documentesribyid(id): data = request.body.readline() result = json.loads(data) print data entity = db['documents'].find_one({'_id':id}) if not entity: entity = result tmp = {} tmp["features"] = [entity] tmp["geometryType"] = entity["geometryType"] geojs = esri_to_geo(tmp) geojs["_id"] = entity["_id"] near_dict = {"$near": geojs["geometry"]["coordinates"]} max_dict = max_dict = {"$maxDistance": 0.001} q = SON(near_dict) q.update(max_dict) gq = {"geometry.coordinates": q} check = db["documents"].find(gq) print check if check: abort(400, {"success": False, "error": "Already exist"}) else: geojs = entity geojs["properties"]["votes"] = result["attributes"]["votes"] try: db['documents'].save(geojs) except ValidationError as ve: abort(400, str(ve)) return result
def map_reduce(self, map, reduce, full_response=False, **kwargs): """Perform a map/reduce operation on this collection. If `full_response` is ``False`` (default) returns a :class:`~pymongo.collection.Collection` instance containing the results of the operation. Otherwise, returns the full response from the server to the `map reduce command`_. :Parameters: - `map`: map function (as a JavaScript string) - `reduce`: reduce function (as a JavaScript string) - `full_response` (optional): if ``True``, return full response to this command - otherwise just return the result collection - `**kwargs` (optional): additional arguments to the `map reduce command`_ may be passed as keyword arguments to this helper method, e.g.:: >>> db.test.map_reduce(map, reduce, limit=2) .. note:: Requires server version **>= 1.1.1** .. seealso:: :doc:`/examples/map_reduce` .. versionadded:: 1.2 .. _map reduce command: http://www.mongodb.org/display/DOCS/MapReduce """ command = SON([("mapreduce", self.__name), ("map", map), ("reduce", reduce)]) command.update(**kwargs) response = self.__database.command(command) if full_response: return response return self.__database[response["result"]]
def check_for_pos_dupes_via_geoloc(attrs, collection_name=None): if attrs['geometry_type'] == "Point": ll = { '$near': [ float(attrs['geometry_centroid'][0]), float(attrs['geometry_centroid'][1]) ] } cc = ClassifierCategories.objects.get( slug=attrs['classifiers']['category']) md = {'$maxDistance': int(cc.duplicate_distance_tolerance)} q = SON(ll) q.update(md) gq = { 'geometry_centroid': q, 'classifiers.category': attrs['classifiers']['category'] } x = query_mongo_db(gq, collection_name=collection_name) if x.has_key('features'): if len(x['features']) > 0: attrs['possible_duplicate'] = True return attrs
def transform_incoming(self, son, collection): """Move _id to the front if it's there. """ if not "_id" in son: return son transformed = SON({"_id": son["_id"]}) transformed.update(son) return transformed
def as_doc(self): """Get the SON document representation of this DBRef. Generally not needed by application developers """ doc = SON([("$ref", self.collection), ("$id", self.id)]) if self.database is not None: doc["$db"] = self.database doc.update(self.__kwargs) return doc
def getDuplicateCandidates(self, entity): if not 'lat' in entity or not 'lng' in entity: raise Fail('invalid entity') # TODO: verify lat / lng versus lng / lat q = SON({"$near" : [entity.lng, entity.lat]}) q.update({"$maxDistance" : self.distance }) docs = self._placesDB._collection.find({"coordinates" : q}, output=list) entities = self._gen_entities(docs) return entities
def __create(self, options): """Sends a create command with the given options. """ # Send size as a float, not an int/long. BSON can only handle 32-bit # ints which conflicts w/ max collection size of 10000000000. if "size" in options: options["size"] = float(options["size"]) command = SON({"create": self.__name}) command.update(options) self.__database.command(command)
def get_features_near_point(request, lat, lon, max_distance=None, limit=None): """ Return a geographic features located neat these coordinates. """ ll = {'$near':[float(lat), float(lon)]} if max_distance: md={'$maxDistance': max_distance} q=SON(ll) q.update(md) return get_document_by(request, limit=limit, search_in=('url', 'get'), geometry_coordinates=q, ) else: return get_document_by(request, limit=limit, search_in=('url', 'get'), geometry_coordinates=ll, )
def check_for_pos_dupes_via_geoloc(attrs, collection_name=None): if attrs['geometry_type']=="Point": ll = {'$near':[float(attrs['geometry_centroid'][0]), float(attrs['geometry_centroid'][1] )]} cc = ClassifierCategories.objects.get(slug=attrs['classifiers']['category']) md={'$maxDistance': int(cc.duplicate_distance_tolerance)} q=SON(ll) q.update(md) gq={'geometry_centroid': q, 'classifiers.category': attrs['classifiers']['category']} x=query_mongo_db(gq, collection_name=collection_name) if x.has_key('features'): if len(x['features'])>0: attrs['possible_duplicate']=True return attrs
def map_reduce(self, map, reduce, full_response=False, **kwargs): """Perform a map/reduce operation on this collection. If `full_response` is ``False`` (default) returns a :class:`~pymongo.collection.Collection` instance containing the results of the operation. Otherwise, returns the full response from the server to the `map reduce command`_. :Parameters: - `map`: map function (as a JavaScript string) - `reduce`: reduce function (as a JavaScript string) - `full_response` (optional): if ``True``, return full response to this command - otherwise just return the result collection - `**kwargs` (optional): additional arguments to the `map reduce command`_ may be passed as keyword arguments to this helper method, e.g.:: >>> db.test.map_reduce(map, reduce, limit=2) .. note:: Requires server version **>= 1.1.1** .. seealso:: :doc:`/examples/map_reduce` .. versionadded:: 1.2 .. _map reduce command: http://www.mongodb.org/display/DOCS/MapReduce .. mongodoc:: mapreduce """ command = SON([("mapreduce", self.__name), ("map", map), ("reduce", reduce)]) command.update(**kwargs) response = self.__database.command(command) if full_response: return response return self.__database[response["result"]]
def command(self, command, value=1, check=True, allowable_errors=[], _sock=None, **kwargs): """Issue a MongoDB command. Send command `command` to the database and return the response. If `command` is an instance of :class:`basestring` then the command {`command`: `value`} will be sent. Otherwise, `command` must be an instance of :class:`dict` and will be sent as is. Any additional keyword arguments will be added to the final command document before it is sent. For example, a command like ``{buildinfo: 1}`` can be sent using: >>> db.command("buildinfo") For a command where the value matters, like ``{collstats: collection_name}`` we can do: >>> db.command("collstats", collection_name) For commands that take additional arguments we can use kwargs. So ``{filemd5: object_id, root: file_root}`` becomes: >>> db.command("filemd5", object_id, root=file_root) :Parameters: - `command`: document representing the command to be issued, or the name of the command (for simple commands only). .. note:: the order of keys in the `command` document is significant (the "verb" must come first), so commands which require multiple keys (e.g. `findandmodify`) should use an instance of :class:`~pymongo.son.SON` or a string and kwargs instead of a Python `dict`. - `value` (optional): value to use for the command verb when `command` is passed as a string - `check` (optional): check the response for errors, raising :class:`~pymongo.errors.OperationFailure` if there are any - `allowable_errors`: if `check` is ``True``, error messages in this list will be ignored by error-checking - `**kwargs` (optional): additional keyword arguments will be added to the command document before it is sent .. versionchanged:: 1.6 Added the `value` argument for string commands, and keyword arguments for additional command options. .. versionchanged:: 1.5 `command` can be a string in addition to a full document. .. versionadded:: 1.4 .. mongodoc:: commands """ if isinstance(command, basestring): command = SON([(command, value)]) command.update(kwargs) result = self["$cmd"].find_one(command, _sock=_sock, _must_use_master=True, _is_command=True) if check and result["ok"] != 1: if result["errmsg"] in allowable_errors: return result raise OperationFailure("command %r failed: %s" % (command, result["errmsg"])) return result
def __last_error(args): """Data to send to do a lastError. """ cmd = SON([("getlasterror", 1)]) cmd.update(args) return query(0, "admin.$cmd", 0, -1, cmd)