mongodbmongodb-indexescompound-index

compound index syntax in mongodb


a quick question regarding how to define a compound index

as per the definition I can create a compound index as below and it would create a compound index for student_id and class_id.

db.students.createIndex({"student_id":1,"class_id":1});

however what will happen if I give something like this

db.students.createIndex({"student_id,class_id":1})

the syntax is valid and it accepts the index, but I am not sure how the index would be created.

any thoughts ?


Solution

  • Well if you do that and then just run db.students.getIndexes() you will see that the index is created for a field called student_id,class_id - which is exactly what you asked for, i.e. not a compound index but an index on a field with a comma in its name.

    {
        "v" : 1,
        "key" : {
            "student_id,class_id" : 1.0
        },
        "name" : "student_id,class_id_1",
        "ns" : "tmp.students"
    }