I have a restaurants collection that contains 3772 documents and I am trying to calculate the total number of documents that contain a score in first element of the grades array that's a multiple of 7 using the aggregation framework.
Query:
db.restaurants.aggregate([
{$project: {remainder: {$mod: ["$grades.0.score", 7]},
restaurant_id: 1,
name: 1,
grades: 1
}
},
{$match: {remainder: {$eq: 0}}},
{$group: {_id: null, total: {$sum: 1}}}
])
However, I am getting an error message that's caused by the use of the $mod operator in the $project pipeline stage. The error message is the following:
$mod only supports numeric types, not Array and NumberDouble
However, both $grades.0.score and 7 are integers, right? What should I change to make this query work as intended?
Example document:
{
"_id" : ObjectId("57290430139a4a37132c9e93"),
"address" : {
"building" : "469",
"coord" : [
-73.961704,
40.662942
],
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate("2014-12-30T00:00:00Z"),
"grade" : "A",
"score" : 8
},
{
"date" : ISODate("2014-07-01T00:00:00Z"),
"grade" : "B",
"score" : 23
},
{
"date" : ISODate("2013-04-30T00:00:00Z"),
"grade" : "A",
"score" : 12
},
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}
instead of $grades.0.score
put $grades[0].score
in your query.
the above is wrong. see below the correct form. As you want to filter by grades whose first score is a multiple of 7, you aggregation should start like this.
db.restaurants.aggregate([{$match: {"grades.0.score": {$mod: [7, 0]}}},{$group: {_id: null, total: {$sum: 1}}}])
I changed the grade.0.score to 7 and ran the command to check it is working or not, it seems it is working as you wanted.
> db.restaurants.find().pretty();
{
"_id" : 0,
"address" : {
"building" : "469",
"coord" : [
-73.961704,
40.662942
],
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate("2014-12-30T00:00:00Z"),
"grade" : "A",
"score" : 7
},
{
"date" : ISODate("2014-07-01T00:00:00Z"),
"grade" : "B",
"score" : 23
},
{
"date" : ISODate("2013-04-30T00:00:00Z"),
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
> db.restaurants.aggregate([{$match: {"grades.0.score": {$mod: [7, 0]}}},{$group:{_id:null,count:{$sum:1}}} ])
{ "_id" : null, "count" : 1 }
First: why doesn't it work? Try:
db.restaurants.aggregate([
{$project: {
score0: "$grades.0.score",
restaurant_id: 1,
name: 1
}
}
])
You'll see that score0 returns [0 elements] so it does output an array hence the error message.
Based on this other question Get first element in array and return using Aggregate? (Mongodb), here is a solution to your problem:
db.restaurants.aggregate([
{$unwind: "$grades"},
{$group:{"_id":"$_id","grade0":{$first:"$grades"}}},
{$project: {
remainder: {$mod: ["$grade0.score", 7]},
restaurant_id: 1,
name: 1,
grade0: 1,
}
},
{$match: {remainder: {$eq: 0}}},
{$group: {_id: null, total: {$sum: 1}}}
])
Related
I'm learning MongoDB's sorting. I have a collection with documents that look like this:
{
"_id" : ObjectId("5d0c13fbfdca455311248d6f"),
"borough" : "Brooklyn",
"grades" :
[
{ "date" : ISODate("2014-04-16T00:00:00Z"), "grade" : "A", "score" : 5 },
{ "date" : ISODate("2013-04-23T00:00:00Z"), "grade" : "B", "score" : 2 },
{ "date" : ISODate("2012-04-24T00:00:00Z"), "grade" : "A", "score" : 4 }
],
"name" : "C & C Catering Service",
"restaurant_id" : "40357437"
}
And I want to sort all restaurants in Brooklyn by their most recent score.
Right now I have:
db.restaurants.find({borough: "Brooklyn"}).sort()
But I don't know how to proceed. Any help on how to sort this by most recent score, which is the first entry in grades?
This is not possible in mongo with a find query, you'll have to use an aggregation like this one:
db.collection.aggregate([
{
$unwind: "$grades"
},
{
$sort: {"grades.date": -1}
},
{
$group: {
_id:"$_id",
grades: {$push:"$grades"},
resturant_id: {$first: "$resturant_id",
name: {$first: "$name"},
borough: {$first: "$borough"}
}
}
]);
EDIT:
collection.find({}).sort({'grades.0.date': -1});
I am looking at performing a group by upon a given value within a nest object. For example, my document structure is as follows:(Sorry about the limited data)
"_id" : "92623ba7-4ca5-46c7-8d76-c4bc8387ea00",
"Status" : 2.0,
"UploadDate" : ISODate("2018-10-30T12:01:19.619Z"),
"UpdateDate" : ISODate("2018-10-30T12:01:19.619Z"),
"Request" : "abc123",
"ShowCaseHtml" : "",
"PageResult" : [ ],
"ProductFilter" : {
"_id" : "9430fb88-2deb-4508-8422-dd67c3a35205",
"Status" : 2,
"UploadDate" : ISODate("2018-11-05T10:52:37.122Z"),
"UpdateDate" : ISODate("2018-11-05T10:52:37.122Z"),
"ProductPageType" : 0,
"Categories" : [ ],
"PriceRanges": [ ],
"Brands" : [ ],
"Sellers": [ ],
"StarRatings" : [ ],
},
"BreadCrumbs"[ ]
Result of my query returns only a part of object, what i need is full object but filterd version, here is my query
db.getCollection('dbName').aggregate(
{$match: {"Request": "abc123"}},
{$project :
{"PageResult":1,"ProductFilter":1,"BreadCrumbs":1,"ShowCaseHtml":1}},
{$unwind: "$PageResult"},
{$sort:{'PageResult.MarketPlaceProductPrice.ProductPrice':1}},
{$skip: 2},
{$limit: 3},
{$group: {"_id": "$_id", "PageResult": {$push: "$PageResult"}}},
{$project :
{"PageResult.MarketPlaceProductPrice.ProductPrice":1,"_id":0}}
)
Result of the query is like,
{
"PageResult" : [
{
"MarketPlaceProductPrice" : {
"ProductPrice" : 1519.00
}
},
{
"MarketPlaceProductPrice" : {
"ProductPrice" : 2749.00
}
},
{
"MarketPlaceProductPrice" : {
"ProductPrice" : 3359.00
}
}
]
}
i need the ProductFilter,BreadCrumbs and ShowCaseHtml areas too, that is why i specified the
{$project :
{"PageResult":1,"ProductFilter":1,"BreadCrumbs":1,"ShowCaseHtml":1}}
but i only have filtered PageResult area, any ideas ?
Using this query solved my problem
db.getCollection('dbName').aggregate(
{$match: {"Request":"abc123"}},
{$project : {"PageResult":1,"ProductFilter":1,"BreadCrumbs":1,"ShowCaseHtml":1}},
{$unwind: "$PageResult"},
{$sort:{'PageResult.MarketPlaceProductPrice.ProductPrice':1}},
{$skip: 2},
{$limit: 3},
{$group: {"_id": {"Request":"$Request","ProductFilter":"$ProductFilter","BreadCrumbs":"$BreadCrumbs","ShowCaseHtml":"$ShowCaseHtml"},"PageResult": {$push : "$PageResult"}
}}
)
I've got a lot of documents using the following structure in MongoDB:
{
"_id" : ObjectId("..."),
"plant" : "XY_4711",
"hour" : 1473321600,
"units" : [
{
"_id" : ObjectId("..."),
"unit_id" : 10951,
"values" : [
{
"quarter" : 1473321600,
"value" : 395,
},
{
"quarter" : 1473322500,
"value" : 402,
},
{
"quarter" : 1473323400,
"value" : 406,
},
{
"quarter" : 1473324300,
"value" : 410,
}
]
}
]
}
Now I need to find all embedded document values where the quarter is between some given timestamps (eg: { $gte: 1473324300, $lte: 1473328800 }).
I've only got the unit_id and the quarter timestamp from/to for filtering the documents. And I only need the quarter and value grouped and ordered by unit.
I'm new in MongoDB and read something about find() and aggregate(). But I don't know how to do it. MongoDB 3.0 is installed on the server.
Finally I've got it:
I simply have to take apart each array, filtering out the things I don't need and put it back together:
db.collection.aggregate([
{$match : {$and : [{"units.values.quarter" : {$gte : 1473324300}}, {"units.values.quarter" : {$lte : 1473328800 }}]}},
{$unwind: "$units"},
{$unwind: "$units.values"},
{$match : {$and : [{"units.values.quarter" : {$gte : 1473324300}}, {"units.values.quarter" : {$lte : 1473328800 }}]}},
{$project: {"units": {values: {quarter: 1, "value": 1}, unit_id: 1}}},
{$group: {"_id": "$units.unit_id", "quarter_values": {$push: "$units.values"}}} ,
{$sort: {"_id": 1}}
])
Will give:
{
"_id" : 10951,
"quarter_values" : [
{
"quarter" : 1473324300,
"value" : 410
},
{
"quarter" : 1473325200,
"value" : 412
},
{
"quarter" : 1473326100,
"value" : 412
},
{
"quarter" : 1473327000,
"value" : 411
},
{
"quarter" : 1473327900,
"value" : 408
},
{
"quarter" : 1473328800,
"value" : 403
}
]
}
See: Return only matched sub-document elements within a nested array for a detailed description!
I think I have to switch to $map or $filter in the future. Thanks to notionquest for supporting my questions :)
Please see the sample query below. I didn't exactly get your grouping requirement. However, with this sample query you should be able to change and get your desired output.
db.collection.aggregate([
{$unwind : {path : "$units"}},
{$match : {$and : [{"units.values.quarter" : {$gte : 1473324300}}, {"units.values.quarter" : {$lte : 1473328800 }}]}},
{$project : {"units" : {values : {quarter : 1, "value" : 1}, unit_id : 1}}},
{$group : { _id : "$units.unit_id", quarter_values : { $push :{ quarter : "$units.values.quarter", value : "$units.values.value"}}}},
{$sort : {_id : 1 }}
]);
Sample output:-
{
"_id" : 10951,
"quarter_values" : [
{
"quarter" : [
1473321600,
1473322500,
1473323400,
1473324300
],
"value" : [
395,
402,
406,
410
]
}
]
}
I asked the question before. The question
{
"_id" : ObjectId("5539d45ee3cd0e48e99c3fa6"),
"userId" : 1,
"movieId" : 6,
"rating" : 2.0000000000000000,
"timestamp" : 9.80731e+008
}
{
"_id" : ObjectId("5539d45ee3cd0e48e99c1fa7"),
"userId" : 1,
"movieId" : 22,
"rating" : 3.0000000000000000,
"timestamp" : 9.80731e+008
},
{
"_id" : ObjectId("5539d45ee3cd0e48e99c1fa8"),
"userId" : 1,
"movieId" : 32,
"rating" : 2.0000000000000000,
"timestamp" : 9.80732e+008
},
{
"_id" : ObjectId("5539d45ee3cd0e48e99c1fa9"),
"userId" : 2,
"movieId" : 32,
"rating" : 4.0000000000000000,
"timestamp" : 9.80732e+008
},
{
"_id" : ObjectId("5539d45ee3cd0e48e99c1fa3"),
"userId" : 2,
"movieId" : 6,
"rating" : 5.0000000000000000,
"timestamp" : 9.80731e+008
}
Then needed to get the common(intersect) items for given two users (like userId:1 and userId:2) like [6,32].
But now i need to get that with ratings of each of them like [ {"movieId":6,"user1_rating" : 2,"user2_rating" : 4},{"movieId":32,"user1_rating" : 2,"user2_rating" : 5} ]
How can i get that?
I tried to do with
db.collection.aggregate([
{$match: {"$or":[{"userId":2},{"userId":1}]}},
{$group: {_id: "$movieId", users: {$push: {"userId":"$userId","rating":"$rating"}}}},
{$project: { movieId: "$_id", _id: 0,rating:"$users.rating", allUsersIncluded: { $setIsSubset: [ [1,2], "$users.userId"]}}},
{$match: { allUsersIncluded: true }},
{$group: { _id: null, movies: {$push: {"movie":"$movieId","Rating":"$rating"}}}}
])
But I get [ {"movie":6,0 : 2,1 : 4},{"movie":32,0 : 2,1 : 5} ]
Finally i achieved my target.The answer is
db.collection.aggregate([
{$match: {"$or":[{"userId":2},{"userId":1}]}},
{$group: {_id: "$movieId", users: {$addToSet: {"userId":"$userId","rating":"$rating"}}}},
{$project: { movieId: "$_id", _id: 0,user:"$users", allUsersIncluded: { $setIsSubset: [ [1,2], "$users.userId"]}}},
{$match: { allUsersIncluded: true }},
{$group: { _id: null, movies: {$addToSet: {"movie":"$movieId","user":"$user"}}}}
])
Here is a sample of my document from collection called products:
{
"_id" : "B000KIT6LQ",
"brand" : "unknown",
"category" : "Electronics",
"price" : "11.99",
"title" : "Scosche KA2067B 2005..."
"reviews" : [
{
"date" : ISODate("1969-12-31T23:59:59Z"),
"score" : 5,
"user_id" : "AK7M5Y7E9O3L7",
"sentiment" : 0.5,
"text" : "Bought this so I ...",
"user_gender" : "female",
"voted_total" : 0,
"voted_helpful" : 0,
"user_name" : "Alex",
"summary" : "It is what it is"
},
{
"date" : ISODate("1969-12-31T23:59:59Z"),
"score" : 5,
"user_id" : "A26VRLMPEA8IDR",
"sentiment" : 0.352,
"text" : "Years ago I worked as an...",
"user_gender" : "male",
"voted_total" : 0,
"voted_helpful" : 0,
"user_name" : "Jack R. Smith",
"summary" : "Great Kit"
},
{
"date" : ISODate("1969-12-31T23:59:59Z"),
"score" : 4,
"user_id" : "A1TGBDVX3QXCRH",
"sentiment" : 0.19318181818181818,
"text" : "This insert works great in my ...",
"user_gender" : "female",
"voted_total" : 0,
"voted_helpful" : 0,
"user_name" : "J. Reed",
"summary" : "Fits great in my 2006 Spectra5"
}
]
}
I have many documents with multiple categories. I am trying to create a mongo query which will result in all categories with the number of reviews (subdocument) per year. I have to group by categories and year, and get the count for number of reviews.
This is the query that I have got so far:
db.products.aggregate([
{ $unwind : "$reviews" },
{ $group: {
_id: {category: "$category", date: "$reviews.date.getFullYear()"},
count: { $sum: 1 }}},
{$sort:{"count": -1}}
])
For some reason the getFullYear() method is not working for me. If I group by $reviews.date I get the results.
Any pointers on getting the query right is appreciated.
Thanks
You can't use JavaScript functions like getFullYear() in your aggregate pipeline, you need to use the equivalent aggregation Date operator, which in this case is $year.
db.products.aggregate([
{ $unwind : "$reviews" },
{ $group: {
_id: {category: "$category", date: {$year: "$reviews.date"}},
count: { $sum: 1 }}},
{$sort:{"count": -1}}
])