Simple update not working using positional $ operator - mongodb

I'm just starting out with Mongo, and following the documentation here, I can't seem to update a value in a nested array when I apply the same technique.
This is my document:
{
"_id" : ObjectId("56d2cf8ee2b075667d4f0545"),
"address" : {
"building" : "522",
"coord" : [
-73.95171,
40.767461
],
"street" : "East 74 Street",
"zipcode" : "10021"
},
"borough" : "Manhattan",
"cuisine" : "American ",
"grades" : [
{
"date" : ISODate("2014-09-02T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"grade" : "B",
"score" : 16,
"date" : ISODate("2013-12-19T00:00:00Z")
},
{
"date" : ISODate("2013-05-28T00:00:00Z"),
"grade" : "A",
"score" : 9
},
{
"date" : ISODate("2012-12-07T00:00:00Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2012-03-29T00:00:00Z"),
"grade" : "A",
"score" : 11
}
],
"name" : "Glorious Food",
"restaurant_id" : "40361521"
}
and this is my query:
db.restaurants.update(
{
_id: 'ObjectId("56d2cf8ee2b075667d4f0545")',
'grades.date': 'ISODate("2014-09-02T00:00:00Z")'
},
{
$set: { 'grades.$.score': 1 }
}
)
I'm sure I must have missed something obvious.

remove quotes from objectId and date field - please see below:
db.restaurants.update(
{
_id: ObjectId("56d2cf8ee2b075667d4f0545"),
"grades.date": ISODate("2014-09-02T00:00:00Z")
},
{
$set: { 'grades.$.score': 1 }
}
)

Related

Map/reduce mongodb using two attributes

I'm practicing with mongo using the sample db restaurants.
The db has the following structure:
{
"_id" : ObjectId("6035397d0076410d79b29bde"),
"address" : {
"building" : "522",
"coord" : [
-73.95171,
40.767461
],
"street" : "East 74 Street",
"zipcode" : "10021"
},
"borough" : "Manhattan",
"cuisine" : "American ",
"grades" : [
{
"date" : ISODate("2014-09-02T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2013-12-19T00:00:00Z"),
"grade" : "B",
"score" : 16
},
{
"date" : ISODate("2013-05-28T00:00:00Z"),
"grade" : "A",
"score" : 9
},
{
"date" : ISODate("2012-12-07T00:00:00Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2012-03-29T00:00:00Z"),
"grade" : "A",
"score" : 11
}
],
"name" : "Glorious Food",
"restaurant_id" : "40361521"
}
{
"_id" : ObjectId("6035397d0076410d79b29bdf"),
"address" : {
"building" : "284",
"coord" : [
-73.9829239,
40.6580753
],
"street" : "Prospect Park West",
"zipcode" : "11215"
},
"borough" : "Brooklyn",
"cuisine" : "American ",
"grades" : [
{
"date" : ISODate("2014-11-19T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2013-11-14T00:00:00Z"),
"grade" : "A",
"score" : 2
},
{
"date" : ISODate("2012-12-05T00:00:00Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2012-05-17T00:00:00Z"),
"grade" : "A",
"score" : 11
}
],
"name" : "The Movable Feast",
"restaurant_id" : "40361606"
}
What I'm trying to do is to get the total of cuisines per borought. So, if a borought has 5 resturants, but in this restaurants, 2 serve the same kind of food, my total should be 4.
I'm suposed to use map/reduce/finalize functions, but I can't figure out how.
All exemples I saw, in the map you have just one attribute to filter de data, but I need to group the restaurants of the same borough and then group e count by cuisine.
Can anyone help me? I've been tryng for a long time, didn't get enyware. The best I manage was to count the total of restaurants per borough.
Thank you.
Try this:
db.restaurants.aggregate([
// First group by "borough" and "cousine".
{
$group: {
_id: {
"borough": "$borough",
"cuisine": "$cuisine"
},
count: { $first: 1 }
}
},
// Second group by only "borough".
{
$group: {
_id: "$_id.borough",
count: { $sum: "$count" }
}
}
]);
Output:
/* 1 */
{
"_id" : "Brooklyn",
"count" : 1
},
/* 2 */
{
"_id" : "Manhattan",
"count" : 2
}
My test data:
/* 1 createdAt:2/24/2021, 11:35:10 PM*/
{
"_id" : ObjectId("6036955624f3423874ac9540"),
"borough" : "Manhattan",
"cuisine" : "Indian",
"restaurant_id" : "40361605"
},
/* 2 createdAt:2/24/2021, 11:35:10 PM*/
{
"_id" : ObjectId("6036955624f3423874ac953f"),
"borough" : "Brooklyn",
"cuisine" : "American",
"restaurant_id" : "40361606"
},
/* 3 createdAt:2/24/2021, 11:35:10 PM*/
{
"_id" : ObjectId("6036955624f3423874ac953e"),
"borough" : "Manhattan",
"cuisine" : "American",
"restaurant_id" : "40361522"
},
/* 4 createdAt:2/24/2021, 11:35:10 PM*/
{
"_id" : ObjectId("6036955624f3423874ac953d"),
"borough" : "Manhattan",
"cuisine" : "American",
"restaurant_id" : "40361521"
}

MongoDB: Filtering out elements from nested arrays

I have a data collection similar to:
{
"_id" : 1,
"name" : "Class 1",
"students" : [
{ "rollNo" : 10001, "name" : "Ram", "score" : 65 },
{ "rollNo" : 10002, "name" : "Shyam", "score" : 90 },
{ "rollNo" : 10003, "name" : "Mohan", "score" : 75 }
]
},
{
"_id" : 2,
"name" : "Class 2",
"students" : [
{ "rollNo" : 20001, "name" : "Krishna", "score" : 88 },
{ "rollNo" : 20002, "name" : "Sohan", "score" : 91 },
{ "rollNo" : 20003, "name" : "Radhika", "score" : 82 },
{ "rollNo" : 20004, "name" : "Komal", "score" : 55 }
]
},
{
"_id" : 3,
"name" : "Class 3",
"students" : [
{ "rollNo" : 30001, "name" : "Monika", "score" : 77 },
{ "rollNo" : 30002, "name" : "Rahul", "score" : 81 }
]
}
In this case I want to grab all students that have a score greater than or equal to 90.
The expected output is:
{
"_id" : 1,
"name" : "Class 1",
"students" : [
{ "rollNo" : 10002, "name" : "Shyam", "score" : 90 }
]
},
{
"_id" : 2,
"name" : "Class 2",
"students" : [
{ "rollNo" : 20002, "name" : "Sohan", "score" : 91 },
]
},
I've tried the following, but it grabs all of Class 1 and Class 2
db.school.find({ $match : {'students.score': { $gte : 90 }}})
I think this is what you need:
db.school.aggregate([
{
$project: {
students: {
$filter: {
input: "$students",
as: "student",
cond: { $gte: ["$$student.score", 90] }
}
},
name: 1,
_id: 1
}
},
{ $match: { students: { $exists: 1, $ne: [] } } }
])
But that does not seem elegant enough for me. There must be a better solution.
You can read about it here:
https://docs.mongodb.com/manual/reference/operator/aggregation/filter/

Mongodb aggregate match value in array

i'm working with the restaurants db in mongo
{
"_id" : ObjectId("5c66fcf59e184ea712adfba6"),
"address" : {
"building" : "97-22",
"coord" : [
-73.8601152,
40.7311739
],
"street" : "63 Road",
"zipcode" : "11374"
},
"borough" : "Queens",
"cuisine" : "Jewish/Kosher",
"grades" : [
{
"date" : ISODate("2014-11-24T00:00:00.000Z"),
"grade" : "Z",
"score" : 20
},
{
"date" : ISODate("2013-01-17T00:00:00.000Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2012-08-02T00:00:00.000Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2011-12-15T00:00:00.000Z"),
"grade" : "B",
"score" : 25
}
],
"name" : "Tov Kosher Kitchen",
"restaurant_id" : "40356068"
}
I'm tryng to filter with match in aggregate. I want to check if any score in grades is greater than 5
db.runCommand({
aggregate: "restaurants",
pipeline : [
{$match: {"grades": {$anyElementTrue: {"score": {$gt:5}}}}}
but i'm getting this error:
"errmsg" : "unknown operator: $anyElementTrue",
thanks
Try with $eleMatch
db.restaurants.aggregate([{$match: {"grades": {$elemMatch: {"score": {$gt:5}}}}}])

How to query mongodb with embedded document

Hi i'm practicing mongodb and I'm stuck with a problem. I'av the following set of documents.
{
"_id" : ObjectId("57cf9a134607674792dbad9e"),
"address" : {
"building" : "351",
"coord" : [
-73.9851356,
40.7676919
],
"street" : "West 57 Street",
"zipcode" : "10019"
},
"borough" : "Manhattan",
"cuisine" : "Irish",
"grades" : [
{
"date" : ISODate("2014-09-06T00:00:00.000Z"),
"grade" : "A",
"score" : 2
},
{
"date" : ISODate("2013-07-22T00:00:00.000Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2012-07-31T00:00:00.000Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2011-12-29T00:00:00.000Z"),
"grade" : "A",
"score" : 12
}
],
"name" : "Dj Reynolds Pub And Restaurant",
"restaurant_id" : "30191841"
}
I want to fetch list of all documents where zipcode is 10019
I'm following mongodb db tutorials and i've tried the following queries but nothing seems to work and i'm getting zero errors.
db.restaurants.find({address:{zipcode:10019}});
db.restaurants.find({"address.zipcode":10019})
zipcode is a string so your query should be
db.restaurants.find({ "address.zipcode": "10019" })
instead of
db.restaurants.find({ "address.zipcode": 10019 })

MongoDB Aggregation - return default value for documents that don't match query

I'm having trouble figuring out the right aggregation pipe operations to return the results I need.
I have a collection similar to the following :-
{
"_id" : "writer1",
"Name" : "writer1",
"Website" : "website1",
"Reviews" : [
{
"Film" : {
"Name" : "Jurassic Park",
"Genre" : "Action"
},
"Score" : 4
},
{
"Technology" : {
"Name" : "Mad Max",
"Genre" : "Action"
},
"Score" : 5
}
]
}
{
"_id" : "writer2",
"Name" : "writer2",
"Website" : "website1",
"Reviews" : [
{
"Technology" : {
"Name" : "Mad Max",
"Genre" : "Action"
},
"Score" : 5
}
]
}
And this is my aggregation so far : -
db.writers.aggregate([
{ "$unwind" : "$Reviews" },
{ "$match" : { "Reviews.Film.Name" : "Jurassic Park" } },
{ "$group" : { "_id" : "$Website" , "score" : { "$avg" : "$Reviews.Score" },
writers :{ $push: { name:"$Name", score:"$Reviews.Score" } }
}}
])
This returns only writers who have a review of the matching film and also only websites that have at least 1 writer who has reviewed the film,
however, I need to return all websites containing a list of their all writers, with a score of 0 if they haven't written a review for the specified film.
so, I am currently getting : -
{ "_id" : "website1", "score" : 4, "writers" : [ { "name" : "writer1", "score" : 4 } ] }
When I actually need : -
{ "_id" : "website1", "score" : 2, "writers" : [ { "name" : "writer1", "score" : 4 },{ "name" :"writer2", "score" : 0 } ] }
Can anyone point me in the right direction?
Cheers