Student Collection: Download Student.json
> show dbs
CRUD 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
> use CRUD
switched to db CRUD
> show collections
skill
student
test
> db.student.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "CRUD.student"
}
]
> db.student.createIndex({id:1},{unique:true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.student.createIndex({sec:1},{unique:true})
{
"ok" : 0,
"errmsg" : "E11000 duplicate key error collection: CRUD.student index: sec_1 dup key:
{ sec: \"A\" }",
"code" : 11000,
"codeName" : "DuplicateKey",
"keyPattern" : {
"sec" : 1
},
"keyValue" : {
"sec" : "A"
}
}
> db.student.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "CRUD.student"
},
{
"v" : 2,
"unique" : true,
"key" : {
"id" : 1
},
"name" : "id_1",
"ns" : "CRUD.student"
}
]
> db.student.find({"id":3})
{ "_id" : ObjectId("601193736aed415ccc1d4bc1"), "name" : "kamal", "lastName" : "sharma", "age" : 11,
"averageScore" : 6.4, "id" : 3, "sec" : "A", "gender" : "male" }
>
> db.student.explain().find({id:3})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "CRUD.student",
"indexFilterSet" : false,
"parsedQuery" : {
"id" : {
"$eq" : 3
}
},
"queryHash" : "6DAB46EC",
"planCacheKey" : "801B9D84",
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"id" : 1
},
"indexName" : "id_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"id" : [ ]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"id" : [
"[3.0, 3.0]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "LAPTOP-7HHTVTQF",
"port" : 27017,
"version" : "4.2.2",
"gitVersion" : "a0bbbff6ada159e19298d37946ac8dc4b497eadf"
},
"ok" : 1
}
> db.student.explain().find({age:25})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "CRUD.student",
"indexFilterSet" : false,
"parsedQuery" : {
"age" : {
"$eq" : 25
}
},
"queryHash" : "3838C5F3",
"planCacheKey" : "3838C5F3",
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"age" : {
"$eq" : 25
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "LAPTOP-7HHTVTQF",
"port" : 27017,
"version" : "4.2.2",
"gitVersion" : "a0bbbff6ada159e19298d37946ac8dc4b497eadf"
},
"ok" : 1
}
> db.student.createIndex({age:1},{name:"myAgeIndex"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
> db.student.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "CRUD.student"
},
{
"v" : 2,
"unique" : true,
"key" : {
"id" : 1
},
"name" : "id_1",
"ns" : "CRUD.student"
},
{
"v" : 2,
"key" : {
"age" : 1
},
"name" : "myAgeIndex",
"ns" : "CRUD.student"
}
]
> db.student.insert({id:20, "name":"pankaj"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: CRUD.student index: id_1 dup key:
{ id: 20.0 }"
}
})
> db.student.insert({id:21, "name":"pankaj"})
WriteResult({ "nInserted" : 1 })
> db.student.insert({id:22, "name":"pankaj", age:31})
WriteResult({ "nInserted" : 1 })
> db.student.dropIndex({age:1})
{ "nIndexesWas" : 3, "ok" : 1 }
> db.student.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "CRUD.student"
},
{
"v" : 2,
"unique" : true,
"key" : {
"id" : 1
},
"name" : "id_1",
"ns" : "CRUD.student"
}
]
> db.student.dropIndexes()
{
"nIndexesWas" : 2,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}