I need to create a query using mongoose, which allows me to use the group, to count the elements that are in the STARTED or NOT INITIATED state, and then return the count of the elements that are in each of the states.
This is the json object that I am working with.
[
{
status: "INITIATED",
_id: "6057e0013a3dec1d44a7d152",
group: "dairy",
location: "001",
total_items: 30,
__v: 0
},
{
status: "INITIATED",
_id: "6057e0013a3dec1d44a7d153",
group: "dairy",
location: "002",
total_items: 45,
__v: 0
},
{
status: "INITIATED",
_id: "6057e0013a3dec1d44a7d154",
group: "dairy",
location: "003",
total_items: 12,
__v: 0
},
{
status: "NOT INITIATED",
_id: "6057e0013a3dec1d44a7d155",
group: "dairy",
location: "004",
total_items: 50,
__v: 0
},
{
status: "NOT INITIATED",
_id: "6057e0013a3dec1d44a7d156",
group: "drugs",
location: "005",
total_items: 23,
__v: 0
},
{
status: "NOT INITIATED",
_id: "6057e0013a3dec1d44a7d157",
group: "drugs",
location: "006",
total_items: 76,
__v: 0
}]
For example, from the dairy group I need to return a json with the following structure.
{
'id': null,
'group': 'dairy',
'INITIATED': 3,
'NO INITIATED': 1
}
Because of the dairy group 3 of the elements are in the STARTED state and one of the elements is in the NOT INITIATED state. Another example would be with the group of drugs that should return the following json.
{
'id': null,
'group': 'medications',
'STARTED': 0,
'NOT INITIATED': 2
}
In order to do this I am trying to get the following code to work.
db.collection.aggregate ([
{
"$ group": {
_id: {
source: "$ group",
status: "$ status"
},
count: {
$ sum: 1
}
}
}
])
But I am not very clear on how to use the group to generate the response as I need it.
Ok, so this was trickier than I thought at first, but here's how you could do it:
First, you group your documents by the group field, and sum up the counts for the status field. I'm using the $cond and $eq operators to make sure that the status matches the count field. Finally, you apply $project to get the desired output without the _id field:
db.collection.aggregate([
{ "$group": {
"_id": "$group",
"NOT INITIATED": {
"$sum": {
"$cond": [ { "$eq": [ "$status", "NOT INITIATED" ] }, 1, 0]
}
},
"INITIATED": {
"$sum": {
"$cond": [ { "$eq": [ "$status", "INITIATED" ] }, 1, 0]
}
},
} },
{ "$project": {
"_id": 0,
"group": "$_id",
"INITIATED":1,
"NOT INITIATED":1
} }])
I've created an example on mongoplayground for you: https://mongoplayground.net/p/H-72VzWoeoN
Related
I want to return an array, which is a property inside my mongo model/document, and I want that array to be sorted.
My MongoDB document looks like:
_id: ObjectID("6248e49c88ff07aedee8c000")
title: "School"
items: [
{
sort: 2,
name: "homework"
},
{
sort: 1,
name: "exam"
},
{
sort: 3,
name: "essay"
},
]
And I'm trying to return:
items: [
{
sort: 1,
name: "exam"
},
{
sort: 2,
name: "homework"
},
{
sort: 3,
name: "essay"
}
]
I have tried aggregation:
app.get("/api/v1/lists/:id", async (req,res) =>{
List.aggregate([{
"$match" :{"_id": req.params.id}
},{
"$unwind" : "$items"
} , {
"$sort" : {"sort": 1}
}
], (err, items)=>{
res.json(items)
})
}
Mongo Playground reference
Since $unwind returns the arrays as objects, we are using the $group to push the objects back into the items array
db.collection.aggregate([
{
$unwind: "$items"
},
{
$sort: {
"items.sort": 1
}
},
{
$group: {
_id: "$_id",
items: {
$push: "$items"
}
}
},
])
Output -
[
{
"_id": 1.212112e+06,
"items": [
{
"name": "exam",
"sort": 1
},
{
"name": "homework",
"sort": 2
},
{
"name": "essay",
"sort": 3
}
]
}
]
I have below schema
{
id: 123,
values:[
{valueId: "12444", name: "asd"},
{valueId: "555", name: "www"},
]
}
i want to convert it into (combine name into single string)
{
id: 123,
values: "asdwww"
}
i have tried below aggregate which puts all name value in an array
$project: {
attributes: {
"$map": {
"input": "$attributes",
"as": "attr",
"in": {
"id": "$$attr.id",
"values": "$$attr.values.name"
}
}
}
},
which makes it into
{
id: 123,
values:[
"asd",
"www"
]
}
i want to have values as single string value as "asd,www" or "asdwww"
You need $reduce instead of $map:
db.collection.aggregate([
{
$project: {
_id: 1,
values: {
$reduce: {
input: "$values",
initialValue: "",
in: { $concat: [ "$$value", "$$this.name" ] }
}
}
}
}
])
Mongo Playground
Here's an example which shows how to handle delimiters
I'm creating an app in which I want to include chat. The last thing that I have to do is showing last message from each conversation (including those sent by me).
My Db looks like this
[
{
_id: 5f9b06cdb5d5eb3b94f066e3
from: "5f860655e3fc7f43709cc408"
to: "5f8b0a941efeb42a24f1f926"
message: "123"
createdAt: 2020-10-29T18:15:41.424+00:00
updatedAt: 2020-10-29T18:15:41.424+00:00
__v: 0
},
{
_id: 5f9b0761b5d5eb3b94f066e5
from: "5f860655e3fc7f43709cc408"
to: "5f8b0a941efeb42a24f1f926"
message: "321"
createdAt: 2020-10-29T18:15:41.424+00:00
updatedAt: 2020-10-29T18:15:41.424+00:00
__v: 0
}
]
Results that I want to achieve:
[
{
message: '123',
createdAt: 2020-10-29T18:15:41.424+00:00,
from: "5f860655e3fc7f43709cc408"
}
]
Chat.findOne({_id: 0, message: 1, createdAt: 1, from: 1}).sort({createdAt: -1})
You need something like this I think:
db.collection.aggregate([
{
"$sort": {
"createdAt": -1
}
},
{
"$project": {
"_id": 0,
"message": 1,
"createdAt": 1,
"from": 1
}
},
{
"$limit": 1
}
])
sort is to sort by date. In this case, the newest date first.
project is to use only these fields.
limit to get only the first.
Mongo PlayGround example here
Edit:
If you want to query by conversation (with your model I don't know how to do that...) you can use $match and something like: $match {"id_conver": "..."}
For example, querying matching "from" and "to".
db.collection.aggregate([
{
"$match": {
"from": "5f860655e3fc7f43709cc408",
"to": "5f8b0a941efeb42a24f1f926"
}
},
{
"$sort": {
"createdAt": -1
}
},
{
"$project": {
"_id": 0,
"message": 1,
"createdAt": 1,
"from": 1
}
},
{
"$limit": 1
}
])
Im currently having an almost working aggregate query, that would get the users array, and order the objects there by their score.
But not getting the expected output, for some reason the entire family data is beeing printed again.
How do i fix this?
Executed code:
return Family.aggregate([
// Initial document match (uses index, if a suitable one is available)
{ $match: {name: 'Management'}},
// Expand the scores array into a stream of documents
{ $unwind: '$users' },
// Sort in descending order
{ $sort: {
'users.score': -1
}}]
Current result:
{ _id: 5c8e5c79e55ef42ce4923e0b,
name: 'Management',
time_started: 1552833657354,
location: 1,
isFamily: true,
last_member: 0,
score: 0,
users:
{ userid: '5c852292d1bd911abc4957dc',
joined_date: 1552839246371,
permission: 5,
upgrade: 0,
score: 141,
_id: 5c8e724e6e5e6512447c1a61 },
__v: 0 },
{ _id: 5c8e5c79e55ef42ce4923e0b,
name: 'Management',
time_started: 1552833657354,
location: 1,
isFamily: true,
last_member: 0,
score: 0,
users:
{ userid: '5c8522a96bcca9268c0753fe',
joined_date: 1552833657354,
permission: 6,
upgrade: 0,
score: 32,
_id: 5c8e5c79e55ef42ce4923e0c },
__v: 0 } ]
wanted result:
{
name: 'Management',
time_started: 1552833657354,
location: 1,
isFamily: true,
last_member: 0,
score: 0,
users:
[{ userid: '5c852292d1bd911abc4957dc',
joined_date: 1552839246371,
permission: 5,
upgrade: 0,
score: 141,
_id: 5c8e724e6e5e6512447c1a61 },
__v: 0 },
{ userid: '5c8522a96bcca9268c0753fe',
joined_date: 1552833657354,
permission: 6,
upgrade: 0,
score: 32,
_id: 5c8e5c79e55ef42ce4923e0c },
__v: 0 }
]}
You need to use one more $group stage to reshape the splited array into its original form after $unwind
Family.aggregate([
{ "$match": { "name": "Management" }},
{ "$unwind": "$users" },
{ "$sort": { "users.score": -1 }},
{ "$group": {
"_id": "$_id",
"users": { "$push": "$users" },
"name": { "$first": "$name" },
"time_started": { "$first": "$time_started" },
"isFamily": { "$first": "$isFamily" },
"last_member": { "$first": "$last_member" },
"score": { "$first": "$score" },
}}
])
Assuming I have a schema that looks something like this:
{
field: [{
subDoc: ObjectId,
...
}],
...
}
and I have some list of ObjectIds (user input), how would I get a count of those specific ObjectIds? For exmaple, if I have data like this:
[
{field: [ {subDoc: 123}, {subDoc: 234} ]},
{field: [ {subDoc: 234}, {subDoc: 345} ]},
{field: [ {subDoc: 123}, {subDoc: 345}, {subDoc: 456} ]}
]
and the list of ids given by the user is 123, 234, 345, I need to get a count the given ids, so a result approximating this:
{
123: 2,
234: 2,
345: 2
}
What would be the best way to go about this?
The aggregation framework itself if not going to dynamically name keys the way you have presented as a proposed output, and that probably is a good thing really. But you can probably just do a query like this:
db.collection.aggregate([
// Match documents that contain the elements
{ "$match": {
"field.subDoc": { "$in": [123,234,345] }
}},
// De-normalize the array field content
{ "$unwind": "$field" },
// Match just the elements you want
{ "$match": {
"field.subDoc": { "$in": [123,234,345] }
}},
// Count by the element as a key
{ "$group": {
"_id": "$field.subDoc",
"count": { "$sum": 1 }
}}
])
That gives you output like this:
{ "_id" : 345, "count" : 2 }
{ "_id" : 234, "count" : 2 }
{ "_id" : 123, "count" : 2 }
But if you really want to go nuts on this, you are specifying the "keys" that you want as part of your query, so you could form a pipeline like this:
db.collection.aggregate([
{ "$match": {
"field.subDoc": { "$in": [123,234,345] }
}},
{ "$unwind": "$field" },
{ "$match": {
"field.subDoc": { "$in": [123,234,345] }
}},
{ "$group": {
"_id": "$field.subDoc",
"count": { "$sum": 1 }
}},
{ "$group": {
"_id": null,
"123": {
"$max": {
"$cond": [
{ "$eq": [ "$_id", 123 ] },
"$count",
0
]
}
},
"234": {
"$max": {
"$cond": [
{ "$eq": [ "$_id", 234 ] },
"$count",
0
]
}
},
"345": {
"$max": {
"$cond": [
{ "$eq": [ "$_id", 345 ] },
"$count",
0
]
}
}
}}
])
Which is a relatively simple thing to construct that last stage in code by just processing the list of arguments:
var list = [123,234,345];
var group2 = { "$group": { "_id": null } };
list.forEach(function(id) {
group2["$group"][id] = {
"$max": {
"$cond": [
{ "$eq": [ "$_id", id ] },
"$count",
0
]
}
};
});
And that comes out more or less how you want it.
{
"_id" : null,
"123" : 2,
"234" : 2,
"345" : 2
}
Not exactly what you're asking for but it can give you an idea:
db.test.aggregate([
{
$unwind: '$field'
},
{
$group: {
_id: {
subDoc: '$field.subDoc'
},
count: {
$sum: 1
}
}
},
{
$project: {
subDoc: '$subDoc.subDoc',
count: '$count'
}
}
]);
Output:
{
"result": [
{
"_id": {
"subDoc": 456
},
"count": 1
},
{
"_id": {
"subDoc": 345
},
"count": 2
},
{
"_id": {
"subDoc": 234
},
"count": 2
},
{
"_id": {
"subDoc": 123
},
"count": 2
}
],
"ok": 1
}