mongo composite index

1. The compound index creation syntax
        db.collection.createIndex( { <field1>: <type>, <field2>: <type2>, ... } ) is
        the same as creating a single key (column) index. When creating an index, you need to specify each The order of a key index for
        multiple keys is directly separated by commas.
        Index creation syntax can be referred to: http://blog.csdn.net/leshami/article/details/53541978

2. Some features of composite indexes The
        composite index can support queries that require matching multiple keys.
        The order of each key of the composite index is very important, which will determine whether the index can be used in the query process. The
        composite index supports leading (suffix) columns Index queries
        cannot create a composite index based on the hash index type.
        Any composite index field cannot exceed 31

In the business scenario of paying attention to the relationship, I often unfollow and follow. This operation is very frequent. Every time I follow and cancel, I have to add or delete the relationship in the library accordingly. Previously, I had to query and delete before I deleted it. Prevent repeated submissions, and later in the test phase, it was found that preventing repeated additions would make normal attention and removal operations impossible; then if I cancel the prevention of repeated submissions, there will be duplicate data in high concurrency situations; then consider using mongo's composite unique index To this de-duplication, annotate the corresponding entity class in java

@CompoundIndexes({
        @CompoundIndex(name = "xx名字",def = "{'xx':1,'xx':1}",unique = true)
})

Then I tested it and found that the same five records were inserted in a row, and no errors were reported in the middle, but there was exactly one and only one in the library, which just met my needs; then I used parallel streams to simulate high concurrency. There is an error; I am testing a composite unique index, if it is a composite index, I will test it separately.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324963166&siteId=291194637