Filter aggregations in mongodb - mongodb

Say I'm acting on a collection of documents that look like this:
{
"_id" : ObjectId("552712c3f92ea17426000ace"),
"product" : "Mobile Safari",
"venue_id" : NumberLong(71540),
"uid" : "dd542fea6b4443469ff7bf1f56472eac",
"ag" : 0,
"promo" : "bc40100abc8d4eb6a0c68f81f4a756c7",
"promo_f" : NumberLong(1),
"brand" : NumberLong(17),
"venue" : "ovation_2480",
"lt" : 0,
"ts" : ISODate("2015-04-10T00:01:07.734Z"),
"evt" : "login",
"mac" : "00:00:00:00:00:00",
"__ns__" : "wifipromo",
"pvdr" : NumberLong(42),
"os" : "iPhone",
"cmpgn" : "fc6de34aef8b4f57af0b8fda98d8c530",
"ip" : "192.119.43.250",
"lng" : 0,
"product_ver" : "8"
}
I want to count the total amount of aggregate documents with the same uid.
I use this:
db.events_2015_04_10.aggregate([
{
$group: {
_id: "$uid",
count: {
$sum: 1
}
}
}
]);
But lets say I want only a list of aggregations that don't contain evt: "login." In short I only want to aggregate by uid only if all documents with that uid do not have evt: "login."
how would I do that?

You were almost there. Use $match with the $ne operator.
db.events_2015_04_10.aggregate([
{ "$match": { "evt": { "$ne": "login" }}},
{ "$group": { "_id": "$uid", "count": { "$sum": 1 }}}
])

Related

Finding the collection details if one field is distinct

I have a database of teachers details as given
{ "_id" : ObjectId("5bcc0a44f2752576a8545d99"), "Teacher_id" : "Pic002", "Teacher_Name" : "Ravi Kumar", "Dept_Name" : "IT", "Salary" : 40000, "Status" : "A" }
{ "_id" : ObjectId("5bcc0a5af2752576a8545d9a"), "Teacher_id" : "Pic003", "Teacher_Name" : "Akshay", "Dept_Name" : "Comp", "Salary" : 25500, "Status" : "N" }
{ "_id" : ObjectId("5bcc0a85f2752576a8545d9b"), "Teacher_id" : "Pic003", "Teacher_Name" : "Akshay", "Dept_Name" : "Comp", "Salary" : 25500, "Status" : "N" }
{ "_id" : ObjectId("5bcc0a9af2752576a8545d9c"), "Teacher_id" : "Pic004", "Teacher_Name" : "Sumit", "Dept_Name" : "Mech", "Salary" : 35000, "Status" : "N" }
How would I list down complete details of a teacher whose Department Name is distinct?
Basically, I want to display the details of the first first and last document in this collection.
You can achieve this via this aggregation:
db.collection.aggregate([{
$group: {
_id: "$Dept_Name",
docs: {
$addToSet: "$$CURRENT"
},
count: {
$sum: 1
}
}
},
{
$match: {
"count": {
"$eq": 1
}
}
},
{
$unwind: "$docs"
},
{
$replaceRoot: {
newRoot: "$docs"
}
}
])
The idea is first to $group and at the same time keep the objects via $addToSet.
Then filter (via $match) on those which count is 1 and then $unwind & $replaceRoot.
See it working here

get $sum of items in the same document mongodb

/* 1 */
{
"_id" : ObjectId("5941240cdc8dd12a183337b0"),
"item" : ObjectId("593e700d9f34350aac73231d"),
"store" : ObjectId("593e7589a489ce07c899210d"),
"itemcredits" : [
{
"expireDate" : "10/2019",
"unitePrice" : "50",
"itemCredit" : "5"
},
{
"expireDate" : "12/2018",
"unitePrice" : "50",
"itemCredit" : "3"
}
]
}
i want to query to get the total sum of itemcredits.itemCredit of the same document that match same store and item to get the result = 8 for this example
Try some thing like this
db.collectionName.aggregate([{$group: {
'_id': '$_id',
"total": {$sum: "$itemcredits.itemcredits" }
}}]);
itemcredits need to be integer

Aggregation query returning array of all objects for mongodb

I'm using mongo for the first time. I'm trying to aggregate some documents in a collection using the query below. Instead the query returns an object with a key "result" that contains an array of all the documents that fit with $match.
Below is the query.
db.events_2015_04_10.aggregate([
{$group:{
_id: "$uid",
count: {$sum: 1},
},
$match : {promo:"bc40100abc8d4eb6a0c68f81f4a756c7", evt:"login"}
}
]
);
Below is a sample document in the collection:
{
"_id" : ObjectId("552712c3f92ea17426000ace"),
"product" : "Mobile Safari",
"venue_id" : NumberLong(71540),
"uid" : "dd542fea6b4443469ff7bf1f56472eac",
"ag" : 0,
"promo" : "bc40100abc8d4eb6a0c68f81f4a756c7",
"promo_f" : NumberLong(1),
"brand" : NumberLong(17),
"venue" : "ovation_2480",
"lt" : 0,
"ts" : ISODate("2015-04-10T00:01:07.734Z"),
"evt" : "login",
"mac" : "00:00:00:00:00:00",
"__ns__" : "wifipromo",
"pvdr" : NumberLong(42),
"os" : "iPhone",
"cmpgn" : "fc6de34aef8b4f57af0b8fda98d8c530",
"ip" : "192.119.43.250",
"lng" : 0,
"product_ver" : "8"
}
I'm trying to get it all grouped by uid's with the total sum of each group... What is the correct way to achieve this?
Try the following aggregation framework which has the $match pipeline stage first and then the $group pipeline later:
db.events_2015_04_10.aggregate([
{
$match: {
promo: "bc40100abc8d4eb6a0c68f81f4a756c7",
evt: "login"
}
},
{
$group: {
_id: "$uid",
count: {
$sum: 1
}
}
}
])

MongoDb aggregation framework value of a field where max another field

I have a collection that has records looking like this:
"_id" : ObjectId("550424ef2f44472856286d56"), "accountId" : "123",
"contactOperations" :
[
{ "contactId" : "1", "operation" : 1, "date" : 500 },
{ "contactId" : "1", "operation" : 2, "date" : 501 },
{ "contactId" : "2", "operation" : 1, "date" : 502 }
]
}
I want to know the latest operation number that has been applied on a certain contact.
I'm using the aggregation framework to first unwind the contactOperations and then grouping by accountId and contactOperations.contactId and max contactOperations.date.
aggregate([{$unwind : "$contactOperations"}, {$group : {"_id":{"accountId":"$accountId", "contactId":"$contactOperations.contactId"}, "date":{$max:"$contactOperations.date"} }}])
The result I get is:
"_id" : { "accountId" : "123", "contactId" : "2" }, "time" : 502 }
"_id" : { "accountId" : "123", "contactId" : "1" }, "time" : 501 }
Which seems correct so far, but I also need the contactOperations.operation field that was recorded with $max date. How can I select that?
You have to sort the unwind values then apply $last operator to get operation for max date. Hope this query will solve your problem.
aggregate([
{
$unwind: "$contactOperations"
},
{
$sort: {
"date": 1
}
},
{
$group: {
"_id": {
"accountId": "$accountId",
"contactId": "$contactOperations.contactId"
},
"date": {
$max: "$contactOperations.date"
},
"operationId": {
$last: "$contactOperations.operation"
}
}
}
])

MongoDB $ne in sub documents

{
"_id" : ObjectId("53692eb238ed04c824679f18"),
"firstUserId" : 1,
"secondUserId" : 17,
"messages" : [
{
"_id" : ObjectId("5369338997b964b81d579fc6"),
"read" : true,
"dateTime" : 1399403401,
"message" : "d",
"userId" : 1
},
{
"_id" : ObjectId("536933c797b964b81d579fc7"),
"read" : false,
"dateTime" : 1399403463,
"message" : "asdf",
"userId" : 17
}
]
}
I'm trying to select all documents that have firstUserId = 1 and also have sub documents
that have userId differnet ($ne) to 1 and read = false.
I tried:
db.usermessages.find({firstUserId: 1, "messages.userId": {$ne: 1}, "messages.read": false})
But it returns empty cause messages have both 1 and 17.
And also how to count subdocuments that have given case?
Are you trying to get the count of all the documents which are returned after your match criteria? If Yes, then you might consider using aggregation framework. http://docs.mongodb.org/manual/aggregation/
Something like below could be done to get you the counts:
db.usermessages.aggregate(
{ "$unwind": "$messages" },
{ "$match":
{ "firstUserId": 1,
"messages.userId": { "$ne" : 1},
"messages.read": false
}
},
{ "$group": { "_id" :null, "count" : { "$sum": 1 } } }
)
Hope this helps.
PS: I have not tried this on my system.