mongoDB query Result Output - mongodb

I have a following documents in my collection:
{
"_id" : ObjectId("539c118b310c022c947b0055"),
"term" : "aero storm tour",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "4",
"weekofyear" : "22",
"productcount" : 0,
"count" : 22
},
{
"_id" : ObjectId("558c118b310c022c947b1145"),
"term" : "aero",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "4",
"weekofyear" : "22",
"productcount" : 0,
"count" : 21
},
{
"_id" : ObjectId("558c992b310c022c947b0055"),
"term" : "aero storm tour",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "1",
"weekofyear" : "22",
"productcount" : 0,
"count" : 2
},
{
"_id" : ObjectId("558c118b123c022c947b0055"),
"term" : "aero storm tour",
"year" : "2014",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "364",
"weekofyear" : "22",
"productcount" : 0,
"count" : 32
},
{
"_id" : ObjectId("558c223c310c022c947b0055"),
"term" : "aero storm tour",
"year" : "2014",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "365",
"weekofyear" : "22",
"productcount" : 0,
"count" : 22
}
I need to calculate the sum of count and termbased on the condition specified by year and dayofyear .
My query :
db.tq.aggregate(
{$match:
{$or :[
{ $and :[{dayofyear:{ $gte : "1", $lte : "4" }},{year : "2015"}]},
{ $and :[{dayofyear:{ $gte : "363", $lte : "365" }},{year : "2014"}]}
]
}
},
{$group:{
_id :"$term",
totalcount : {
$sum : "$count"
}
}
},
{
$sort : {totalcount : -1}
}
)
Here , I have manually specified the dayofyear as between 1 & 4 year 2015 AND dayofyear 363 & 365 year 2014.
But it does not give the desired result. Can any one point out the mistake in my query ?

Related

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}}}}}])

Moongo db aggregation in embeded document

i'am working on aggregating some data from mongodb et here is an exemple document from my collection :
{
"_id" : ObjectId("573dd055f32d05411462894e"),
"metadata" : {
"ip" : "105.12.84.26",
"yearmonthday" : "20160519",
"hour" : 14,
"month" : 5,
"day" : 19,
"yearmonth" : "201605",
"minute" : 41,
"year" : 2016
},
"new" : {
"minutes" : {
"40" : 1
},
"hourly" : {
"14" : 1
}
},
"minute" : {
"14" : {
"40" : 3,
"41" : 7
}
},
"hourly" : {
"14" : 10
}
}
this collection is recolting the ip address then store then, new is thehour et minute an ip address request the site for the firste time.
I want my result to respond to this : in an interval of time (between hour "X" and hour "Y"), how much the same ip adress connected to my site ?
can onyone help ? thx
Finally, i changed my data model to a simpler one like this :
{
"_id" : ObjectId("573ee726f32d054114648cdb"),
"day" : 20,
"minute" : 29,
"ip" : "197.48.5.8",
"yearmonthday" : "20160520",
"hour" : 10,
"month" : 5,
"year" : 2016
}
{
"_id" : ObjectId("573ee72cf32d054114648cff"),
"day" : 20,
"minute" : 30,
"ip" : "197.48.5.8",
"yearmonthday" : "20160520",
"hour" : 10,
"month" : 5,
"year" : 2016
}
so that a can easily aggregate ip connection based on time range.

mongodb: how to calculate by month difference sessions in percent

My collection metrics contains the following data:
{ "year" : "2011", "month" : "01", "day" : "1", "sessions" : 10 }
{ "year" : "2011", "month" : "01", "day" : "2", "sessions" : 20 }
{ "year" : "2011", "month" : "01", "day" : "3", "sessions" : 30 }
......
{ "year" : "2011", "month" : "02", "day" : "19", "sessions" : 10 }
......
{ "year" : "2011", "month" : "03", "day" : "20", "sessions" : 100 }
...........
I would like to know for each month, the percent of sessions increased or decreased compared to the previous month?
Example of results expected
2011-01 100%
2011-02 -83% (= (10-60)/60 )
2011-03 +900% (= (100-10)/10 )
Is it possible to do this in a mongodb query?

mongoDB Query-eliminate repeating results

I have a collection which looks like this :
{
"_id" : ObjectId("558c108b209c022c947b0055"),
"term" : "aero storm tour",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "184",
"weekofyear" : 26,
"productcount" : 2,
"count" : 1
}
{
"_id" : ObjectId("558c108b209c022c947b0056"),
"term" : "aero storm tour",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "184",
"weekofyear" : 26,
"productcount" : 2,
"count" : 2
}
{
"_id" : ObjectId("558c108b209c022c947b0026"),
"term" : "aero",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "184",
"weekofyear" : 26,
"productcount" : 23,
"count" : 2
}
{
"_id" : ObjectId("558c108b209c022c947b0022"),
"term" : "aero",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "184",
"weekofyear" : 26,
"productcount" : 23,
"count" : 12
}
{
"_id" : ObjectId("558c108b209c022c947b0032"),
"term" : "aero",
"year" : "2015",
"month" : "07",
"day" : "01",
"hour" : "17",
"dayofyear" : "184",
"weekofyear" : 26,
"productcount" : 23,
"count" : 12
}
{
"_id" : ObjectId("5348c108b09c022c947b0055"),
"term" : "aero storm tour",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "170",
"weekofyear" : 26,
"productcount" : 235,
"count" : 1
}
{
"_id" : ObjectId("658c108b209c022c947b0055"),
"term" : "aero storm tour",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "173",
"weekofyear" : 26,
"productcount" : 235,
"count" : 1
}
{
"_id" : ObjectId("558c108b209c022c947a0055"),
"term" : "aero storm tour",
"year" : "2015",
"month" : "06",
"day" : "01",
"hour" : "17",
"dayofyear" : "164",
"weekofyear" : 26,
"productcount" : 235,
"count" : 1
}
I need to find the term where productcount is less than equals to 2 on a particular year , month and day .
My query :
db.tq.find(
{ year : "2015" , month : "06" , day : "01" ,productcount: { $lte: 2 } } ,
{ _id: 0, term: 1, productcount: 1 }
)
Output :
{
"term" : "aero storm tour",
"productcount" : 2
}
{
"term" : "aero storm tour",
"productcount" : 2
}
It returns the correct result but I want to avoid repetitive results by using something like distinct or similar.
Expected output:
{
"term" : "aero storm tour",
"productcount" : 2
}
How to achieve that ?
Your find query is not working because for the date you are matching, there are two documents in your collection. That is why it is giving you two documents as a result.
You can work add limit to find query to get only one result like following-
db.collection.find({
year: "2015",
month: "06",
day: "01",
productcount: {
$lte: 2
}
}, {
_id: 0,
term: 1,
productcount: 1
}
).limit(1)
Otherwise
You can use mongo aggregation to get expected result as shown as following:
db.collection.aggregate({
$match: {
productcount: {
$lte: 2
},
year: "2015",
month: "06",
day: "01"
}
}, {
$group: {
_id: "$term",
"term": {
$first: "$term"
},
"productcount": {
$first: "$productcount" //u can add $max:"$productcount" to get productcount with max value instead of $first: "$productcount"
}
}
}, {
$project: {
"term": 1,
"productcount": 1,
"_id": 0
}
})

MongoDB - query in denormalized model

I have to show the advantages to use normalized model(in this case), with two collections in MongoDB: the first for coaches and the second for teams.
So, I use a denormalized model to show the differences.
For example, if I want to update the palmarès of Barcellona(from 23 Liga to 24 Liga), with denormalized model, I have to find all coaches that coached Barcellona and then I have to update the palmarès inside each coach. Clearly it's too expensive. I want to do this example.
I have this denormalized model of soccer coaches and teams they have coached.
Here is an example:
"_id" : "LEMG_1970",
"name" : "Luis",
"surname" : "Enrique Martinez Garcia",
"age" : 45,
"date_Of_birth" : {
"day" : 8,
"month" : 5,
"year" : 1970
},
"place_Of_birth" : "Gijòn",
"nationality" : "Spanish",
"preferred_formation" : "4-3-3 off",
"coached_Team" : [
{
"_id" : "Bar.43",
"official_name" : "Futbol Club Barcelona"
"common_name" : "Barcellona",
"country" : "Spain",
"started_by" : {
"day" : 28,
"month" : 11,
"year" : 1899
},
"championship" : "La Liga",
"stadium" : {
"name" : "Camp Nou",
"capacity" : 99354
},
"palmarès" : {
"La Liga" : 23,
"Copa del Rey" : 27,
"Supercopa de Espana" : 11,
"UEFA Champions League" : 4,
"UEFA Cup Winners Cup" : 4,
"UEFA Super Cup" : 4,
"FIFA Club World cup" : 2
},
"average age" : 26.9,
"squad value(in mln)" : 591.5,
"foreigners" : 13,
"uniform" : [
"blue",
"dark red"
],
"in_charge" : {
"from" : {
"day" : 1,
"month" : 7,
"year" : 2014
}
},
"matches" : 59
},
{
{
"_id" : "Rom.01",
"official_name" : "Associazione Sportiva Roma SpA",
"common_name" : "Roma",
"country" : "Italy",
"started_by" : {
"day" : 22,
"month" : 6,
"year" : 1927
},
"championship" : "Serie A",
"stadium" : {
"name" : "Olimpico di Roma",
"capacity" : 73261
},
"palmarès" : {
"Serie A" : 3,
"Coppa Italia" : 9,
"Supercoppa Italiana" : 2,
"Serie B" : 1
},
"average age" : 28.3,
"squad value(in mln)" : 253.7,
"foreigners" : 22,
"uniform" : [
"red",
"yellow"
],
"in_charge" : {
"from" : {
"day" : 7,
"month" : 6,
"year" : 2011
},
"to" : {
"day" : 10,
"month" : 5,
"year" : 2012
}
},
"matches" : 41
}
]
As you can see information about teams are into coach document. Now, I want to update the palmarès of Barcellona. I tried this query, but I got an error:
db.coach.update({_id:"LEMG_1970"}, {$set:{"coached_Team.palmarès.La Liga":24}})
This is the advice:
"code" : 16837,
"errmsg" : "cannot use the part (coached_Team of coached_Team.palmarès.La Liga) to traverse the element
What can I do to update the palmarès with denormalized model?
Use the $ positional operator in your update, this identifies an element in an array to update without explicitly specifying the position of the element in the array. Since the positional $ operator acts as a placeholder for the first element that matches the query document, the the array field must appear as part of the query document hence you need the coached_Team array field in your query:
var query = {
"_id" : "LEMG_1970",
"coached_Team._id" : "Bar.43"
},
update = {
"$set": {
"coached_Team.$.palmarès.La Liga": 24
}
};
db.coach.update(query, update);