In RDMS to look for what data are stored in a table we would use SELECT. In MongoDB we are going to use find method instead.
Search method
For the search function you provide 2 documents as arguments.
The 1st arg is what you are looking for.
The 2th what values do you want to retrieve.
> db.funnynumbers.insert( { rado: 1, a:100, b:200 , c:300} ) > > db.funnynumbers.find( {a:100} ) { "_id" : ObjectId("52b252ff95aed72de4e6ed39"), "rado" : 1, "a" : 100, "b" : 200, "c" : 300 } > > db.funnynumbers.find( {a:100}, {rado:true} ) { "_id" : ObjectId("52b252ff95aed72de4e6ed39"), "rado" : 1 } > db.funnynumbers.find( {a:100}, {rado:true, _id : false} ) { "rado" : 1 }
Another example.
> db.grades.find() { "_id" : ObjectId("50906d7fa3c412bb040eb577"), "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 } { "_id" : ObjectId("50906d7fa3c412bb040eb578"), "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 } { "_id" : ObjectId("50906d7fa3c412bb040eb579"), "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 } { "_id" : ObjectId("50906d7fa3c412bb040eb57a"), "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 } { "_id" : ObjectId("50906d7fa3c412bb040eb57b"), "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 } { "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
You can specify multiple search criterias by specifying multiple document attributes.
> db.grades.find( {"student_id" : 0} ) { "_id" : ObjectId("50906d7fa3c412bb040eb577"), "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 } { "_id" : ObjectId("50906d7fa3c412bb040eb578"), "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 } { "_id" : ObjectId("50906d7fa3c412bb040eb579"), "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 } { "_id" : ObjectId("50906d7fa3c412bb040eb57a"), "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 } > db.grades.find( {"student_id" : 0, "type" : "quiz"} ) { "_id" : ObjectId("50906d7fa3c412bb040eb578"), "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
No comments:
Post a Comment