Spring Data MongoDB
  1. Spring Data MongoDB
  2. DATAMONGO-79

@Indexed(name = "foo", unique = true, dropDups = true) where foo is not a valid field causes an exception, but index is still created and all but one data is dropped

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Complete
    • Affects Version/s: 1.0 M1, 1.0 M2
    • Fix Version/s: 1.0 M2
    • Component/s: Core
    • Labels:
      None
    • Environment:
      osx

      Description

      Using:

      @Indexed(name = "foo", collection="test", unique = true, dropDups = true)
      private String bar;

      causes an exception to be thrown, along the lines of:

      com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error index: mongoDb.test.$foo_1 dup key: { : null }

      but checking via shell of mongodb, the index is still created and all but one entry in the collection is dropped.

        Activity

        Hide
        Adrian Co added a comment -

        Additionally, I'm using the snapshot from 1.0.0.BUILD-20110405.112748-214.

        Thanks and let me know if you need further details.

        Show
        Adrian Co added a comment - Additionally, I'm using the snapshot from 1.0.0.BUILD-20110405.112748-214. Thanks and let me know if you need further details.
        Hide
        Jon Brisbin added a comment -

        This happens in MongoDB, not Spring Data. I just re-created this error directly from the command line:

        db.test.ensureIndex(

        {"foo":1}

        ,

        {"unique":true,"dropDups":true}

        );
        db.test.insert(

        {"test":"value"}

        );
        db.test.insert(

        {"test":"value"}

        );

        E11000 duplicate key error index: database.test.$foo_1 dup key: { : null }

        I'm not sure there's anything in Spring Data we can do, other than to not allow the "name" to be specified on the @Indexed annotation.

        Show
        Jon Brisbin added a comment - This happens in MongoDB, not Spring Data. I just re-created this error directly from the command line: db.test.ensureIndex( {"foo":1} , {"unique":true,"dropDups":true} ); db.test.insert( {"test":"value"} ); db.test.insert( {"test":"value"} ); E11000 duplicate key error index: database.test.$foo_1 dup key: { : null } I'm not sure there's anything in Spring Data we can do, other than to not allow the "name" to be specified on the @Indexed annotation.
        Hide
        Jon Brisbin added a comment -

        Checked in the MongoDB IRC and if you specify "sparse=true" on your index, this doesn't happen.

        I just tested this with:

        @Indexed(name = "foo", unique = true, dropDups = true, sparse = true)
        private Integer ssn;

        There is no error. There's also duplicate records because the index name doesn't match the property name, but that's to be expected.

        Show
        Jon Brisbin added a comment - Checked in the MongoDB IRC and if you specify "sparse=true" on your index, this doesn't happen. I just tested this with: @Indexed(name = "foo", unique = true, dropDups = true, sparse = true) private Integer ssn; There is no error. There's also duplicate records because the index name doesn't match the property name, but that's to be expected.
        Hide
        Jon Brisbin added a comment -

        I also added some checking in the code to issue a warning if the name specified in the annotation doesn't match the property name and sparse is not set to true.

        Show
        Jon Brisbin added a comment - I also added some checking in the code to issue a warning if the name specified in the annotation doesn't match the property name and sparse is not set to true.
        Hide
        Jon Brisbin added a comment -

        Added code to check for the situation where a unique index name doesn't match the property it's annotated on and sparse is not set to true. This error doesn't occur if either the names match or sparse is set to true on unique indexes.

        Show
        Jon Brisbin added a comment - Added code to check for the situation where a unique index name doesn't match the property it's annotated on and sparse is not set to true. This error doesn't occur if either the names match or sparse is set to true on unique indexes.
        Hide
        Adrian Co added a comment -

        Hi,

        Thanks for clarifying this.

        On a similar note, I'm not sure what others think, but would it be better to set dropDups=false as the default value? I find it a bit unnerving that the default value might make me lose a lot of data.

        Show
        Adrian Co added a comment - Hi, Thanks for clarifying this. On a similar note, I'm not sure what others think, but would it be better to set dropDups=false as the default value? I find it a bit unnerving that the default value might make me lose a lot of data.

          People

          • Assignee:
            Jon Brisbin
            Reporter:
            Adrian Co
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: