I have the following mongo data which looks like this
{
eventType : "mousedown",
eventArgs : {
type : "touchstart",
elementId : "id1"
},
creationDateTime : ISODate("2017-02-24T07:05:49.986Z")
}
I wrote the following query to perform group count.
db.analytics.aggregate
(
{
$match :
{
$and :
[
{"eventArgs.type" : 'touchstart'},
{eventType : 'mousedown'},
{creationDateTime : {$gte : ISODate("2017-02-24T000:00:00.000Z")}}
]
}
},
{
$group :
{
_id :
{
"eventsArgs.elementId" : "$elementId"
},
count :
{
$sum : 1
}
}
}
);
I'm getting error for $group, which states that
FieldPath field names may not contain '.'
If I were not able to specific '.' in
$group :
{
_id :
{
"eventsArgs.elementId" : "$elementId"
},
What is the correct way to do so?
Since you have a single group field, the best way is to just use the _id group key on that field and then create another $project pipeline that will reshape the _id key from the previous pipeline into the desired subdocument that you want. For example
db.analytics.aggregate([
{
"$match": {
"eventArgs.type": 'touchstart',
"eventType": 'mousedown',
"creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") }
}
},
{
"$group": {
"_id": "$eventArgs.elementId",
"count": { "$sum": 1 }
}
},
{
"$project": {
"eventsArgs.elementId": "$_id",
"count": 1, "_id": 0
}
}
]);
The following should work as well:
db.analytics.aggregate([
{
"$match": {
"eventArgs.type": 'touchstart',
"eventType": 'mousedown',
"creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") }
}
},
{
"$group": {
"_id": {
"eventArgs": {
"elementId": "$eventArgs.elementId"
}
},
"count": { "$sum": 1 }
}
}
]);
Related
I am trying to calculate total value if that value exits. But query is not working 100%. So can somebody help me to solve this problem. Here my sample document. I have attached two documents. Please these documents & find out best solution
Document : 1
{
"_id" : 1"),
"message_count" : 4,
"messages" : {
"data" : [
{
"id" : "11",
"saleValue": 1000
},
{
"id" : "112",
"saleValue": 1400
},
{
"id" : "22",
},
{
"id" : "234",
"saleValue": 111
}
],
},
"createdTime" : ISODate("2018-03-18T10:18:48.000Z")
}
Document : 2
{
"_id" : 444,
"message_count" : 4,
"messages" : {
"data" : [
{
"id" : "444",
"saleValue" : 2060
},
{
"id" : "444",
},
{
"id" : 234,
"saleValue" : 260
},
{
"id" : "34534",
}
]
},
"createdTime" : ISODate("2018-03-18T03:11:50.000Z")
}
Needed Output:
{
total : 4831
}
My query :
db.getCollection('myCollection').aggregate([
{
"$group": {
"_id": "$Id",
"totalValue": {
$sum: {
$sum: "$messages.data.saleValue"
}
}
}
}
])
So please if possible help me to solve this problem. Thanks in advance
It's not working correctly because it is aggregating all the documents in the collection; you are grouping on a constant "_id": "tempId", you just need to reference the correct key by adding the $ as:
db.getCollection('myCollection').aggregate([
{ "$group": {
"_id": "$tempId",
"totalValue": {
"$sum": { "$sum": "$messages.data.saleValue" }
}
} }
])
which in essence is a single stage pipeline version of an aggregate operation with an extra field that holds the sum expression before the group pipeline then calling that field as the $sum operator in the group.
The above works since $sum from MongoDB 3.2+ is available in both the $project and $group stages and when used in the $project stage, $sum returns the sum of the list of expressions. The expression "$messages.data.value" returns a list of numbers [120, 1200] which are then used as the $sum expression:
db.getCollection('myCollection').aggregate([
{ "$project": {
"values": { "$sum": "$messages.data.value" },
"tempId": 1,
} },
{ "$group": {
"_id": "$tempId",
"totalValue": { "$sum": "$values" }
} }
])
You can add a $unwind before your $group, in that way you will deconstructs the data array, and then you can group properly:
db.myCollection.aggregate([
{
"$unwind": "$messages.data"
},
{
"$group": {
"_id": "tempId",
"totalValue": {
$sum: {
$sum: "$messages.data.value"
}
}
}
}
])
Output:
{ "_id" : "tempId", "totalValue" : 1320 }
db.getCollection('myCollection').aggregate([
{
$unwind: "$messages.data",
$group: {
"_id": "tempId",
"totalValue": { $sum: "$messages.data.value" }
}
}
])
$unwind
According to description as mentioned into above question, as a solution please try executing following aggregate query
db.myCollection.aggregate(
// Pipeline
[
// Stage 1
{
$unwind: {
path: '$messages.data'
}
},
// Stage 2
{
$group: {
_id: {
pageId: '$pageId'
},
total: {
$sum: '$messages.data.saleValue'
}
}
},
// Stage 3
{
$project: {
pageId: '$_id.pageId',
total: 1,
_id: 0
}
}
]
);
You can do it without using $group. Grouping made other data to be managed and addressed. So, I prefer using $sum and $map as shown below:
db.getCollection('myCollection').aggregate([
{
$addFields: {
total: {
$sum: {
$map: {
input: "$messages.data",
as: "message",
in: "$$message.saleValue",
},
},
},
},
},
}
])
I have the following mongo data which looks like this
{
eventType : "mousedown",
eventArgs : {
type : "touchstart",
elementId : "id1"
},
creationDateTime : ISODate("2017-02-24T07:05:49.986Z")
}
I wrote the following query to perform group count.
db.analytics.aggregate
(
{
$match :
{
$and :
[
{"eventArgs.type" : 'touchstart'},
{eventType : 'mousedown'},
{creationDateTime : {$gte : ISODate("2017-02-24T000:00:00.000Z")}}
]
}
},
{
$group :
{
_id :
{
"eventsArgs.elementId" : "$elementId"
},
count :
{
$sum : 1
}
}
}
);
I'm getting error for $group, which states that
FieldPath field names may not contain '.'
If I were not able to specific '.' in
$group :
{
_id :
{
"eventsArgs.elementId" : "$elementId"
},
What is the correct way to do so?
Since you have a single group field, the best way is to just use the _id group key on that field and then create another $project pipeline that will reshape the _id key from the previous pipeline into the desired subdocument that you want. For example
db.analytics.aggregate([
{
"$match": {
"eventArgs.type": 'touchstart',
"eventType": 'mousedown',
"creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") }
}
},
{
"$group": {
"_id": "$eventArgs.elementId",
"count": { "$sum": 1 }
}
},
{
"$project": {
"eventsArgs.elementId": "$_id",
"count": 1, "_id": 0
}
}
]);
The following should work as well:
db.analytics.aggregate([
{
"$match": {
"eventArgs.type": 'touchstart',
"eventType": 'mousedown',
"creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") }
}
},
{
"$group": {
"_id": {
"eventArgs": {
"elementId": "$eventArgs.elementId"
}
},
"count": { "$sum": 1 }
}
}
]);
I have the following dataset. I need to group them by Account, and then turn the Element_Fieldname into a column.
var collection = [
{
Account:12345,
Element_Fieldname:"cars",
Element_Value:true
},
{
Account:12345,
Element_Fieldname:"boats",
Element_Value:false
}
]
This was my attempt to convert rows to columns, but its not working.
db.getCollection('my_collection').aggregate([{
$match : {
Element_Fieldname : {
$in : ["cars", "boats"]
}
}
}, {
$group : {
_id : "$Account",
values : {
$addToSet : {
field : "$Element_Fieldname",
value : "$Element_Value"
}
}
}
}, {
$project : {
Account : "$_id",
cars : {
"$cond" : [{
$eq : ["$Element_Fieldname", "cars"]
}, "$Element_Value", null]
},
boats : {
"$cond" : [{
$eq : ["$Element_Fieldname", "day_before_water_bottles"]
}, "$Element_Value", null]
},
}
}
])
This just gives me null in my cars and boats fields. Any help would be great.
And this is my desired results:
var desiredResult = [
{
Account:12345,
cars:true,
boats:false
}
]
this is a big tricky but you will get what you need :-)
please add $match on the top of aggregation pipeline
db.collection.aggregate([{
$project : {
_id : 0,
"Account" : 1,
car : {
$cond : [{
$eq : ["$Element_Fieldname", "cars"]
}, "$Element_Value", null]
},
boats : {
$cond : [{
$eq : ["$Element_Fieldname", "boats"]
}, "$Element_Value", null]
},
}
},
{
$group : {
_id : "$Account",
carData : {
$addToSet : "$car"
},
boatsData : {
$addToSet : "$boats"
}
}
}, {
$unwind : "$carData"
}, {
$match : {
carData : {
$ne : null
}
}
}, {
$unwind : "$boatsData"
}, {
$match : {
boatsData : {
$ne : null
}
}
},
])
and result
{
"_id" : 12345,
"carData" : true,
"boatsData" : false
}
It is not possible to do the type of computation you are describing with the aggregation framework, however there is a proposed $arrayToObject expression which will give you the functionality to peek into the key names, and create new key/values dynamically.
For example, you could do
db.collection.aggregate([
{
"$match": { "Element_Fieldname":{ "$in": ["cars", "boats"] } }
},
{
"$group": {
"_id": "$Account",
"attrs": {
"$push": {
"key": "$Element_Fieldname",
"val": "$Element_Value"
}
}
}
},
{
"$project": {
"Account": "$_id",
"_id": 0,
"newAttrs": {
"$arrayToObject": {
"$map": {
"input": "$attrs",
"as": "el",
in: ["$$el.key", "$$el.val"]
}
}
}
}
},
{
"$project": {
"Account": 1,
"cars": "$newAttrs.cars",
"boats": "$newAttrs.boats"
}
}
])
Vote for this jira ticket https://jira.mongodb.org/browse/SERVER-23310 to get this feature.
As a workaround, mapreduce seems like the available option. Consider running the following map-reduce operation:
db.collection.mapReduce(
function() {
var obj = {};
obj[this.Element_Fieldname] = this.Element_Value;
emit(this.Account, obj);
},
function(key, values) {
var obj = {};
values.forEach(function(value) {
Object.keys(value).forEach(function(key) {
obj[key] = value[key];
});
});
return obj;
},
{ "out": { "inline": 1 } }
)
Result:
{
"_id" : 12345,
"value" : {
"cars" : true,
"boats" : false
}
}
db.test.aggregate({
$match : { "themType" : "SuperTest" , "mType" : { "$in" : [ 1 , 2]}}
},
{ $project : { "_id" : 1, "refTestId" : 1, "avatar" : { $concat : [$refTestId] }
} });
and avatar returns me null, probably its because its objectId, is it possible in this query to make from this objectId string ?
From MongoDB 4.0 and newer, there is a $toString operator which returns the ObjectId value as a hexadecimal string:
db.test.aggregate([
{ "$match": {
"themType": "SuperTest",
"mType": { "$in" : [1 , 2] }
} },
{ "$addFields": {
"avatar": { "$toString": "$refTestId" }
} }
])
or using $convert
db.test.aggregate([
{ "$match": {
"themType": "SuperTest",
"mType": { "$in" : [1 , 2] }
} },
{ "$addFields": {
"avatar": {
"$convert": { "input": "$refTestId", "to": "string" }
}
} }
])
This isn't possible yet. WiP issue see: https://jira.mongodb.org/browse/SERVER-29512
I'm pretty new to MongoDB, and having some problems getting my query as I want it. The documents contain "errors" that have happened a specific time. The result I want from the query is an error count for each month per user. This I have already figured out, but additionally I want the total errorcount per user.
This is what I've got so far:
db.Logger.aggregate([
{ "$group": {
"_id": {
"name": "$name",
"month": { "$month": "$errorTime" }
},
"totalErrors": { "$sum": 1 }
}},
{ $group :
{ _id: { name : "$_id.name"},
errors: { $addToSet: { totalErrors: { errorsThisMonth: "$totalErrors", currentMonth : "$_id.month" } } },
}
}
])
The result is:
{
"_id" : {
"name" : "abhos"
},
"errors" : [
{
"totalErrors" : {
"errorsThisMonth" : 6,
"currentMonth" : 2
}
},
{
"totalErrors" : {
"errorsThisMonth" : 6,
"currentMonth" : 1
}
}
]
},
Will it be possible to get what I want by adding to that query?
All you need is an additional $sum in your second $group:
db.Logger.aggregate([
{ "$group": {
"_id": {
"name": "$name",
"month": { "$month": "$errorTime" }
},
"totalErrors": { "$sum": 1 }
}},
{ "$group": {
"_id": "$_id.name",
"errors": {
"$addToSet": {
"errorsThisMonth": "$totalErrors",
"currentMonth" : "$_id.month"
}
},
"totalErrors": { "$sum": "$totalErrors" }
}}
])
Also you have a few extra document levels you do not need in there, such as extra fields under the _id and the "errors" "set" produced in the grouping. This output is just a little different without those additional levels:
{
"_id": "abhos"
"errors" : [
{
"errorsThisMonth" : 6,
"currentMonth" : 2
},
{
"errorsThisMonth" : 6,
"currentMonth" : 1
}
],
"totalErrors": 12
},