Reputation: 1731
I have a background of postgresql and relational databases and I'm trying to give mongo a chance again since it seems good for some uses. I was doing the exercises on this website http://www.w3resource.com/mongodb-exercises/ which operates over a document with the following structure:
{
"address": {
"building": "1007",
"coord": [ -73.856077, 40.848447 ],
"street": "Morris Park Ave",
"zipcode": "10462"
},
"borough": "Bronx",
"cuisine": "Bakery",
"grades": [
{ "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
{ "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
{ "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
{ "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
{ "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
],
"name": "Morris Park Bake Shop",
"restaurant_id": "30075445"
}
In two of the exercises you operate with the "coord" array, defining conditions for the latitude and the longitude. The thing is that I don't see where is restricting the first component or the second component of the array. It's not saying it specifically....can you see the difference in the following exercises where they ask to limit the latitude and on the other one the longitude?. How do you say specifically "The first component of the coords array must be less than..." or "The second component of coords array must be...." Thank you very much
Write a MongoDB query to find the restaurants that does not prepare any cuisine of 'American' and their grade score more than 70 and latitude less than -65.754168. Go to the editor
db.restaurants.find( {$and: [ {"cuisine" : {$ne :"American "}}, {"grades.score" : {$gt : 70}}, {"address.coord" : {$lt : -65.754168}} ] } );
Write a MongoDB query to find the restaurants which does not prepare any cuisine of 'American' and achieved a score more than 70 and not located in the longitude less than -65.754168. Note : Do this query without using $and operator. Go to the editor
db.restaurants.find( {$query: { "cuisine" : {$ne : "American "}, "grades.score" :{$gt: 70}, "address.coord" : {$lt : -65.754168} } });
Upvotes: 3
Views: 921
Reputation: 103435
Answering your question, to generate a query which translates this command
"The first component of the coords array must be less than..."
or
"The second component of coords array must be...."
you would need to use the dot notation for accessing the element of an array by the zero-based index position and concatenating the array name with the dot (.) and zero-based index position, and enclose in quotes:
"<array>.<index>"
For the above example, the first element of the array should hold the longitude value and the second should contain the latitude value. Thus to access the longitude you need
"address.coord.0"
and for the latitude
"address.coord.1"
So for both exercices, the correct queries follow:
Find the restaurants that does not prepare any cuisine of 'American' and their grade score more than 70 and latitude less than -65.754168:
db.restaurants.find({
"cuisine": { "$ne": "American "},
"grades.score": { "$gt": 70 },
"address.coord.1": { "$lt": -65.754168 }
});
Find the restaurants which does not prepare any cuisine of 'American' and achieved a score more than 70 and not located in the longitude less than -65.754168.
db.restaurants.find({
"cuisine": { "$ne": "American "},
"grades.score": { "$gt": 70 },
"address.coord.0": { "$gte": -65.754168 }
});
In both the above examples, $and
is implicitly used when specifying a comma separated list of expressions. Using an explicit AND with the $and
operator is necessary when the same field or operator has to be specified in multiple expressions.
Upvotes: 4