MongoDB noob here...
Ok, I have a collection of students, each with a record that looks like the following.... I want to sort the 'type' : 'homework' scores in descending order.
what does that incantation look like on the mongo shell?
> db.students.find({'_id': 1}).pretty()
{
"_id" : 1,
"name" : "Aurelia Menendez",
"scores" : [
{
"type" : "exam",
"score" : 60.06045071030959
},
{
"type" : "quiz",
"score" : 52.79790691903873
},
{
"type" : "homework",
"score" : 71.76133439165544
},
{
"type" : "homework",
"score" : 34.85718117893772
}
]
}
I'm trying this incantation....
doc = db.students.find()
for (_id,score) in doc.scores:
print _id,score
but it's not working.
You will need to manipulate the embedded array in your application code or using the new Aggregation Framework in MongoDB 2.2.
Example aggregation in the
mongo
shell:Sample output:
I believe you are doing
M101P: MongoDB for Developers
where homework 3.1 is to remove the lower one from two homework scores. Since aggregations were not taught up to that point you can do something like this:It's easy enough to guess, but anyway, try not cheat with mongo university courses because you won't understand basics then.
This is how I have implemented in Java (Have kept it simple so that it's easier to understand) -
Approach :
Below is working Java code:
sort by the score can be simple like:
but you need to find the one for type:homework ...
This is my approach using pyMongo, the Python driver to MongoDB: