Feature news

05. MongoDB - CRUD operations - select all fields


                                                                                                     Download file
our database-UNIVERSITY ,  collection-RESULT

#  select single field
> db.result.find()
{ "_id" : ObjectId("538de41d2110740e7c9a5373"), "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "_id" : ObjectId("538de41d2110740e7c9a5374"), "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5375"), "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "_id" : ObjectId("538de41d2110740e7c9a5376"), "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "_id" : ObjectId("538de41d2110740e7c9a5377"), "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }
....

> db.result.find({"student":2})
{ "_id" : ObjectId("538de41d2110740e7c9a5376"), "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "_id" : ObjectId("538de41d2110740e7c9a5377"), "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }

> db.result.find({"exam type":"Quiz"})
{ "_id" : ObjectId("538de41d2110740e7c9a5375"), "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "_id" : ObjectId("538de41d2110740e7c9a537b"), "student" : 3, "exam type" : "Quiz", "score" : 19 }
{ "_id" : ObjectId("538de41d2110740e7c9a537e"), "student" : 4, "exam type" : "Quiz", "score" : 74 }
{ "_id" : ObjectId("538de41d2110740e7c9a5381"), "student" : 5, "exam type" : "Quiz", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5384"), "student" : 6, "exam type" : "Quiz", "score" : 59 }
....

#  select double or more field

> db.result.find({"student":2,"exam type":"Quiz"})
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }

> db.result.find({"exam type":"Quiz","score":59})
{ "_id" : ObjectId("538de41d2110740e7c9a5378"), "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "_id" : ObjectId("538de41d2110740e7c9a5384"), "student" : 6, "exam type" : "Quiz", "score" : 59 }

> db.result.find({"exam type":"Quiz","score":59},{"_id":false})
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
{ "student" : 6, "exam type" : "Quiz", "score" : 59 }

> db.result.find({"exam type":"Quiz","score":59},{"_id":false,"student":true})
{ "student" : 2 }
{ "student" : 6 }

> db.result.find({"exam type":"Quiz","score":59},{"_id":false,"student":true,"score":true})
{ "student" : 2, "score" : 59 }
{ "student" : 6, "score" : 59 }


Next ===> MongoDB - CRUD operations query using field SELECT graterhtan,lessthan


0 comments: