Added server-side js docs
This commit is contained in:
		| @@ -232,6 +232,109 @@ calling it with keyword arguments:: | ||||
|    natively supported by MongoDB -- they are compiled to Javascript and sent | ||||
|    to the server for execution. | ||||
|  | ||||
| Server-side javascript execution | ||||
| ================================ | ||||
| Javascript functions may be written and sent to the server for execution. The | ||||
| result of this is the return value of the Javascript function. This | ||||
| functionality is accessed through the | ||||
| :meth:`~mongoengine.queryset.QuerySet.exec_js` method on | ||||
| :meth:`~mongoengine.queryset.QuerySet` objects. Pass in a string containing a | ||||
| Javascript function as the first argument. | ||||
|  | ||||
| The remaining positional arguments are names of fields that will be passed into | ||||
| you Javascript function as its arguments. This allows functions to be written | ||||
| that may be executed on any field in a collection (e.g. the | ||||
| :meth:`~mongoengine.queryset.QuerySet.sum` method, which accepts the name of | ||||
| the field to sum over as its argument). Note that field names passed in in this | ||||
| manner are automatically translated to the names used on the database (set | ||||
| using the :attr:`name` keyword argument to a field constructor). | ||||
|  | ||||
| Keyword arguments to :meth:`~mongoengine.queryset.QuerySet.exec_js` are | ||||
| combined into an object called :attr:`options`, which is available in the | ||||
| Javascript function. This may be used for defining specific parameters for your | ||||
| function. | ||||
|  | ||||
| Some variables are made available in the scope of the Javascript function: | ||||
|  | ||||
| * ``collection`` -- the name of the collection that corresponds to the | ||||
|   :class:`~mongoengine.Document` class that is being used; this should be | ||||
|   used to get the :class:`Collection` object from :attr:`db` in Javascript | ||||
|   code | ||||
| * ``query`` -- the query that has been generated by the | ||||
|   :class:`~mongoengine.queryset.QuerySet` object; this may be passed into | ||||
|   the :meth:`find` method on a :class:`Collection` object in the Javascript | ||||
|   function | ||||
| * ``options`` -- an object containing the keyword arguments passed into | ||||
|   :meth:`~mongoengine.queryset.QuerySet.exec_js` | ||||
|  | ||||
| The following example demonstrates the intended usage of | ||||
| :meth:`~mongoengine.queryset.QuerySet.exec_js` by defining a function that sums | ||||
| over a field on a document (this functionality is already available throught | ||||
| :meth:`~mongoengine.queryset.QuerySet.sum` but is shown here for sake of | ||||
| example):: | ||||
|  | ||||
|     def sum_field(document, field_name, include_negatives=True): | ||||
|         code = """ | ||||
|         function(sumField) { | ||||
|             var total = 0.0; | ||||
|             db[collection].find(query).forEach(function(doc) { | ||||
|                 var val = doc[sumField]; | ||||
|                 if (val >= 0.0 || options.includeNegatives) { | ||||
|                     total += val; | ||||
|                 } | ||||
|             }); | ||||
|             return total; | ||||
|         } | ||||
|         """ | ||||
|         options = {'includeNegatives': include_negatives} | ||||
|         return document.objects.exec_js(code, field_name, **options) | ||||
|  | ||||
| As fields in MongoEngine may use different names in the database (set using the | ||||
| :attr:`name` keyword argument to a :class:`Field` constructor), a mechanism | ||||
| exists for replacing MongoEngine field names with the database field names in | ||||
| Javascript code. When accessing a field on a collection object, use | ||||
| square-bracket notation, and prefix the MongoEngine field name with a tilde. | ||||
| The field name that follows the tilde will be translated to the name used in | ||||
| the database. Note that when referring to fields on embedded documents, | ||||
| the name of the :class:`~mongoengine.EmbeddedDocumentField`, followed by a dot, | ||||
| should be used before the name of the field on the embedded document. The | ||||
| following example shows how the substitutions are made:: | ||||
|  | ||||
|     class Comment(EmbeddedDocument): | ||||
|         content = StringField(name='body') | ||||
|  | ||||
|     class BlogPost(Document): | ||||
|         title = StringField(name='doctitle') | ||||
|         comments = ListField(EmbeddedDocumentField(Comment), name='cs') | ||||
|  | ||||
|     # Returns a list of dictionaries. Each dictionary contains a value named | ||||
|     # "document", which corresponds to the "title" field on a BlogPost, and | ||||
|     # "comment", which corresponds to an individual comment. The substitutions | ||||
|     # made are shown in the comments. | ||||
|     BlogPost.objects.exec_js(""" | ||||
|     function() { | ||||
|         var comments = []; | ||||
|         db[collection].find(query).forEach(function(doc) { | ||||
|             // doc[~comments] -> doc["cs"] | ||||
|             var docComments = doc[~comments]; | ||||
|  | ||||
|             for (var i = 0; i < docComments.length; i++) { | ||||
|                 // doc[~comments][i] -> doc["cs"][i] | ||||
|                 var comment = doc[~comments][i]; | ||||
|  | ||||
|                 comments.push({ | ||||
|                     // doc[~title] -> doc["doctitle"] | ||||
|                     'document': doc[~title], | ||||
|  | ||||
|                     // comment[~comments.content] -> comment["body"] | ||||
|                     'comment': comment[~comments.content] | ||||
|                 }); | ||||
|             } | ||||
|         }); | ||||
|         return comments; | ||||
|     } | ||||
|     """) | ||||
|  | ||||
| .. _guide-atomic-updates: | ||||
|  | ||||
| Atomic updates | ||||
|   | ||||
		Reference in New Issue
	
	Block a user