MongoDB $push operator and count characters in a name - mongodb

I've populated a database in Mongodb with the following data in a collection named people.
{ "_id" : 0, "name" : "Bernice Pope", "age" : 69, "date" : "2017-10-04T18:35:44.011Z" }
{ "_id" : 1, "name" : "Eric Malone", "age" : 57, "date" : "2017-10-04T18:35:44.014Z" }
{ "_id" : 2, "name" : "Blanche Miller", "age" : 35, "date" : "2017-10-4T18:35:44.015Z" }
{ "_id" : 3, "name" : "Sue Perez", "age" : 64, "date" : "2017-10-04T18:35:44.016Z" }
{ "_id" : 4, "name" : "Ryan White", "age" : 39, "date" : "2017-10-04T18:35:44.019Z"}
{ "_id" : 5, "name" : "Grace Payne", "age" : 56, "date" : "2017-10-04T18:35:44.020Z" }
{ "_id" : 6, "name" : "Jessie Yates", "age" : 53, "date" : "2017-10-04T18:35:44.020Z"}
{ "_id" : 7, "name" : "Herbert Mason", "age" : 37, "date" : "2017-10-4T18:35:44.020Z" }
{ "_id" : 8, "name" : "Jesse Jordan", "age" : 47, "date" : "2017-10-04T18:35:44.020Z"}
{ "_id" : 9, "name" : "Hulda Fuller", "age" : 25, "date" : "2017-10-04T18:35:44.020Z"}
Based on the people collection, I need to create a view named PeopleNames which likes below:
{ "LengthOfName": 8, "names" : [ "Sue Perez" ]}
{ "LengthOfName" : 9, "names" : [ "Ryan White" ]}
{ "LengthOfName" : 10, "names" : [ "Eric Malone", "Grace Payne" ]}
{ "LengthOfName" : 11, "names" : [ "Bernice Pope", "Jessie Yates", "Jesse Jordan", "Hulda Fuller" ]}
{ "LengthOfName" : 12, "names" : [ "Herbert Mason" ]}
{ "LengthOfName" : 13, "names" : [ "Blanche Miller"]}
LengthOfName is the total number of the characters in the last name and first name.
My approach is to first add the names into an array, the use $split operator to split the names and use $strLenCP to count the characters.
db.people.aggregate([
{
$project : {
name : 1,
name_array : [{$push : { $split : {$name : " "}}}]
}
}
,{
$unwind : "$name_array"
},{
$project : {
name : 1,
nameLength : {$strLenCP : $name_array}
}
},{
$group :{
_id : "$nameLength",
nameLength: 1
}
}])
But I am receiving error saying that my "$name_array" is undefined Any ideas?

The $push aggregation operator is only available in the $group stage.
You can group the documents by the name length (after trimming the spaces in the name using $replaceAll) and use $push to add the names to the names array. You can then add a $project stage to add the LengthOfName field to the documents and finally add a $sort stage to sort the documents by the LengthOfName field.
db.collection.aggregate([
{
$group: {
_id: {
$strLenCP: {
$replaceAll: {
input: "$name",
find: " ",
replacement: ""
}
}
},
names: {
$push: "$name"
}
}
},
{
$project: {
_id: 0,
LengthOfName: "$_id",
names: "$names",
}
},
{
$sort: {
LengthOfName: 1,
}
}
])
MongoPlayground

Related

How to get last document of each day in MongoDB collection?

I have a model Entry to which includes details of a hospital at a particular time. The data looks like this:
{
"_id": "5ef9c7337874820008c1a026",
"date": 1593427763640,
//... some data
"hospital": {
"_id": "5ef8d06630c364000840bb6d",
"name": "City Hospital",
//... some data
},
}
I want to get the last query of each day grouped by the hospital ID. In MySQL, it can be achieved using INNER JOIN. How can I do it using MongoDB?
Given a day, calculate start and end of a day.
This is to be used for filtering records, $match
start_of_day_ephocs=
end_of_day_ephocs=
Aggregate Query
sort by date, Group by hospital id,and select first document
db.Entry.aggregate(
[
{ "$match": { "date": {"$gte":start_of_day_ephocs,"$lte":end_of_day_ephocs }} },
{ "$sort": { "date": -1 } },
{
$group:
{
"_id": "$hospital._id",
"last_document": { "$first": "$$ROOT" }
}
}
]
)
Consider a sales collection with the following documents:
{ "_id" : 1, "item" : "abc", "date" : ISODate("2014-01-01T08:00:00Z"), "price" : 10, "quantity" : 2 }
{ "_id" : 2, "item" : "jkl", "date" : ISODate("2014-02-03T09:00:00Z"), "price" : 20, "quantity" : 1 }
{ "_id" : 3, "item" : "xyz", "date" : ISODate("2014-02-03T09:05:00Z"), "price" : 5, "quantity" : 5 }
{ "_id" : 4, "item" : "abc", "date" : ISODate("2014-02-15T08:00:00Z"), "price" : 10, "quantity" : 10 }
{ "_id" : 5, "item" : "xyz", "date" : ISODate("2014-02-15T09:05:00Z"), "price" : 5, "quantity" : 10 }
{ "_id" : 6, "item" : "xyz", "date" : ISODate("2014-02-15T12:05:10Z"), "price" : 5, "quantity" : 5 }
{ "_id" : 7, "item" : "xyz", "date" : ISODate("2014-02-15T14:12:12Z"), "price" : 5, "quantity" : 10 }
The following operation first sorts the documents by item and date, and then in the following $group stage, groups the now sorted documents by the item field and uses the $last accumulator to compute the last sales date for each item:
db.sales.aggregate(
[
{ $sort: { item: 1, date: 1 } },
{
$group:
{
_id: "$item",
lastSalesDate: { $last: "$date" }
}
}
]
)
The operation returns the following results:
{ "_id" : "xyz", "lastSalesDate" : ISODate("2014-02-15T14:12:12Z") }
{ "_id" : "jkl", "lastSalesDate" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : "abc", "lastSalesDate" : ISODate("2014-02-15T08:00:00Z") }
Resource

aggregate function to project array size after removing empty value in array

My primary goal is to print titles are having number of grades greater than four, i can achieve it with below query,
db.students.aggregate({$project : { title:1 ,_id : 0, count: {$size : "$grades"}}},{$match: {"count": {$gt:4}}})
But if grades array have empty values how can i remove them, tried this but not giving correct output.
db.students.aggregate({$project : { title:1 ,_id : 0, count: {$size : "$grades"}}},{$match: {"count": {$gt:4},grades : {$ne:''}}})
You can use $filter to remove empty grades before you run $size:
db.students.aggregate([
{$project : { title:1 ,_id : 0, count: { $size : { $filter: { input: "$grades", cond: { $ne: [ "$$this", '' ] } } }}}},
{$match: {"count": {$gt:4}}}
])
Let's explain this with step by step of different different queries:
All possible values in the collection grades:
> db.grades.find()
{ "_id" : ObjectId("5cb2ff50d33f6ed856afe577"), "title" : "abc", "grades" : [ 12, 23, 1 ] }
{ "_id" : ObjectId("5cb2ff55d33f6ed856afe578"), "title" : "abc", "grades" : [ 12, 23 ] }
{ "_id" : ObjectId("5cb2ff5cd33f6ed856afe579"), "title" : "abc", "grades" : [ 12, 23, 10, 100, 34 ] }
{ "_id" : ObjectId("5cb2ff63d33f6ed856afe57a"), "title" : "abc", "grades" : "" }
{ "_id" : ObjectId("5cb2ff66d33f6ed856afe57b"), "title" : "abc", "grades" : [ ] }
{ "_id" : ObjectId("5cb2ff6bd33f6ed856afe57c"), "title" : "abc", "grades" : [ 1, 2, 3, 4, 5 ] }
Just filtered empty grades records as:
> db.grades.aggregate([{$match: {grades: {$ne:''}} }])
{ "_id" : ObjectId("5cb2ff50d33f6ed856afe577"), "title" : "abc", "grades" : [ 12, 23, 1 ] }
{ "_id" : ObjectId("5cb2ff55d33f6ed856afe578"), "title" : "abc", "grades" : [ 12, 23 ] }
{ "_id" : ObjectId("5cb2ff5cd33f6ed856afe579"), "title" : "abc", "grades" : [ 12, 23, 10, 100, 34 ] }
{ "_id" : ObjectId("5cb2ff66d33f6ed856afe57b"), "title" : "abc", "grades" : [ ] }
{ "_id" : ObjectId("5cb2ff6bd33f6ed856afe57c"), "title" : "abc", "grades" : [ 1, 2, 3, 4, 5 ] }
Now project the grades count values in a variable along with required other columns.
> db.grades.aggregate([{$match: {grades: {$ne:''}} }, {$project: {_id:0, title:1, count: {$size: "$grades"} } }])
{ "title" : "abc", "count" : 3 }
{ "title" : "abc", "count" : 2 }
{ "title" : "abc", "count" : 5 }
{ "title" : "abc", "count" : 0 }
{ "title" : "abc", "count" : 5 }
Now match required condition of grades array count greater than 4 as below:
> db.grades.aggregate([{$match: {grades: {$ne:''}} }, {$project: {_id:0, title:1, count: {$size: "$grades"} } }, {$match: {count: {$gte: 4}}} ])
{ "title" : "abc", "count" : 5 }
{ "title" : "abc", "count" : 5 }
>

How do I calculate a field in all documents based on a value of a particular document in the same collection?

I am new to MongoDb and trying to achieve some basic calculation in it. I have collection, calc, as below
{ "_id" : 1, "value" : 10}
{ "_id" : 2, "value" : 20}
{ "_id" : 3, "value" : 20}
{ "_id" : 4, "value" : 30}
{ "_id" : 5, "value" : 30}
{ "_id" : 6, "value" : 30}
I want to add the value of "_id":1 to all value field of the documents in that collection and create a new field with the calculated result. So the final result I am looking for is as below.
{ "_id" : 1, "value" : 10, "sumup":20 }
{ "_id" : 2, "value" : 20, "sumup":30 }
{ "_id" : 3, "value" : 20, "sumup":30 }
{ "_id" : 4, "value" : 30, "sumup":40 }
{ "_id" : 5, "value" : 30, "sumup":40 }
{ "_id" : 6, "value" : 30, "sumup":40 }
You could try this in mongo shell:
db.collection.aggregate([
{
"$project": {
"value": 1,
"sumup": {
"$add": [ "$value", (db.collection.findOne({"_id": 1})).value ]
}
}
}
])

Select all distinct subdocuments

I am have the next document collection:
{ "_id" : ObjectId("568dc77473bf57f465da61dd"), "name" : "Rama", "items" : [ { "id" : 40, "name" : "Botella" }, { "id" : 30, "name" : "Frasco" } ] }
{ "_id" : ObjectId("568dc78873bf57f465da61de"), "name" : "Pepe", "items" : [ { "id" : 40, "name" : "Botella" }, { "id" : 29, "name" : "Cigarrillo" } ] }
Each document have a collection of items. Each item is a document. So i have the list of all distinct items on the collection:
{ "id" : 40, "name" : "Botella" },
{ "id" : 29, "name" : "Cigarrillo" },
{ "id" : 30, "name" : "Frasco" }
With $unwind i obtained one item per document:
db.test.aggregate([{'$unwind': '$items'}, {'$project': {'items.id': 1, 'items.name': 1, '_id': 0}}])
{ "items" : { "id" : 40, "name" : "Botella" } }
{ "items" : { "id" : 30, "name" : "Frasco" } }
{ "items" : { "id" : 40, "name" : "Botella" } }
{ "items" : { "id" : 29, "name" : "Cigarrillo" } }
But now, i don't know how get the distinct 'items'. I am doing my first steps with MongoDB.
Any ideas ?
You can group on the items.id and items.name to get the distinct items
db.test.aggregate([
{'$unwind': '$items'},
{'$group': {'_id':{'id':'$items.id', 'name':'$items.name'}}},
{'$project': {'items': '$_id', '_id':0}}])

MongoDB: Sort in combination with Aggregation group

