04. MongoDB - CRUD operations FIND
# create database name (university) and collection name (result)
>use university
switched to db university
>db.createCollection("result")
{ "ok" : 1 }
>
>for (i=1; i<21; i++) { "name"=["Essay","Exams","Quiz"]; for (j=0; j<3; j++){ db.result.insert( {"student":i , "exam type":name[j] , "score":Math.round(Math.random()*100) }) ; } }
>
# if you need to display all the document. use for find() method db.<collection name>.find()
>db.result.find()
{ "_id" : ObjectId("538dd52e2110740e7c9a534b"), "studrnt" : 1, "type" : "Exams", "score" : 70 }
{ "_id" : ObjectId("538dd52e2110740e7c9a534c"), "studrnt" : 1, "type" : "Essay", "score" : 5 }
{ "_id" : ObjectId("538dd52e2110740e7c9a534d"), "studrnt" : 2, "type" : "Exams", "score" : 80 }
{ "_id" : ObjectId("538dd52e2110740e7c9a534e"), "studrnt" : 2, "type" : "Essay", "score" : 83 }
{ "_id" : ObjectId("538dd52e2110740e7c9a534f"), "studrnt" : 3, "type" : "Exams", "score" : 42 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5350"), "studrnt" : 3, "type" : "Essay", "score" : 36 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5351"), "studrnt" : 4, "type" : "Exams", "score" : 67 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5352"), "studrnt" : 4, "type" : "Essay", "score" : 28 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5353"), "studrnt" : 5, "type" : "Exams", "score" : 31 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5354"), "studrnt" : 5, "type" : "Essay", "score" : 7 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5355"), "studrnt" : 6, "type" : "Exams", "score" : 23 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5356"), "studrnt" : 6, "type" : "Essay", "score" : 32 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5357"), "studrnt" : 7, "type" : "Exams", "score" : 94 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5358"), "studrnt" : 7, "type" : "Essay", "score" : 79 }
{ "_id" : ObjectId("538dd52e2110740e7c9a5359"), "studrnt" : 8, "type" : "Exams", "score" : 6 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535a"), "studrnt" : 8, "type" : "Essay", "score" : 28 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535b"), "studrnt" : 9, "type" : "Exams", "score" : 9 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535c"), "studrnt" : 9, "type" : "Essay", "score" : 74 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535d"), "studrnt" : 10, "type" : "Exams", "score" : 4 }
{ "_id" : ObjectId("538dd52e2110740e7c9a535e"), "studrnt" : 10, "type" : "Essay", "score" : 79 }
Type "it" for more
>
# if you need to display the results in a formatted way. you can use pretty() method db.<collection name>.find() .pretty()
>db.result.find().pretty()
{
"_id" : ObjectId("533a5661c49df1c1c35cec92"),
"student" : 1,
"exam type" : "Essay",
"score" : 95
}
{
"_id" : ObjectId("533a5661c49df1c1c35cec93"),
"student" : 1,
"exam type" : "Exams",
"score" : 43
}
{
"_id" : ObjectId("533a5661c49df1c1c35cec94"),
"student" : 1,
"exam type" : "Quiz",
"score" : 6
}
{
"_id" : ObjectId("533a5661c49df1c1c35cec95"),
"student" : 2,
"exam type" : "Essay",
"score" : 54
}
......
>
# if you need to display the first results in a formatted way. you can use findOne() method db.<collection name>.findOne()
>db.result.findOne()
{
"_id" : ObjectId("533a5661c49df1c1c35cec92"),
"student" : 1,
"exam type" : "Essay",
"score" : 95
}
>Next ===> mongoDB - CRUD operations find() operator and operations
0 comments: