class Meta: collection_name = "ctf" ignore_unknown_fields = True indexes = [ IndexModel([('guild_id', 1), ('name', 1)], unique=True), IndexModel([('guild_id', 1), ('category_id', 1)], unique=True) ]
def _create_indexes(self): text_index = IndexModel( [("given_name", TEXT), ("family_name", TEXT), ("email", TEXT)] ) sort_index = IndexModel([("family_name", ASCENDING), ("splash_md.last_edit", DESCENDING)]) self._collection.create_indexes([text_index, sort_index]) super()._create_indexes()
class Meta: # write_concern = WriteConcern(j=True) connection_alias = AUTH_DB indexes = [ IndexModel("jti", unique=True), IndexModel("token", unique=True) ]
class Meta: # write_concern = WriteConcern(j=True) connection_alias = AUTH_DB indexes = [ IndexModel("uuid", unique=True), IndexModel("shortname", unique=True), ]
class Meta: write_concern = WriteConcern(w=1) connection_alias = "reviewer" final = True indexes = [ IndexModel([("phone_no", pymongo.DESCENDING)], unique=True), IndexModel("session_id") ]
class Meta: write_concern = WriteConcern(w=1) connection_alias = "reviewer" final = True indexes = [ IndexModel([("name", pymongo.DESCENDING), ("department_id", pymongo.DESCENDING)], unique=True), IndexModel("department_id") ]
class Meta: collection_name = 'product_images' indexes = [ IndexModel([ ('upc', pymongo.ASCENDING) ]), IndexModel([ ('shelf_image_id', pymongo.ASCENDING) ]) ]
class Meta: write_concern = WriteConcern(w=1) connection_alias = "reviewer" final = True indexes = [ IndexModel([("person_id", pymongo.DESCENDING), ("group_id", pymongo.DESCENDING), ("role_id", pymongo.DESCENDING)], unique=True), IndexModel("group_id") ]
class Meta: write_concern = WriteConcern(j=True) indexes = [ IndexModel([("barcode", 1)], unique=True), IndexModel( [("barcode", TEXT), ("properties.sample_info.summary.name", TEXT), ("properties.sample_info.summary.submitted_species_name", TEXT), ("properties.sample_info.summary.emails", TEXT), ("properties.sample_info.summary.group", TEXT), ("workflows.*.root.0.sample.batch_name", TEXT)], name="textindex") ]
class Meta: write_concern = WriteConcern(w=1) connection_alias = "reviewer" final = True indexes = [ IndexModel([("phone_no", pymongo.DESCENDING)], unique=True, partialFilterExpression={"phone_no": { "$exists": True }}), IndexModel("first_name"), IndexModel("middle_name"), IndexModel("surname") ]
class Meta: indexes = [ IndexModel([ ("version_a", ASCENDING), ("editor", ASCENDING), ("concise", ASCENDING), ("timestamp", DESCENDING), ]), IndexModel([ ("version_a", ASCENDING), ("version_b", ASCENDING), ("concise", ASCENDING), ]), ]
class Meta: connection_alias = "uknow" write_concern = WriteConcern(j=True) indexes = [ IndexModel('email', unique=True, partialFilterExpression={ '_cls': '%s.User' % (__name__,)}) ]
def test_transaction_write_concern_override(self): """Test txn overrides Client/Database/Collection write_concern.""" client = rs_client(w=0) self.addCleanup(client.close) db = client.test coll = db.test coll.insert_one({}) with client.start_session() as s: with s.start_transaction(write_concern=WriteConcern(w=1)): self.assertTrue(coll.insert_one({}, session=s).acknowledged) self.assertTrue( coll.insert_many([{}, {}], session=s).acknowledged) self.assertTrue( coll.bulk_write([InsertOne({})], session=s).acknowledged) self.assertTrue( coll.replace_one({}, {}, session=s).acknowledged) self.assertTrue( coll.update_one({}, { "$set": { "a": 1 } }, session=s).acknowledged) self.assertTrue( coll.update_many({}, { "$set": { "a": 1 } }, session=s).acknowledged) self.assertTrue(coll.delete_one({}, session=s).acknowledged) self.assertTrue(coll.delete_many({}, session=s).acknowledged) coll.find_one_and_delete({}, session=s) coll.find_one_and_replace({}, {}, session=s) coll.find_one_and_update({}, {"$set": {"a": 1}}, session=s) unsupported_txn_writes = [ (client.drop_database, [db.name], {}), (db.create_collection, ['collection'], {}), (db.drop_collection, ['collection'], {}), (coll.drop, [], {}), (coll.map_reduce, ['function() {}', 'function() {}', 'output'], {}), (coll.rename, ['collection2'], {}), # Drop collection2 between tests of "rename", above. (coll.database.drop_collection, ['collection2'], {}), (coll.create_indexes, [[IndexModel('a')]], {}), (coll.create_index, ['a'], {}), (coll.drop_index, ['a_1'], {}), (coll.drop_indexes, [], {}), (coll.aggregate, [[{ "$out": "aggout" }]], {}), ] for op in unsupported_txn_writes: op, args, kwargs = op with client.start_session() as s: kwargs['session'] = s s.start_transaction(write_concern=WriteConcern(w=1)) with self.assertRaises(OperationFailure): op(*args, **kwargs) s.abort_transaction()
class Meta: collection_name = 'skus' indexes = [ IndexModel([ ('upc', pymongo.ASCENDING) ]) ]
class Meta: # Для скорейшей выборки данных настраиваем индексы indexes = [ IndexModel([('card_number', DESCENDING), ('date_flight', DESCENDING)]), ] # Для снижения нагрузки на PRIMARY ноду читаем из SECONDARY read_preference = ReadPreference.SECONDARY
def assertWriteOpsRaise(self, write_concern, expected_exception): wc = write_concern.document # Set socket timeout to avoid indefinite stalls client = rs_or_single_client( w=wc['w'], wTimeoutMS=wc['wtimeout'], socketTimeoutMS=30000) db = client.get_database('pymongo_test') coll = db.test def insert_command(): coll.database.command( 'insert', 'new_collection', documents=[{}], writeConcern=write_concern.document, parse_write_concern_error=True) ops = [ ('insert_one', lambda: coll.insert_one({})), ('insert_many', lambda: coll.insert_many([{}, {}])), ('update_one', lambda: coll.update_one({}, {'$set': {'x': 1}})), ('update_many', lambda: coll.update_many({}, {'$set': {'x': 1}})), ('delete_one', lambda: coll.delete_one({})), ('delete_many', lambda: coll.delete_many({})), ('bulk_write', lambda: coll.bulk_write([InsertOne({})])), ('command', insert_command), ] ops_require_34 = [ ('aggregate', lambda: coll.aggregate([{'$out': 'out'}])), ('create', lambda: db.create_collection('new')), ('rename', lambda: coll.rename('new')), ('drop', lambda: db.new.drop()), ] if client_context.version > (3, 4): ops.extend(ops_require_34) # SERVER-34776: dropDatabase does not respect wtimeout in 3.6. if client_context.version[:2] != (3, 6): ops.append(('drop_database', lambda: client.drop_database(db))) # SERVER-46668: createIndexes does not respect wtimeout in 4.4+. if client_context.version <= (4, 3): ops.extend([ ('create_index', lambda: coll.create_index([('a', DESCENDING)])), ('create_indexes', lambda: coll.create_indexes([IndexModel('b')])), ('drop_index', lambda: coll.drop_index([('a', DESCENDING)])), ]) for name, f in ops: # Ensure insert_many and bulk_write still raise BulkWriteError. if name in ('insert_many', 'bulk_write'): expected = BulkWriteError else: expected = expected_exception with self.assertRaises(expected, msg=name) as cm: f() if expected == BulkWriteError: bulk_result = cm.exception.details wc_errors = bulk_result['writeConcernErrors'] self.assertTrue(wc_errors)
class Meta: write_concern = WriteConcern(j=True) final = True indexes = [ IndexModel([("id", pymongo.DESCENDING), ("name", pymongo.DESCENDING), ("version", pymongo.DESCENDING)], unique=True) ]
def test_geoHaystack_deprecation(self): self.addCleanup(self.db.test.drop) keys = [("pos", GEOHAYSTACK), ("type", ASCENDING)] self.assertRaises(DeprecationWarning, self.db.test.create_index, keys, bucketSize=1) indexes = [IndexModel(keys, bucketSize=1)] self.assertRaises(DeprecationWarning, self.db.test.create_indexes, indexes)
def test_indexes_same_keys_different_collations(self): self.db.test.drop() usa_collation = Collation('en_US') ja_collation = Collation('ja') self.db.test.create_indexes([ IndexModel('fieldname', collation=usa_collation), IndexModel('fieldname', name='japanese_version', collation=ja_collation), IndexModel('fieldname', name='simple') ]) indexes = self.db.test.index_information() self.assertEqual(usa_collation.document['locale'], indexes['fieldname_1']['collation']['locale']) self.assertEqual(ja_collation.document['locale'], indexes['japanese_version']['collation']['locale']) self.assertNotIn('collation', indexes['simple']) self.db.test.drop_index('fieldname_1') indexes = self.db.test.index_information() self.assertIn('japanese_version', indexes) self.assertIn('simple', indexes) self.assertNotIn('fieldname', indexes)
def parse_indexes(self, config): rv = {} for index in config: fields, collection, unique = ( [], index['collection'], index.get('unique', False)) for field in index['fields']: if isinstance(field, dict): fields.append( (field['name'], getattr(pymongo, field['type']))) else: fields.append((field, pymongo.ASCENDING)) name = self._build_index_name(collection, fields, unique) rv[name] = {'collection': collection, 'model': IndexModel( fields, name=name, unique=unique, background=True)} return rv
import copy import datetime import pymongo from pymongo.operations import IndexModel import datetime from lib import utils DEVICE_DATABASE = "xmq_device2" """ 设备信息 """ DEVICE_INFOS_TB = "device_info" DEVICE_INFOS_TB_INDEXES = [ IndexModel("imei", uniq=True), ] DEVICE_TB_ROW_DEFINE = { "imei": (u"", unicode), "sleep_data": ([], list), "heart_rate_data": ([], list), "log_items": ([], list), "iccid": (u"", unicode), "hardware_version": (u"", unicode), "software_version": (u"", unicode), "electric_quantity": (-1, int), "j01_repoter_date": (None, datetime.datetime), "sim_deadline": (None, datetime.datetime), "location_data": ([], list), "light_status": (0, int), "device_name": (u"", unicode), #设备名称
def _create_indexes(self): self._collection.create_index("name", unique=True) sort_index = IndexModel([("name", ASCENDING), ("splash_md.last_edit", DESCENDING)]) self._collection.create_indexes([sort_index]) super()._create_indexes()
class Meta: write_concern = WriteConcern(j=True) indexes = [IndexModel([("name", 1)], unique=True)]
def test_index_model(self): model = IndexModel([('a', 1), ('b', -1)], collation=self.collation) self.assertEqual(self.collation.document, model.document['collation'])
class Meta: indexes = [IndexModel("email", unique=True)]
class Meta: indexes = [IndexModel("user", unique=True)]
class Meta: indexes = [ IndexModel([('product_id', 1), ('name', 1)], unique=True) ]
class Meta: indexes = [IndexModel([("name", 1)], unique=True)]
def prepare_database(): db = get_db() db.client_parameters.create_indexes([ IndexModel([('hostname', ASCENDING), ('parameter', ASCENDING)]), IndexModel([('parameter', ASCENDING)])]) db.clients.create_indexes([ IndexModel([('hostname', ASCENDING)], unique=True), IndexModel([('suspended', ASCENDING)])]) db.issues.create_indexes([IndexModel([('hostname', ASCENDING), ('name', ASCENDING), ('closed_at', ASCENDING), ('unsnooze_at', ASCENDING)]), IndexModel([('closed_at', ASCENDING), ('unsnooze_at', ASCENDING)]), IndexModel([('hostname', ASCENDING), ('closed_at', ASCENDING), ('unsnooze_at', ASCENDING)]), IndexModel([('name', ASCENDING), ('closed_at', ASCENDING), ('unsnooze_at', ASCENDING)]), IndexModel([('hostname', ASCENDING), ('suspended', ASCENDING)])]) db.patches.create_indexes([IndexModel([('pending_hosts', ASCENDING)]), IndexModel([('files.path', ASCENDING)])])
import copy import datetime import pymongo from pymongo.operations import IndexModel from lib import utils PET_DATABASE = "xmq_pet" """ 宠物基本信息表 """ PET_INFOS_TB = "pet_infos" PET_INFOS_TB_INDEXES = [ IndexModel("pet_id", unique=True), IndexModel("device_imei", unique=True), IndexModel("uid"), ] _PET_INFOS_TB_ROW_DEFINE = { "pet_id": (None, int), # 宠物全局唯一ID "uid": (None, int), "nick": (u"", unicode), "logo_url": (u"", unicode), "logo_small_url": (u"", unicode), "birthday": (datetime.datetime(1970, 1, 1), datetime.datetime), "device_type": None, # 设备类型 "sex": (0, int), "weight": (0, float), "pet_type_id": (None, int), "description": (u"", unicode),