ReactiveMongo aggregate function - reactivemongo

I'm trying to convert the following mongo query to Reactive Mongo Equivalent ( JSON )
db.media.aggregate( {$group : { "_id" : "$createdBy", "count" : { $sum : 1 }}}, {$sort : {"count" : -1}}, {$limit : 10} )
What I have come up with is this, but can't get around it.
override def getMediasCountByHandle(db:reactivemongo.api.DefaultDB): Future[JsObject] = {
val commandDoc = Json.obj(
"aggregate" -> "media", // we aggregate on collection orders
"pipeline" -> List(
Json.obj(
"$group" -> Json.obj(
"_id" -> "$createdBy",
"count" -> Json.obj("$sum" -> 1))),
Json.obj("$sort" -> Json.obj("total" -> -1)),
Json.obj("$limit" -> 10)
)
)
val runner = Command.run(JSONSerializationPack)
runner.apply(db, runner.rawCommand(commandDoc)).one[JsObject]
}
Please help

Related

Parse and modify JSON

I've a JSON with next structure and data:
[ {
"id" : 716612,
"type" : "ad",
"stats" : [ {
"day" : "2020-06-01",
"impressions" : 1956,
"clicks" : 1,
"reach" : 1782
},
{
"day" : "2020-06-13",
"spent" : "73.32",
"reach" : 1059
} ]
}, {
"id" : 414290,
"type" : "campaign",
"stats" : [ {
"day" : "2020-05-21",
"effective_cost_per_click" : "31.200",
"effective_cost_per_mille" : "108.337"
},
{
"day" : "2020-05-17",
"impressions" : 1,
"reach" : 1,
"ctr" : "0.000",
"uniq_views_count" : 1
} ]
} ]
I need to map id and type from top level with data inside stats to get result like this:
[ {
"id" : 716612,
"type" : "ad",
"day" : "2020-06-01",
"impressions" : 1956,
"clicks" : 1,
"reach" : 1782
},
{
"id" : 716612,
"type" : "ad",
"day" : "2020-06-13",
"spent" : "73.32",
"reach" : 1059
},
...
I tried with:
def json = new JsonSlurper().parseText(text)
def result = json.collectMany{ a ->
a["stats"].collectMany{ b ->
b.collect{
[id: a.id,
type: a.type
]
}
}
}
But it returns only id and type fields without stats. I thought that I'm looping through stat and just adding needed fields from above. I guess I don't get the difference between collectMany and collect?
You were close 😁
You want to collect the stat plus the id and type, so you need:
def result = json.collectMany { a ->
a.stats.collect { b ->
[ id: a.id, type: a.type ] + b
}
}

Arangodb AQL Joining, merging, embedding nested three collections or more

I have the following collections, based on the example Arangodb doc here but have added a third collection called region
Users
{
"name" : {
"first" : "John",
"last" : "Doe"
},
"city" : "cities/2241300989",
"_id" : "users/2290649597",
"_rev" : "2290649597",
"_key" : "2290649597"
}
Cities
{
"population" : 1000,
"name" : "Metropolis",
"region" : "regions/2282300990",
"_id" : "cities/2241300989",
"_rev" : "2241300989",
"_key" : "2241300989"
}
Regions
{
"name" : "SomeRegion1",
"_id" : "regions/2282300990",
"_rev" : "2282300990",
"_key" : "2282300990"
}
I want to have a target result like this
[
{
"user" : {
"name" : {
"first" : "John",
"last" : "Doe"
},
"_id" : "users/2290649597",
"_rev" : "2290649597",
"_key" : "2290649597"
},
"city" : {
"population" : 1000,
"name" : "Metropolis",
"_id" : "cities/2241300989",
"_rev" : "2241300989",
"_key" : "2241300989",
"region" : {
"name" : "SomeRegion1",
"_id" : "regions/2282300990",
"_rev" : "2282300990",
"_key" : "2282300990"
}
}
}
]
The example in the Arangodb doc here only has queries for two collections
FOR u IN users
FOR c IN cities
FILTER u.city == c._id RETURN merge(u, {city: c})
# However I want to have more than two collections e.g.
FOR u IN users
FOR c IN cities
For r IN regions
FILTER u.city == c._id and c.region == r._id RETURN merge(????????)
How would you get the result with three collections joined as above? What happens if I want a forth nested one?
When you store a document _id that references another collection, then you can leverage the DOCUMENT AQL command.
So your AQL query becomes a bit simpler, like this:
FOR u IN users
LET city = DOCUMENT(u.city)
LET city_with_region = MERGE(city, { region: DOCUMENT(city.region})
RETURN MERGE(u, { city: city_with_region})
This query could be collapsed even more, but I left it like this so it's more self documenting.
What is cool about DOCUMENT is you can return only a single attribute of a document, such as LET region_name = DOCUMENT(city.region).name.
I've also found that in most cases it's more performant that doing a subquery to locate the document.
Probably something like this:
FOR u IN users
FOR c IN cities
For r IN regions
FILTER u.city == c._id AND c.region == r._id
RETURN { user: u, city: MERGE(c, {region: r } }
Is there a particular reason why you store ids instead of keys to refer to cities and regions? The _id is just a virtual field that consists of the _key prefixed by the collection name (plus a slash). So this would work just as well (I intentionally omit the internal _id and _rev fields):
Users
{
"name" : {
"first" : "John",
"last" : "Doe"
},
"city" : "2241300989",
"_key" : "2290649597"
}
Cities
{
"population" : 1000,
"name" : "Metropolis",
"region" : "2282300990",
"_key" : "2241300989"
}
Regions
{
"name" : "SomeRegion1",
"_key" : "2282300990"
}
FOR u IN users
FOR c IN cities
For r IN regions
FILTER u.city == c._key AND c.region == r._key
RETURN { user: u, city: MERGE(c, {region: r } }

How to sort response in Ready API using groovy in ascending and descending order

I have gone through various articles but didn't find any simple way to sort the API response in ascending or descending order using groovy. Can someone please help on this?
import groovy.json.JsonSlurper
def inputJson = '''{
"status" : "success",
"locale" : "",
"data" : {
"periods" : [
{
"payCycleId" : "custompayperiod",
"sequence" : 1,
"cycleStartDate" : "2018-10-01",
"cycleEndDate" : "2018-10-08"
},
{
"payCycleId" : "custompayperiod",
"sequence" : 2,
"cycleStartDate" : "2018-10-09",
"cycleEndDate" : "2018-10-16"
}
]
}
}
Want to sort above response based on sequence.
There are a lot of ways.
For example:
def json = new JsonSlurper().parseText(inputJson)
//Descending
json.data.periods = json.data.periods.toSorted { a, b -> b.sequence <=> a.sequence }
//Ascending
//json.data.periods = json.data.periods.toSorted { a, b -> a.sequence <=> b.sequence }
String output = JsonOutput.prettyPrint(JsonOutput.toJson(json))

How to pull all elements from array in MongoDB without any condition

I have a document as below, and I want to pull all the elements in this array without any condition just via one statement. how can I do?
"energy_sent" : [
{
"player_id" : "034010000093",
"_id" : ObjectId("53675b8d251c20490d9679c6"),
"time" : ISODate("2014-05-05T09:36:13.629Z"),
"has_accepted" : 0,
"energy_value" : 2
},
{
"player_id" : "034010000094",
"_id" : ObjectId("53675cfa251c20490d9679cc"),
"time" : ISODate("2014-05-05T09:42:18.015Z"),
"has_accepted" : 0,
"energy_value" : 2
},
{
"player_id" : "034010000116",
"_id" : ObjectId("5367767889f8e3ee137dd239"),
"time" : ISODate("2014-05-05T11:31:04.457Z"),
"has_accepted" : 0,
"energy_value" : 2
}
]
If you are just after emptying the entire array just set it to empty:
db.collection.update(
{ /* query to match document */ },
{ "$set": { "energy_sent": [] }
)
So just use the $set operator

Getting a document with the minimum or maximum value in a field MongoDB

How do you get a document with the minimum or maximum value in a field in MongoDB?
Similar to the MIN() or MAX() functions in MySQL, but using the NodeJS MongoDB driver. Any ideas?
you can try to use asc, desc sort with limit(1)
db.col.insert({val: 9});
db.col.insert({val: 4});
db.col.insert({val: 20});
db.col.find();
{ "_id" : ObjectId("511bf79298bb011e3a33747d"), "val" : 9 }
{ "_id" : ObjectId("511bf79698bb011e3a33747e"), "val" : 4 }
{ "_id" : ObjectId("511bf79d98bb011e3a33747f"), "val" : 20 }
db.col.find().sort({val: 1}).limit(1); //min
{ "_id" : ObjectId("511bf79698bb011e3a33747e"), "val" : 4 }
db.col.find().sort({val: -1}).limit(1); //max
{ "_id" : ObjectId("511bf79d98bb011e3a33747f"), "val" : 20 }

Resources