08. MongoDB - CRUD operations - select the fields using $or,$and
dba@HP:~$ mongo university
MongoDB shell version: 2.4.9
connecting to: university
> show collections
name
result
system.indexes
>
# query using $or
The $or operator performs a logical OR operation on an array of two or more <expressions> and selects the documents that satisfy at least one of the <expressions>
/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$or' function */
> db.result.find({$or:[{"exam type":"Essay"},{score:{$gte:24}}]})
{ "_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 }
{ "_id" : ObjectId("538de41d2110740e7c9a537a"), "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "_id" : ObjectId("538de41d2110740e7c9a537c"), "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "_id" : ObjectId("538de41d2110740e7c9a537d"), "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
$and performs a logical AND operation on an array of two or more expressions (e.g. <expression1>, <expression2>, etc.) and selects the documents that satisfy all the expressions in the array
/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$and' function */ > db.result.find({$and:[{"exam type":"Essay"},{score:{$gte:24}}]})
{ "_id" : ObjectId("538de41d2110740e7c9a5374"), "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5377"), "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "_id" : ObjectId("538de41d2110740e7c9a537d"), "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "_id" : ObjectId("538de41d2110740e7c9a5380"), "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5389"), "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "_id" : ObjectId("538de41d2110740e7c9a538c"), "student" : 9, "exam type" : "Essay", "score" : 61 }
....
/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$and' function and hide '_id' filed */
> db.result.find({$and:[{"exam type":"Essay"},{score:{$gte:24}}]},{"_id":false})
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "student" : 9, "exam type" : "Essay", "score" : 61 }
MongoDB shell version: 2.4.9
connecting to: university
> show collections
name
result
system.indexes
>
# query using $or
The $or operator performs a logical OR operation on an array of two or more <expressions> and selects the documents that satisfy at least one of the <expressions>
/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$or' function */
> db.result.find({$or:[{"exam type":"Essay"},{score:{$gte:24}}]})
{ "_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 }
{ "_id" : ObjectId("538de41d2110740e7c9a537a"), "student" : 3, "exam type" : "Essay", "score" : 15 }
{ "_id" : ObjectId("538de41d2110740e7c9a537c"), "student" : 4, "exam type" : "Exams", "score" : 56 }
{ "_id" : ObjectId("538de41d2110740e7c9a537d"), "student" : 4, "exam type" : "Essay", "score" : 38 }
....
/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$or' function and hide '_id' filed */
> db.result.find({$or:[{"exam type":"Essay"},{score:{$gte:24}}]},{"_id":false})/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$or' function and hide '_id' filed */
{ "student" : 1, "exam type" : "Exams", "score" : 69 }
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 1, "exam type" : "Quiz", "score" : 54 }
{ "student" : 2, "exam type" : "Exams", "score" : 75 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 2, "exam type" : "Quiz", "score" : 59 }
....
# query using $and $and performs a logical AND operation on an array of two or more expressions (e.g. <expression1>, <expression2>, etc.) and selects the documents that satisfy all the expressions in the array
/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$and' function */ > db.result.find({$and:[{"exam type":"Essay"},{score:{$gte:24}}]})
{ "_id" : ObjectId("538de41d2110740e7c9a5374"), "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "_id" : ObjectId("538de41d2110740e7c9a5377"), "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "_id" : ObjectId("538de41d2110740e7c9a537d"), "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "_id" : ObjectId("538de41d2110740e7c9a5380"), "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "_id" : ObjectId("538de41d2110740e7c9a5389"), "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "_id" : ObjectId("538de41d2110740e7c9a538c"), "student" : 9, "exam type" : "Essay", "score" : 61 }
....
/* select fileds 'exam type' equal 'Essay' or 'score' graterthan equal 24 using '$and' function and hide '_id' filed */
> db.result.find({$and:[{"exam type":"Essay"},{score:{$gte:24}}]},{"_id":false})
{ "student" : 1, "exam type" : "Essay", "score" : 92 }
{ "student" : 2, "exam type" : "Essay", "score" : 66 }
{ "student" : 4, "exam type" : "Essay", "score" : 38 }
{ "student" : 5, "exam type" : "Essay", "score" : 24 }
{ "student" : 8, "exam type" : "Essay", "score" : 85 }
{ "student" : 9, "exam type" : "Essay", "score" : 61 }
....
0 comments: