MongoDB - Field not showing in Aggregate - mongodb

Query:
db.environments.aggregate(
[{$match:{customer:"CustomerName"}},
{$group:{_id:{environment: "$environment",
cpu_oversubscription:"$cpu_oversubscription",
memory_oversubscription:"$memory_oversubscription"}}} ])
When I run the query only the environment and the CPU_oversubcription fields are shown, the memory field is not shown.
This is the output I get:
[{"_id":{"environment":"V&B","cpu_oversubscription":1}}
When I run db.environments.find() I see all the fields
[{"_id":{"environment":"V&B","cpu_oversubscription":1,"memory_oversubscription":1,"customer":"CustomerName"}}
What is wrong with my query?
Edit: Exact Query
Environment.aggregate([{$match:{customer:req.query.customer}},{$group:{_id:{environment: "$environment",cpu_oversubscription:"$cpu_oversubscription",memory_oversubscription:"$memory_oversubscription",}}} ])
Exact Result of Query
{ "_id" : { "environment" : "1", "cpu_oversubscription" : 1 } }
{ "_id" : { "environment" : "2", "cpu_oversubscription" : 4 } }
{ "_id" : { "environment" : "3", "cpu_oversubscription" : 4 } }
{ "_id" : { "environment" : "4", "cpu_oversubscription" : 4 } }
{ "_id" : { "environment" : "5", "cpu_oversubscription" : 4 } }
{ "_id" : { "environment" : "6", "cpu_oversubscription" : 1 } }
{ "_id" : { "environment" : "7", "cpu_oversubscription" : 1 } }
{ "_id" : { "environment" : "8", "cpu_oversubscription" : 1 } }
{ "_id" : { "environment" : "9", "cpu_oversubscription" : 4 } }
{ "_id" : { "environment" : "10", "cpu_oversubscription" : 4 } }
Exact result of db.environments.find() (1st result only)
{ "_id" : ObjectId("560a5139b56a71ea60890201"), "customer" : "CustomerName", "environment" : "1", "memory_oversubcription" : 1, "cpu_oversubscription" : 1 }

As #Yogesh said, you have a typo in your field in your data. This solve your problem.
Nevertheless, you do not need the aggregation framework for this query :
A find with a projection would be much more efficient :
db.environments.find({ "customer" : "CustomerName" },
{ _id : 0, "environment" : 1,
"memory_oversubscription" : 1,
"cpu_oversubscription" : 1
})
Result would be :
{
"environment" : "1",
"memory_oversubscription" : 1,
"cpu_oversubscription" : 1
}
If you really want the result under the _id field for some reasons, a $project whould be much better :
db.environments.aggregate([
{$match:{"customer" : "CustomerName"}},
{$project:
{_id :
{"environment" : "$environment",
"memory_oversubscription" : "$memory_oversubscription",
"cpu_oversubscription" : "$cpu_oversubscription"}
}
}
]);
Result :
{ "_id" : { "environment" : "1",
"memory_oversubscription" : 1,
"cpu_oversubscription" : 1 }
}
Doesn't make sense to me, but maybe it does for you :-).

Related

Mongodb query to group by multiple fields and filter

I want to be able to group each "Place" to show over time, how many "PatientIds" they are seeing on a given day and then be able to filter this by what the action is.
Basically Total Patients on y-axis, Date on x-axis and then a filter or stacked chart to show the action. I also thought about a mapreduce, but have never done that in mongo
I can't figure out the correct mongo query. Right now I have:
db.collection.aggregate({"$group":{_id:{place:"$place",date:"$date",action:"$action",count:{$sum:1}}},{$sort:{"_id.date":1,"_id.place":1}})
However, this is just listing out the data. I tried to do a match on all places, but that didn't give me the results I was looking for either. Any ideas?
Example json:
{
"_id" : ObjectId(""),
"patientId" : "100",
"place" : "1",
"action" : "DIAGNOSED",
"date" : ISODate("2017-01-20")
}
{
"_id" : ObjectId(""),
"patientId" : "101",
"place" : "1",
"action" : "PATIENT IN",
"date" : ISODate("2017-01-20)
}
{
"_id" : ObjectId(""),
"patientId" : "200",
"place" : "2",
"action" : "MEDICINE",
"date" : ISODate("2017-01-05")
}
{
"_id" : ObjectId(""),
"patientId" : "300",
"place" : "2",
"action" : "DIAGNOSED",
"date" : ISODate("2017-01-31")
}
EDIT - mapreduce
> var map = function(){emit(this.place,1)}
> var reduce = function(key,values){var res = 0;values.forEach(function(v){res+=1});return{count:res};}
> db.new.mapReduce(map,reduce,{out:"mapped_places"});
{
"result" : "mapped_places",
"timeMillis" : 88,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 2
},
"ok" : 1
}
> db.mapped_offices.find({})
{ "_id" : "1", "value" : { "count" : 2 } }
{ "_id" : "2", "value" : { "count" : 2 } }
>
You can try below aggregation query.
db.collection.aggregate([
{
"$group": {
"_id": {
"date": "$date",
"place": "$place"
},
"actions": {
"$push": "$action"
},
"count": {
"$sum": 1
}
}
},
{
"$unwind": "$actions"
},
{
"$sort": {
"_id.date": 1,
"_id.place": 1
}
}
]);
This should output something like
{ "_id" : { "date" : ISODate("2017-01-20T00:00:00Z"), "place" : "1"}, "count" : 2, "actions" : "PATIENT IN" }
{ "_id" : { "date" : ISODate("2017-01-20T00:00:00Z"), "place" : "1"}, "count" : 2, "actions" : "DIAGNOSED" }

MongoDB find subdocument

I am starting a collection for aggregated results.
So I am following MO 'Pre aggegrated reports'
I am inserting docs with this schema:
{
"_id" : "20160526/78220/59",
"metadata" : {
"date" : "2016-05-26",
"offer" : "78220",
"adv" : NumberLong(59),
"update" : ISODate("2016-05-26T10:49:25.597Z")
},
"daily" : NumberLong(6),
"hourly" : {
"12" : NumberLong(6)
},
"publisher" : {
"43" : {
"daily" : NumberLong(3),
"hourly" : {
"12" : NumberLong(3)
}
},
"738" : {
"daily" : NumberLong(3),
"hourly" : {
"12" : NumberLong(3)
}
}
}
The idea is to have aggregated info from every hour from every publisher. _id is date/offer/code, and every publisher can give some offers.
But now I need to get, for example, a sum of the daily or hourly data for all publisher's offers.
My main question is how can I access a report on specific publisher, for example, 738, or 43?
If I query:
db.getCollection('daily_aggregate').find({'publisher.738':{$exists:true}})
I get all documents that has publisher 738, but I get other publishers data too. I want to retrieve data just from 738.
I am trying different approaches here, but probably I have to include pub_id inside the publisher schema in some way.
Any clue?
Thanks in advance.
Probably, what I need to query is just this:
db.getCollection('daily_aggregate').find({'publisher.738':{$exists: true}}, {_id: 0, 'publisher' : 1})
It gives me:
1
{
"publisher" : {
"738" : {
"daily" : NumberLong(4),
"hourly" : {
"18" : NumberLong(4)
}
}
}
}
2
{
"publisher" : {
"738" : {
"daily" : NumberLong(6),
"hourly" : {
"18" : NumberLong(6)
}
}
}
}
3
{
"publisher" : {
"43" : {
"daily" : NumberLong(9),
"hourly" : {
"12" : NumberLong(3),
"19" : NumberLong(6)
}
},
"738" : {
"daily" : NumberLong(3),
"hourly" : {
"12" : NumberLong(3)
}
}
}
}
4
{
"publisher" : {
"43" : {
"daily" : NumberLong(1),
"hourly" : {
"12" : NumberLong(1)
}
},
"738" : {
"daily" : NumberLong(2),
"hourly" : {
"12" : NumberLong(2)
}
}
}
}
But I am wondering if this is the best scenario.

Delete portion of Sub Document Collection in Mongodb

I am trying to delete a section of sub documents from a collections..
Tried (and many others):
db.collection.remove( {'Segments': {$gte: '20150612141038' }} )
db.collection.remove( { 'Segments.$' : {$gte: '0150612141038' }} )
db.collection.deleteMany( { 'Segments.$' : {$gte: '0150612141038' }} )
db.collection.deleteOne( { 'Segments.$' : {$eq: '0150612141038' }} )
just can't figure out what I am doing wrong...
{
"_id" : ObjectId("56e32c5147e030bc0a000035"),
"Date" : "2015-06-12",
"UnitID" : "5",
"Segments" : {
"20150612141037" : {
"Parameters" : {
"647" : "0",
"649" : "16",
"653" : "0",
"655" : "0",
"656" : "0",
"658" : "98",
"664" : "447.0486",
"666" : "442.7083",
"677" : "122.8004",
}
},
"20150612141038" : {
"Parameters" : {
"658" : "96",
"664" : "451.3889",
"666" : "447.0486",
"677" : "122.7892"
}
},
"20150612141039" : {
"Parameters" : {
"658" : "44",
"664" : "442.7083",
"677" : "122.8004",
"704" : "1"
}
},
First of all I think your queries are wrong.
If you try this query to the mongoShell:
db.bios.find({'Segments': {$gte: '20150612141038'}})
you don't have anything as output because 20150612141038 is a document and not a field. Maybe you can reshape your schema to set 20150612141038 as a field and Segments as an array of documents:
{
"_id" : ObjectId("56e32c5147e030bc0a000035"),
"Date" : "2015-06-12",
"UnitID" : "5",
"Segments" : [
{
"segmentId" : "20150612141037",
"Parameters" : {
"647" : "0",
"649" : "16",
"653" : "0",
"655" : "0",
"656" : "0",
"658" : "98",
"664" : "447.0486",
"666" : "442.7083",
"677" : "122.8004"
}
},
{
"segmentId" : "20150612141038",
"Parameters" : {
"658" : "96",
"664" : "451.3889",
"666" : "447.0486",
"677" : "122.7892"
}
},
{
"segmentId" : "20150612141039",
"Parameters" : {
"658" : "44",
"664" : "442.7083",
"677" : "122.8004",
"704" : "1"
}
}
]
}
and then you can use $pull operator to remove documents from Segments array where the field Segments.segmentId has a certain value:
db.collection.update({},
{$pull:{Segments:{"segmentId":{$eq:"20150612141039"}}}},
{ multi: true}
)

MongoDB Aggregation - return default value for documents that don't match query

I'm having trouble figuring out the right aggregation pipe operations to return the results I need.
I have a collection similar to the following :-
{
"_id" : "writer1",
"Name" : "writer1",
"Website" : "website1",
"Reviews" : [
{
"Film" : {
"Name" : "Jurassic Park",
"Genre" : "Action"
},
"Score" : 4
},
{
"Technology" : {
"Name" : "Mad Max",
"Genre" : "Action"
},
"Score" : 5
}
]
}
{
"_id" : "writer2",
"Name" : "writer2",
"Website" : "website1",
"Reviews" : [
{
"Technology" : {
"Name" : "Mad Max",
"Genre" : "Action"
},
"Score" : 5
}
]
}
And this is my aggregation so far : -
db.writers.aggregate([
{ "$unwind" : "$Reviews" },
{ "$match" : { "Reviews.Film.Name" : "Jurassic Park" } },
{ "$group" : { "_id" : "$Website" , "score" : { "$avg" : "$Reviews.Score" },
writers :{ $push: { name:"$Name", score:"$Reviews.Score" } }
}}
])
This returns only writers who have a review of the matching film and also only websites that have at least 1 writer who has reviewed the film,
however, I need to return all websites containing a list of their all writers, with a score of 0 if they haven't written a review for the specified film.
so, I am currently getting : -
{ "_id" : "website1", "score" : 4, "writers" : [ { "name" : "writer1", "score" : 4 } ] }
When I actually need : -
{ "_id" : "website1", "score" : 2, "writers" : [ { "name" : "writer1", "score" : 4 },{ "name" :"writer2", "score" : 0 } ] }
Can anyone point me in the right direction?
Cheers

MongoDB groupby query

I have colletions containing records like
{ "type" : "me", "tid" : "1" }
{ "type" : "me", "tid" : "1" }
{ "type" : "me", "tid" : "1" }
{ "type" : "you", "tid" : "1" }
{ "type" : "you", "tid" : "1" }
{ "type" : "me", "tid" : "2" }
{ "type" : "me", "tid" : "2"}
{ "type" : "you", "tid" : "2"}
{ "type" : "you", "tid" : "2" }
{ "type" : "you", "tid" : "2"}
I have want result like below
[
{"tid" : "1","me" : 3,"you": 2},
{"tid" : "2","me" : 2,"you": 3}
]
I have tried group and; aggregate queries doesn't get required result format.
below is the group query.
db.coll.group({
key: {tid : 1,type:1},
cond: { tid : { "$in" : [ "1","2"]} },
reduce: function (curr,result) {
result.total = result.total + 1
},
initial: { total : 0}
})
it result is like
[
{"tid" : "1", "type" : "me" ,"total": 3 },
{"tid" : "1","type" : "you" ,"total": 2 },
{"tid" : "2", "type" : "me" ,"total": 2 },
{"tid" : "2","type" : "you" ,"total": 3 }
]
following is aggregate query
db.coll.aggregate([
{$match : { "tid" : {"$in" : ["1","2"]}}},
{$group : { _id : {tid : "$tid",type : "$type"},total : {"$sum" : 1}}}
])
gives following result
{
"result" :
[
{"_id" : {"tid" : "1","type" : "me"},"total" : 3},
{"_id" : {"tid" : "2","type" : "me" },"total" : 2},
{"_id" : {"tid" : "2","type" : "you"},"total" : 3}
]
"ok" : 1
}
it is possible to obtain I specified result or I have to do some manipulation in my code.
Thanks
If you change your aggregation to this:
db.so.aggregate([
{ $match : { "tid" : { "$in" : ["1", "2"] } } },
{ $group : {
_id : { tid : "$tid", type : "$type" },
total : { "$sum" : 1 }
} },
{ $group : {
_id : "$_id.tid",
values: { $push: { type: "$_id.type", total: '$total' } }
} }
])
Then your output is:
{
"result" : [
{
"_id" : "1",
"values" : [
{ "type" : "you", "total" : 2 },
{ "type" : "me", "total" : 3 }
]
},
{
"_id" : "2",
"values" : [
{ "type" : "me", "total" : 2 },
{ "type" : "you", "total" : 3 }
]
}
],
"ok" : 1
}
Although that is not the same as what you want, it is going to be the closest that you can get. And in your application, you can easily pull out the values in the same was as with what you would like to get out of it.
Just keep in mind, that in general you can not promote a value (you, me) to a key — unless your key is of a limited set (3-4 items max).