Copying indexes from one collection to another is not copying unqiue indexes and background=true setting

24 views
Skip to first unread message

RG

unread,
Apr 30, 2016, 4:55:49 PM4/30/16
to mongodb-user
Hi,

I am copying indexes from one collection to another using :

db.ORIGINAL_TABLE.getIndexes().forEach( function( i ) { db.COPY_TABLE.ensureIndex( i.key );} );


All the indexes are copied over except unqiues and background=true:

example result

Original Table


    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "analytics_api..ORIGINAL_TABLE"
    },
    {
        "v" : 1,
        "unique" : true,
        "key" : {
            "accounttoken" : 1,
            "date" : -1
        },
        "name" : "accounttoken_1_date_-1",
        "ns" : "analytics_api..ORIGINAL_TABLE",
        "background" : true
    },

    {
        "v" : 1,
        "key" : {
            "embeds" : -1
        },
        "name" : "embeds_-1",
        "ns" : "analytics_api..ORIGINAL_TABLE",
        "background" : true
    },
    {
        "v" : 1,
        "key" : {
            "plays" : -1
        },
        "name" : "plays_-1",
        "ns" : "analytics_api..ORIGINAL_TABLE",
        "background" : true
    },
    {
        "v" : 1,
        "key" : {
            "completes" : -1
        },
        "name" : "completes_-1",
        "ns" : "analytics_api..ORIGINAL_TABLE",
        "background" : true
    }
]

Copied Table

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "analytics_api.COPY_TABLE"
    },
    {
        "v" : 1,
        "key" : {
            "accounttoken" : 1,
            "date" : -1
        },
        "name" : "accounttoken_1_date_-1",
        "ns" : "analytics_api.COPY_TABLE"
    },

    {
        "v" : 1,
        "key" : {
            "embeds" : -1
        },
        "name" : "embeds_-1",
        "ns" : "analytics_api.COPY_TABLE"
    },
    {
        "v" : 1,
        "key" : {
            "plays" : -1
        },
        "name" : "plays_-1",
        "ns" : "analytics_api.COPY_TABLE"
    },
    {
        "v" : 1,
        "key" : {
            "completes" : -1
        },
        "name" : "completes_-1",
        "ns" : "analytics_api.COPY_TABLE"
    }
]

What am I missing?

Chris Cunningham

unread,
May 3, 2016, 2:35:03 AM5/3/16
to mongodb-user


Hello RG,


The reason you are not getting `background` and `unique` after the copy is because these are index options and not the actual index.  You will need to pass these options when present in your collection.


For more information please see [index options].


Please note that `ensureIndex()` has been deprecated and `createIndex()` is now used.


Let us know if you have any further questions.


Regards,


Chris

Reply all
Reply to author
Forward
0 new messages