diff --git a/mongorm/connection.py b/mongorm/connection.py index 87fe3b9..12e7d6a 100644 --- a/mongorm/connection.py +++ b/mongorm/connection.py @@ -75,6 +75,9 @@ def ensureIndexes( ): # database[collection].drop_index(key) # droppedIndexes.append(key) + # Build the index in the background + kwargs['background'] = True + database[collection].ensure_index(key_or_list, **kwargs) registeredIndexes += stagedIndexes diff --git a/mongorm/fields/DictField.py b/mongorm/fields/DictField.py index 12f48d6..03715d8 100644 --- a/mongorm/fields/DictField.py +++ b/mongorm/fields/DictField.py @@ -18,8 +18,3 @@ def toPython( self, bsonValue ): if bsonValue is None: bsonValue = {} return bsonValue - - def toQuery( self, pythonValue, dereferences=[] ): - return { - '.'.join( dereferences ): pythonValue - } \ No newline at end of file diff --git a/mongorm/fields/EmbeddedDocumentField.py b/mongorm/fields/EmbeddedDocumentField.py index f6792c8..c2f8d18 100644 --- a/mongorm/fields/EmbeddedDocumentField.py +++ b/mongorm/fields/EmbeddedDocumentField.py @@ -35,6 +35,4 @@ def toPython( self, bsonValue ): def toQuery( self, pythonValue, dereferences=[] ): # FIXME: this doesn't consider converting the value to/from python # should check the types of the fields the whole way down the chain - return { - '.'.join( dereferences ): pythonValue - } \ No newline at end of file + return pythonValue \ No newline at end of file diff --git a/mongorm/fields/ReferenceField.py b/mongorm/fields/ReferenceField.py index f17bc08..452364d 100644 --- a/mongorm/fields/ReferenceField.py +++ b/mongorm/fields/ReferenceField.py @@ -61,11 +61,7 @@ def fromPython( self, pythonValue, dereferences=[], modifier=None ): return data def toQuery( self, pythonValue, dereferences=[] ): - if pythonValue is None: - return None - return { - '_ref': self.fromPython( pythonValue )['_ref'] - } + return self.fromPython( pythonValue ) def toPython( self, bsonValue ): self._getClassInfo( ) @@ -104,4 +100,4 @@ def toPython( self, bsonValue ): return documentClass( )._fromMongo( initialData ) def optimalIndex( self ): - return self.dbField + '._ref' + return self.dbField diff --git a/mongorm/queryset/Q.py b/mongorm/queryset/Q.py index 62cea2e..b9afb9c 100644 --- a/mongorm/queryset/Q.py +++ b/mongorm/queryset/Q.py @@ -24,7 +24,7 @@ def toMongo( self, document, forUpdate=False, modifier=None ): fieldName = name - MONGO_COMPARISONS = ['gt', 'lt', 'lte', 'gte', 'exists', 'ne', 'all', 'in', 'nin', 'elemMatch'] + MONGO_COMPARISONS = ['gt', 'lt', 'lte', 'gte', 'exists', 'eq', 'ne', 'all', 'in', 'nin', 'elemMatch'] REGEX_COMPARISONS = { 'contains': ( '%s', '' ), 'icontains': ( '%s', 'i' ), @@ -64,36 +64,20 @@ def toMongo( self, document, forUpdate=False, modifier=None ): field = document._fields[fieldName] if not forUpdate: if comparison in ARRAY_VALUE_COMPARISONS: - searchValues = [field.toQuery( item, dereferences=dereferences ) for item in value] - if searchValues and isinstance(searchValues[0], dict): - searchValue = {} - for dictValue in searchValues: - for key in dictValue: - # using setdefault instead of defaultdict in case python < 2.5 - searchValue.setdefault(key, []).append( dictValue[key] ) - else: - searchValue = searchValues + searchValue = [field.toQuery( item, dereferences=dereferences ) for item in value] else: searchValue = field.toQuery( value, dereferences=dereferences ) - targetSearchKey = field.dbField else: if comparison in ARRAY_VALUE_COMPARISONS: - searchValues = [field.fromPython( item, dereferences=dereferences, modifier=modifier ) for item in value] - if searchValues and isinstance(searchValues[0], dict): - searchValue = {} - for dictValue in searchValues: - for key in dictValue: - # using setdefault instead of defaultdict in case python < 2.5 - searchValue.setdefault(key, []).append( dictValue[key] ) - else: - searchValue = searchValues + searchValue = [field.fromPython( item, dereferences=dereferences, modifier=modifier ) for item in value] else: searchValue = field.fromPython( value, dereferences=dereferences, modifier=modifier ) - targetSearchKey = '.'.join( [field.dbField] + dereferences) - + + targetSearchKey = '.'.join( [field.dbField] + dereferences) + valueMapper = lambda value: value - - + + if comparison is not None: if comparison in REGEX_COMPARISONS: regex,options = REGEX_COMPARISONS[comparison] @@ -113,24 +97,13 @@ def toMongo( self, document, forUpdate=False, modifier=None ): valueMapper = lambda value: { '$'+comparison: value } #newSearch[targetSearchKey] = { '$'+comparison: searchValue } - if isinstance(searchValue, dict): - if not forUpdate: - for name,value in searchValue.iteritems( ): - if name: - key = targetSearchKey + '.' + name - else: - key = targetSearchKey - newSearch[key] = valueMapper(value) - else: - newSearch[targetSearchKey] = valueMapper(searchValue) - else: - newSearch[targetSearchKey] = valueMapper(searchValue) - + newSearch[targetSearchKey] = valueMapper(searchValue) + return newSearch - + def __or__( self, other ): return self.do_merge( other, '$or' ) - + def __and__( self, other ): if len( set( self.query.keys() ).intersection( other.query.keys() ) ) > 0: # if the 2 queries have overlapping keys, we need to use a $and to join them. diff --git a/setup.py b/setup.py index 67ecdc9..3cb2197 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages setup(name='mongorm', - version='0.5.8.11', + version='0.5.8.15', packages=find_packages(), author='Theo Julienne, John Garland', author_email='theo@icy.com.au, john@openlearning.com',