I have a collection called transaction with below documents,
/* 0 */
{
"_id" : ObjectId("5603fad216e90d53d6795131"),
"statusId" : "65c719e6727d",
"relatedWith" : "65c719e67267",
"status" : "A",
"userId" : "100",
"createdTs" : ISODate("2015-09-24T13:15:36.609Z")
}
/* 1 */
{
"_id" : ObjectId("5603fad216e90d53d6795134"),
"statusId" : "65c719e6727d",
"relatedWith" : "65c719e6726d",
"status" : "B",
"userId" : "100",
"createdTs" : ISODate("2015-09-24T13:14:31.609Z")
}
/* 2 */
{
"_id" : ObjectId("5603fad216e90d53d679512e"),
"statusId" : "65c719e6727d",
"relatedWith" : "65c719e6726d",
"status" : "C",
"userId" : "100",
"createdTs" : ISODate("2015-09-24T13:13:36.609Z")
}
/* 3 */
{
"_id" : ObjectId("5603fad216e90d53d6795132"),
"statusId" : "65c719e6727d",
"relatedWith" : "65c719e6726d",
"status" : "D",
"userId" : "100",
"createdTs" : ISODate("2015-09-24T13:16:36.609Z")
}
When I run the below Aggregation query without $group,
db.transaction.aggregate([
{
"$match": {
"userId": "100",
"statusId": "65c719e6727d"
}
},
{
"$sort": {
"createdTs": -1
}
}
])
I get the result in expected sorting order. i.e Sort createdTs in descending order (Minimal result)
/* 0 */
{
"result" : [
{
"_id" : ObjectId("5603fad216e90d53d6795132"),
"createdTs" : ISODate("2015-09-24T13:16:36.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795131"),
"createdTs" : ISODate("2015-09-24T13:15:36.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795134"),
"createdTs" : ISODate("2015-09-24T13:14:31.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d679512e"),
"createdTs" : ISODate("2015-09-24T13:13:36.609Z")
}
],
"ok" : 1
}
If I apply the below aggregation with $group, the resultant is inversely sorted(i.e Ascending sort)
db.transaction.aggregate([
{
"$match": {
"userId": "100",
"statusId": "65c719e6727d"
}
},
{
"$sort": {
"createdTs": -1
}
},
{
$group: {
"_id": {
"statusId": "$statusId",
"relatedWith": "$relatedWith",
"status": "$status"
},
"status": {$first: "$status"},
"statusId": {$first: "$statusId"},
"relatedWith": {$first: "$relatedWith"},
"createdTs": {$first: "$createdTs"}
}
}
]);
I get the result in inverse Order i.e. ** Sort createdTs in Ascending order**
/* 0 */
{
"result" : [
{
"_id" : ObjectId("5603fad216e90d53d679512e"),
"createdTs" : ISODate("2015-09-24T13:13:36.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795134"),
"createdTs" : ISODate("2015-09-24T13:14:31.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795131"),
"createdTs" : ISODate("2015-09-24T13:15:36.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795132"),
"createdTs" : ISODate("2015-09-24T13:16:36.609Z")
}
],
"ok" : 1
}
Where am I wrong ?
The $group stage doesn't insure the ordering of the results. See here the first paragraph.
If you want the results to be sorted after a $group, you need to add a $sort after the $group stage.
In your case, you should move the $sort after the $group and before you ask the question : No, the $sort won't be able to use an index after the $group like it does before the $group :-).
The internal algorithm of $group seems to keep some sort of ordering (reversed apparently), but I would not count on that and add a $sort.
You are not doing anything wrong here, Its a $group behavior in Mongodb
Lets have a look in this example
Suppose you have following doc in collection
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") }
{ "_id" : 6, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-15T12:05:10Z") }
{ "_id" : 7, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T14:12:12Z") }
Now if you run this
db.collection.aggregate([{ $sort: { item: 1,date:1}} ] )
the output will be in ascending order of item and date.
Now if you add group stage in aggregation pipeline it will reverse the order.
db.collection.aggregate([{ $sort: { item: 1,date:1}},{$group:{_id:"$item"}} ] )
Output will be
{ "_id" : "xyz" }
{ "_id" : "jkl" }
{ "_id" : "abc" }
Now the solution for your problem
change "createdTs": -1 to "createdTs": 1 for group