Use field value as key - mongodb

I am doing this query
db.analytics.aggregate([
{
$match: {"event":"USER_SENTIMENT"}
},
{ $group: {
_id: {brand:"$data.brandId",sentiment:"$data.sentiment"},
count: {$sum : 1}
}
},
{ $group: {
_id: "$_id.brand",
sentiments: {$addToSet : {sentiment:"$_id.sentiment", count:"$count"}}
}
}
])
Which generates that :
{
"result" : [
{
"_id" : 57,
"sentiments" : [
{
"sentiment" : "Meh",
"count" : 4
}
]
},
{
"_id" : 376,
"sentiments" : [
{
"sentiment" : "Meh",
"count" : 1
},
{
"sentiment" : "Happy",
"count" : 1
},
{
"sentiment" : "Confused",
"count" : 1
}
]
}
],
"ok" : 1
}
But What I want is that :
[
{
"_id" : 57,
"Meh" : 4
},
{
"_id" : 376,
"Meh" : 1,
"Happy" : 1,
"Confused" : 1
}
]
Any idea on how to transform that? The blocking point for me is to transform a value into a key.

Related

MongoDB count distinct items

I have following query on a list with this fields : key,time,p,email
use app_db;
db.getCollection("app_log").aggregate(
[
{
"$match" : {
"key" : "login"
}
},
{
"$group" : {
"_id" : {
"$substr" : [
"$time",
0.0,
10.0
]
},
"total" : {
"$sum" : "$p"
},
"count" : {
"$sum" : 1.0
}
}
}
]
);
and the output is something like this :
{
"_id" : "2019-08-25",
"total" : NumberInt(623),
"count" : 400.0
}
{
"_id" : "2019-08-24",
"total" : NumberInt(2195),
"count" : 1963.0
}
{
"_id" : "2019-08-23",
"total" : NumberInt(1294),
"count" : 1706.0
}
{
"_id" : "2019-08-22",
"total" : NumberInt(53),
"count" : 1302.0
}
But I need the count to be distinctive on email field, which is count number of distinct email addresses who logged in per day and their p value is greater 0
You need $addToSet to get an array of unique email values per day and then you can use $size to count the number of items in that array:
db.getCollection("app_log").aggregate(
[
{
"$match" : {
"key" : "login"
}
},
{
"$group" : {
"_id" : {
"$substr" : [
"$time",
0.0,
10.0
]
},
"total" : {
"$sum" : "$p"
},
"emails" : {
"$addToSet": "$email"
}
}
},
{
$project: {
_id: 1,
total: 1,
countDistinct: { $size: "$emails" }
}
}
]
);

how to apply $setunion for this aggregation?

Here my db is:
{
"_id" : ObjectId("5d28667fb0adb622b905ccd2"),
"requestedDate" : ISODate("2019-07-12T10:52:47.711Z"),
"requestType" : "A",
"isRequestSuccess" : false,
"responseDate" : ISODate("2019-07-12T10:53:19.213Z"),
"__v" : 0
},
{
"_id" : ObjectId("5d28667fb0adb622b905ccd2"),
"requestedDate" : ISODate("2019-07-12T10:52:47.711Z"),
"requestType" : "C",
"isRequestSuccess" : false,
"responseDate" : ISODate("2019-07-12T10:53:19.213Z"),
"__v" : 0
},
{
"_id" : ObjectId("5d28667fb0adb622b905ccd2"),
"requestedDate" : ISODate("2019-07-12T10:52:47.711Z"),
"requestedType" : "A",
"isRequestSuccess" : false,
"responseDate" : ISODate("2019-07-12T10:53:19.213Z"),
"__v" : 0
}
I need to get the values by each requestType and also isRequestSuccess: sucess or failure wise.
[
{ requestedType: "A", isRequestSuccess: 2, isRequestFalse: 1 },
{ requestedType: "C", isRequestSuccess: 1, isRequestFalse: 3 }
]
How can I get those values?
db.getCollection('test').aggregate({
"$group": {
"_id": "$requestedType",
"isRequestSuccess": {
$sum: {
$cond: ["$isRequestSuccess", 1, 0]
}
},
"isRequestFalse": {
$sum: {
$cond: ["$isRequestSuccess", 0, 1]
}
}
}
})

Mongo 3.6: Removing Duplicates

Using the following command I get all the duplicate values in my collection based on the "no" field:
db.books.aggregate([{ $group: { _id: {no: "$no"}, uniqueIds: {$addToSet: "$_id"}, count: {$sum: 1}}}, { $match: { count: {"$gt": 1}}}, { $sort: {count: -1}}])
This returns the following:
{ "_id" : { "no" : 160541455 }, "uniqueIds" : [ ObjectId("5b3a3a3bd6194c5bfe40a523"), ObjectId("5b3a345dd6194c5bfe3deefa"), ObjectId("5b3a3509d6194c5bfe3e2e29"), ObjectId("5b3a3440d6194c5bfe3de3b4"), ObjectId("5b3a336ad6194c5bfe3d80a9"), ObjectId("5b3a3600d6194c5bfe3eb073"), ObjectId("5b3a37e1d6194c5bfe3f96f0"), ObjectId("5b3a35aed6194c5bfe3e8b83"), ObjectId("5b3a36ead6194c5bfe3f2dbd"), ObjectId("5b3a3ea6d6194c5bfe42d794"), ObjectId("5b3a3731d6194c5bfe3f50d2"), ObjectId("5b3a399bd6194c5bfe40676c") ], "count" : 12 }
{ "_id" : { "no" : 105900593 }, "uniqueIds" : [ ObjectId("5b3a3efad6194c5bfe42fc1f"), ObjectId("5b3a3df3d6194c5bfe429191"), ObjectId("5b3a3697d6194c5bfe3f0ca9"), ObjectId("5b3a385fd6194c5bfe3fdced"), ObjectId("5b3a3b69d6194c5bfe416901"), ObjectId("5b3a347ed6194c5bfe3dfeff"), ObjectId("5b3a335ad6194c5bfe3d7c15"), ObjectId("5b3a3887d6194c5bfe3feaad"), ObjectId("5b3a38b8d6194c5bfe400088"), ObjectId("5b3a358dd6194c5bfe3e81ea"), ObjectId("5b3a3b3fd6194c5bfe4157bc"), ObjectId("5b3a3decd6194c5bfe428efa") ], "count" : 12 }
{ "_id" : { "no" : 144919593 }, "uniqueIds" : [ ObjectId("5b3a3e46d6194c5bfe42b277"), ObjectId("5b3a3c22d6194c5bfe41b05a"), ObjectId("5b3a398cd6194c5bfe4065be"), ObjectId("5b3a37a1d6194c5bfe3f8239"), ObjectId("5b3a3678d6194c5bfe3f0496"), ObjectId("5b3a33d2d6194c5bfe3db42c"), ObjectId("5b3a36e6d6194c5bfe3f2d4d"), ObjectId("5b3a3d05d6194c5bfe421e97"), ObjectId("5b3a384ad6194c5bfe3fd621"), ObjectId("5b3a3565d6194c5bfe3e6c44") ], "count" : 10 }
{ "_id" : { "no" : 86204442 }, "uniqueIds" : [ ObjectId("5b3a3df5d6194c5bfe429204"), ObjectId("5b3a3d1cd6194c5bfe4224a1"), ObjectId("5b3a3b33d6194c5bfe415288"), ObjectId("5b3a3934d6194c5bfe403ed9"), ObjectId("5b3a3c39d6194c5bfe41b664"), ObjectId("5b3a37dbd6194c5bfe3f961c"), ObjectId("5b3a3688d6194c5bfe3f0613"), ObjectId("5b3a3731d6194c5bfe3f50c6"), ObjectId("5b3a37dfd6194c5bfe3f9697") ], "count" : 9 }
{ "_id" : { "no" : 119417424 }, "uniqueIds" : [ ObjectId("5b3a3b34d6194c5bfe4152a5"), ObjectId("5b3a392cd6194c5bfe403b21"), ObjectId("5b3a399ad6194c5bfe406736"), ObjectId("5b3a38b9d6194c5bfe4000c7"), ObjectId("5b3a3ea5d6194c5bfe42d77f"), ObjectId("5b3a374cd6194c5bfe3f5c9f"), ObjectId("5b3a33d9d6194c5bfe3db4e3"), ObjectId("5b3a3386d6194c5bfe3d8ab8"), ObjectId("5b3a3857d6194c5bfe3fd754") ], "count" : 9 }
{ "_id" : { "no" : 159374794 }, "uniqueIds" : [ ObjectId("5b3a3e52d6194c5bfe42b3b8"), ObjectId("5b3a3c0fd6194c5bfe41ac10"), ObjectId("5b3a38f5d6194c5bfe401b2f"), ObjectId("5b3a37afd6194c5bfe3f83b7"), ObjectId("5b3a33ebd6194c5bfe3dbc27"), ObjectId("5b3a36edd6194c5bfe3f2e78"), ObjectId("5b3a34ced6194c5bfe3e1b24"), ObjectId("5b3a3b36d6194c5bfe41530d"), ObjectId("5b3a3753d6194c5bfe3f5f99") ], "count" : 9 }
{ "_id" : { "no" : 159363089 }, "uniqueIds" : [ ObjectId("5b3a3e4fd6194c5bfe42b340"), ObjectId("5b3a3e0ad6194c5bfe429a1f"), ObjectId("5b3a3854d6194c5bfe3fd708"), ObjectId("5b3a34cbd6194c5bfe3e1acb"), ObjectId("5b3a38f4d6194c5bfe401afb"), ObjectId("5b3a36c4d6194c5bfe3f20aa"), ObjectId("5b3a3a06d6194c5bfe409119"), ObjectId("5b3a36ecd6194c5bfe3f2e2b"), ObjectId("5b3a3764d6194c5bfe3f6439") ], "count" : 9 }
{ "_id" : { "no" : 101412948 }, "uniqueIds" : [ ObjectId("5b3a3ea0d6194c5bfe42d708"), ObjectId("5b3a37b7d6194c5bfe3f8749"), ObjectId("5b3a3712d6194c5bfe3f4510"), ObjectId("5b3a334ed6194c5bfe3d759b"), ObjectId("5b3a3929d6194c5bfe403aca"), ObjectId("5b3a36f3d6194c5bfe3f334d"), ObjectId("5b3a39bad6194c5bfe407541"), ObjectId("5b3a3477d6194c5bfe3df66b"), ObjectId("5b3a35bdd6194c5bfe3e9233") ], "count" : 9 }
{ "_id" : { "no" : 107412155 }, "uniqueIds" : [ ObjectId("5b3a3e52d6194c5bfe42b3d1"), ObjectId("5b3a3a3bd6194c5bfe40a53a"), ObjectId("5b3a399cd6194c5bfe40678d"), ObjectId("5b3a390fd6194c5bfe402698"), ObjectId("5b3a3c0fd6194c5bfe41ac28"), ObjectId("5b3a346ad6194c5bfe3df338"), ObjectId("5b3a3584d6194c5bfe3e718a"), ObjectId("5b3a373cd6194c5bfe3f5563"), ObjectId("5b3a37e5d6194c5bfe3f97d6") ], "count" : 9 }
{ "_id" : { "no" : 156426829 }, "uniqueIds" : [ ObjectId("5b3a3cc6d6194c5bfe421829"), ObjectId("5b3a3f01d6194c5bfe430069"), ObjectId("5b3a3a29d6194c5bfe40a378"), ObjectId("5b3a37c9d6194c5bfe3f8fdc"), ObjectId("5b3a3558d6194c5bfe3e6b19"), ObjectId("5b3a3dabd6194c5bfe428666"), ObjectId("5b3a366ad6194c5bfe3f0356"), ObjectId("5b3a36e3d6194c5bfe3f2d02"), ObjectId("5b3a3798d6194c5bfe3f8160") ], "count" : 9 }
{ "_id" : { "no" : 104966976 }, "uniqueIds" : [ ObjectId("5b3a3c35d6194c5bfe41b614"), ObjectId("5b3a3a04d6194c5bfe4090e9"), ObjectId("5b3a3d19d6194c5bfe422451"), ObjectId("5b3a37a7d6194c5bfe3f82bc"), ObjectId("5b3a348cd6194c5bfe3e034c"), ObjectId("5b3a3e58d6194c5bfe42b5d2"), ObjectId("5b3a345cd6194c5bfe3deee2"), ObjectId("5b3a35aad6194c5bfe3e8b15"), ObjectId("5b3a36c3d6194c5bfe3f208b") ], "count" : 9 }
{ "_id" : { "no" : 105460396 }, "uniqueIds" : [ ObjectId("5b3a3e04d6194c5bfe429777"), ObjectId("5b3a3b99d6194c5bfe417eb6"), ObjectId("5b3a38c7d6194c5bfe400533"), ObjectId("5b3a3e6ed6194c5bfe42bbc6"), ObjectId("5b3a38f5d6194c5bfe401b1d"), ObjectId("5b3a3ee8d6194c5bfe42f43f"), ObjectId("5b3a39a5d6194c5bfe4069ec"), ObjectId("5b3a3a07d6194c5bfe409134") ], "count" : 8 }
{ "_id" : { "no" : 158805980 }, "uniqueIds" : [ ObjectId("5b3a3efad6194c5bfe42fc7c"), ObjectId("5b3a3abed6194c5bfe41470a"), ObjectId("5b3a337cd6194c5bfe3d89c3"), ObjectId("5b3a33bdd6194c5bfe3db218"), ObjectId("5b3a3dfcd6194c5bfe42955f"), ObjectId("5b3a3be7d6194c5bfe41a820"), ObjectId("5b3a3444d6194c5bfe3de564"), ObjectId("5b3a391fd6194c5bfe4039d5") ], "count" : 8 }
{ "_id" : { "no" : 107411546 }, "uniqueIds" : [ ObjectId("5b3a3c0fd6194c5bfe41ac2a"), ObjectId("5b3a3b37d6194c5bfe415337"), ObjectId("5b3a390fd6194c5bfe402699"), ObjectId("5b3a399cd6194c5bfe406793"), ObjectId("5b3a373cd6194c5bfe3f5565"), ObjectId("5b3a346ad6194c5bfe3df339"), ObjectId("5b3a3584d6194c5bfe3e718b"), ObjectId("5b3a37e5d6194c5bfe3f97d7") ], "count" : 8 }
{ "_id" : { "no" : 128015326 }, "uniqueIds" : [ ObjectId("5b3a3ed1d6194c5bfe42f1a3"), ObjectId("5b3a3df4d6194c5bfe4291f9"), ObjectId("5b3a38bed6194c5bfe400437"), ObjectId("5b3a381dd6194c5bfe3fd124"), ObjectId("5b3a3465d6194c5bfe3df1a7"), ObjectId("5b3a36cdd6194c5bfe3f2569"), ObjectId("5b3a3719d6194c5bfe3f4637"), ObjectId("5b3a377bd6194c5bfe3f7dfa") ], "count" : 8 }
{ "_id" : { "no" : 123932233 }, "uniqueIds" : [ ObjectId("5b3a3df3d6194c5bfe429177"), ObjectId("5b3a3d1dd6194c5bfe4224be"), ObjectId("5b3a3c39d6194c5bfe41b681"), ObjectId("5b3a3c0fd6194c5bfe41ac0d"), ObjectId("5b3a34e2d6194c5bfe3e22e6"), ObjectId("5b3a349fd6194c5bfe3e077d"), ObjectId("5b3a35aed6194c5bfe3e8b86"), ObjectId("5b3a3886d6194c5bfe3fea0f") ], "count" : 8 }
{ "_id" : { "no" : 150567857 }, "uniqueIds" : [ ObjectId("5b3a3a97d6194c5bfe413812"), ObjectId("5b3a39d9d6194c5bfe408888"), ObjectId("5b3a3816d6194c5bfe3fcd1b"), ObjectId("5b3a3635d6194c5bfe3ef80f"), ObjectId("5b3a3480d6194c5bfe3e011d"), ObjectId("5b3a34ead6194c5bfe3e2661"), ObjectId("5b3a33f8d6194c5bfe3dc477"), ObjectId("5b3a3529d6194c5bfe3e616a") ], "count" : 8 }
{ "_id" : { "no" : 143815726 }, "uniqueIds" : [ ObjectId("5b3a3b46d6194c5bfe415985"), ObjectId("5b3a3a08d6194c5bfe409172"), ObjectId("5b3a39a5d6194c5bfe4069fd"), ObjectId("5b3a33dad6194c5bfe3db527"), ObjectId("5b3a399cd6194c5bfe4067e7"), ObjectId("5b3a348fd6194c5bfe3e03a3"), ObjectId("5b3a37bcd6194c5bfe3f89c6"), ObjectId("5b3a36b7d6194c5bfe3f1cb9") ], "count" : 8 }
{ "_id" : { "no" : 143839331 }, "uniqueIds" : [ ObjectId("5b3a3ee8d6194c5bfe42f43a"), ObjectId("5b3a3e6ed6194c5bfe42bbbd"), ObjectId("5b3a3e04d6194c5bfe429773"), ObjectId("5b3a38c7d6194c5bfe40052f"), ObjectId("5b3a3a07d6194c5bfe40912e"), ObjectId("5b3a38f5d6194c5bfe401b19"), ObjectId("5b3a3b99d6194c5bfe417eb0"), ObjectId("5b3a39a5d6194c5bfe4069e8") ], "count" : 8 }
{ "_id" : { "no" : 99764925 }, "uniqueIds" : [ ObjectId("5b3a39f7d6194c5bfe408db1"), ObjectId("5b3a39c0d6194c5bfe4075eb"), ObjectId("5b3a399cd6194c5bfe40678a"), ObjectId("5b3a37bcd6194c5bfe3f89bf"), ObjectId("5b3a368ad6194c5bfe3f0673"), ObjectId("5b3a348fd6194c5bfe3e0391"), ObjectId("5b3a3578d6194c5bfe3e6e39"), ObjectId("5b3a36b7d6194c5bfe3f1ca8") ], "count" : 8 }
Question: What is the easiest and quickest way to remove the duplicates in Mongo 3.6.5? How must I edit the above command to achieve this?
I have tried the following but it doesn't work:
db.books.aggregate([ { $group:{ _id: {no: "$no" }, uniqueIds: { $addToSet: "$_id" }, count: {$sum: 1} } }, { $match: {count: {$gt: 1} } } ]).forEach(function(doc){ doc.uniqueIds.shift(); db.uniqueIds.remove({_id : {$in: doc.uniqueIds}});})
Mongo-3.6: Here ReviewId is my Unique Key, want to Delete my duplicate reviewId value
cursor=db.reviewsAnalysed.aggregate([{"$group": {"_id": {"reviewId":"$reviewId"},
"unique_ids": {"$addToSet": "$_id"}, "count": {"$sum": 1 } } }])
response = []
for doc in cursor:
del doc["unique_ids"][0]
for id in doc["unique_ids"]:
response.append(id)
db.reviewsAnalysed.remove({"_id": {"$in": response}})

How to count from two fields in mongoDB

{
"_id" : ObjectId("56bd8e9de517259412a743ab"),
"user_token" : "mzXhdbCu",
"sender_details" : {
"location" : "XYZ",
"zipcode" : "610208"
},
"shipping_address" : {
"location" : "ABC",
"zipcode" : "602578
}
}
I have been trying to count the number of instances of each unique zipcode from both
$sender_details.zipcode
and
$shipping_address.zipcode
I tried to use the following code
db.ac_consignments.aggregate({
$group: {
_id: {
"zipcode":"$sender_details.zipcode",
"szipcode":"$shipping_address.zipcode"
},
count: {"$sum":1}
}
})
The output I receive is this
{
"result" : [
{
"_id" : {
"zipcode" : "610208",
"szipcode" : "602578"
},
"count" : 7
},
{
"_id" : {
"zipcode" : "602578",
"szipcode" : "678705"
},
"count" : 51
}
],
"ok" : 1
}
But what I require is the count of each zipcode present in $sender_details.zipcode and $shipping_address.zipcode totally. So an output like this
{
"result" : [
{
"_id" : {
"zipcode" : "610208",
},
"count" : 7
},
{
"_id" : {
"zipcode" : "602578"
},
"count" : 51
}
{
"_id" : {
"zipcode" : "678705"
},
"count" : 51
}
],
"ok" : 1
}
The following pipeline should work for you
db.getCollection('ac_consignments').aggregate([
{
$project: {
zipcode: [ "$sender_details.zipcode", "$shipping_address.zipcode" ]
}
},
{
$unwind: "$zipcode"
},
{
$group: {
_id: "$zipcode",
count: { $sum: 1 }
}
}
])
which produces output like this
/* 1 */
{
"_id" : "610208",
"count" : 1.0
}
/* 2 */
{
"_id" : "610209",
"count" : 2.0
}
/* 3 */
{
"_id" : "602578",
"count" : 1.0
}
/* 4 */
{
"_id" : "602579",
"count" : 2.0
}
when using the following as sample data
/* 1 */
{
"_id" : ObjectId("56bd8e9de517259412a743ab"),
"user_token" : "mzXhdbCu",
"sender_details" : {
"location" : "XYZ",
"zipcode" : "610208"
},
"shipping_address" : {
"location" : "ABC",
"zipcode" : "602578"
}
}
/* 2 */
{
"_id" : ObjectId("56bd8e9de517259412a743ac"),
"user_token" : "mzXhdbCu",
"sender_details" : {
"location" : "XYZ",
"zipcode" : "610209"
},
"shipping_address" : {
"location" : "ABC",
"zipcode" : "602579"
}
}
/* 3 */
{
"_id" : ObjectId("56bd8e9de517259412a753ac"),
"user_token" : "mzXhdbCu",
"sender_details" : {
"location" : "XYZ",
"zipcode" : "610209"
},
"shipping_address" : {
"location" : "ABC",
"zipcode" : "602579"
}
}
See the following GIF
Update for older versions
db.getCollection('ac_consignments').aggregate([
{
$project: {
sender_zip: "$sender_details.zipcode",
shipping_zip: "$shipping_address.zipcode",
party: { $literal: ["sender_zip", "shipping_zip"] }
}
},
{
$unwind: "$party"
},
{
$group: {
_id: "$_id",
zipcode: {
$push: {
$cond: [
{ $eq: ["$party", "sender_zip"] },
"$sender_zip",
"$shipping_zip"
]
}
}
}
},
{
$unwind: "$zipcode"
},
{
$group: {
_id: "$zipcode",
count: { $sum: 1 }
}
}
])

Mongo aggregation $subtract between dynamic document value

Need to find the difference between two values of attendance,group by ward_id, based on patient id for two dates. The result has dynamic values based on the array. The difference is between two dates. Key would be ward_id, the difference will be between counts of patient's visit to the ward.
Example sample data
{
"_id" : {
"type" : "patient_attendence",
"ts" : ISODate("2015-02-03T21:31:29.902Z"),
"ward_id" : 2561
},
"count" : 4112,
"values" : [
{
"count" : 9,
"patient" : ObjectId("54766f973f35473ffc644618")
},
{
"count" : 19,
"patient" : ObjectId("546680e2d660e2dc5ebfea39")
},
{
"count" : 47,
"patient" : ObjectId("546680e3d660e2dc5ebfea72")
},
{
"count" : 1,
"patient" : ObjectId("546a137bdab5f21e612ea7ef")
},
{
"count" : 93,
"patient" : ObjectId("546680e3d660e2dc5ebfea89")
}
]
}
{
"_id" : {
"type" : "patient_attendence",
"ts" : ISODate("2015-02-03T21:31:29.902Z"),
"ward_id" : 3720
},
"count" : 1,
"values" : [
{
"count" : 1,
"patient" : ObjectId("546a136ddab5f21e612ea6a6")
}
]
}
{
"_id" : {
"type" : "patient_attendence",
"ts" : ISODate("2015-02-04T21:31:29.902Z"),
"ward_id" : 2561
},
"count" : 4112,
"values" : [
{
"count" : 10,
"patient" : ObjectId("54766f973f35473ffc644618")
},
{
"count" : 10,
"patient" : ObjectId("546680e2d660e2dc5ebfea39")
},
{
"count" : 6,
"patient" : ObjectId("5474e9e46606f32570fa48ff")
},
{
"count" : 1,
"patient" : ObjectId("5474e9e36606f32570fa48f2")
},
{
"count" : 1,
"patient" : ObjectId("546680e3d660e2dc5ebfea77")
},
{
"count" : 543,
"patient" : ObjectId("546680e2d660e2dc5ebfea43")
},
{
"count" : 1,
"patient" : ObjectId("5485fdc8d27a9122956b1c66")
}
]
}
{
"_id" : {
"type" : "patient_attendence",
"ts" : ISODate("2015-02-04T21:31:29.902Z"),
"ward_id" : 3720
},
"count" : 1,
"values" : [
{
"count" : 7,
"patient" : ObjectId("546a136ddab5f21e612ea6a6")
}
]
}
Output
{
"ward_id":2561,
"result" : [{"person": ObjectId("54766f973f35473ffc644618"),
"count_1": 9,
"count_1": 10,
"difference":1 },{"person": ObjectId("546680e2d660e2dc5ebfea39"),
"count_1": 19,
"count_1": 10,
"difference":-9 } ....]
},
{
"ward_id":3720,
"result" : [{"person": ObjectId("546a136ddab5f21e612ea6a6"),
"count_1": 9,
"count_1": 10,
"difference":1 },{"person": ObjectId("546680e2d660e2dc5ebfea39"),
"count_1": 1,
"count_1": 7,
"difference":-6 }]
}
you can use the aggregation framework's $subtract operator outlined here: http://docs.mongodb.org/manual/reference/operator/aggregation-arithmetic/
db.wards.aggregate([
{
$match: {id: {$elemMatch: {ward_id: my_ward_id, ts: my_desired_ts}}},
},
{
$limit: 2
},
{
$project: {values: 1}
},
{
$unwind: '$values'
},
{
$match: {patient: my_patient_id}
},
{
$group: {
_id: null,
'count1': {$first: '$values.count'},
'count2': {$last: '$values.count'}
}
},
{
$subtract: ['$count1', '$count2']
}
])
i haven't tested this but it would probably look like something above