I have this Mongo query:
db.getCollection('Catalogos').aggregate(
{ $match: {Items: {$elemMatch: {'MarIclase': '04'} } } },
{ $unwind : "$Items" },
{ $match: { "Items.MarIclase" : "04" } },
{ $group : {
_id : "$_id",
Items : { $push : { 'MarIclase': "$Items.MarIclase", 'MarCdescrip' : '$Items.MarCdescrip' } }
}}
);
The result of this query is:
{
"result" : [
{
"_id" : "CAT_MARCAS_VU",
"Items" : [
{
"MarIclase" : "04",
"MarCdescrip" : "5500 LARSON"
},
{
"MarIclase" : "04",
"MarCdescrip" : "A LINER"
}
]
}
],
"ok" : 1.0000000000000000
}
I'd like to have this result:
{
"result" : [
{
"_id" : "CAT_MARCAS_VU",
"Items" : [
{
"04" : "5500 LARSON"
},
{
"04" : "A LINER"
}
]
}
],
"ok" : 1.0000000000000000
}
¿Do you know if I can make something in the $push and change the fieldnames for values?
I'd like to have something like this:
{ "04" : "A LINER" }
{ "04" : "5500 LARSON" }
Thank you!
Related
I have two JSONs in a collection in mongodb and would like to write a bson.M filter to fetch the first JSON below.
I tried with the filter below to get the first JSON but got no result.
When the first JSON is in the collection, I get result when i use the filter
but when i have both JSONs, I do not get a result. Need help.
filter := bson.M{"type": "FPF", "status": "REGISTERED","fpfInfo.fpfInfoList.ssai.st": 1, "fpfInfo.fpfInfoList.infoList.dn": "sim"}
{
"_id" : "47f6ad68-d431-4b69-9899-f33d828f8f9c",
"type" : "FPF",
"status" : "REGISTERED",
"fpfInfo" : {
"fpfInfoList" : [
{
"ssai" : {
"st" : 1
},
"infoList" : [
{
"dn" : "sim"
}
]
}
]
}
},
{
"_id" : "347c8ed2-d9d1-4f1a-9672-7e8a232d2bf8",
"type" : "FPF",
"status" : "REGISTERED",
"fpfInfo" : {
"fpfInfoList" : [
{
"ssai" : {
"st" : 1,
"ds" : "000004"
},
"infoList" : [
{
"dn" : "sim"
}
]
}
]
}
}
db.collection.aggregate([
{
"$unwind": "$fpfInfo.fpfInfoList"
},
{
"$match": {
"fpfInfo.fpfInfoList.ssai.ds": {
"$exists": false
},
"fpfInfo.fpfInfoList.infoList.dn": "sim",
"fpfInfo.fpfInfoList.ssai.st": 1
}
}
])
Playground
Look my problen hehe. I don't know how I can do it.
There are a lot of assetState 1..n I would like do aggregation for get last asset state group by asset.
Mongo collection : assetState
[
{
"lsd" : {
"$id" : ObjectId("lucas")
},
"stateDate" : ISODate("2018-09-10T16:26:44.501Z"),
"assetId" : ObjectId("5b96b7645f2b3c0101520s60")
},
{
"lsd" : {
"$id" : ObjectId("denner")
},
"stateDate" : ISODate("2018-09-10T17:26:44.501Z"),
"assetId" : ObjectId("5b96b7645f2b3c0101520s60")
},
{
"lsd" : {
"$id" : ObjectId("denner")
},
"stateDate" : ISODate("2018-09-10T18:26:44.501Z"),
"assetId" : ObjectId("5b96b7645f2a8c0001530f61")
},
{
"lsd" : {
"$id" : ObjectId("lermen")
}
},
"stateDate" : ISODate("2018-09-10T20:26:44.501Z"),
"assetId" : ObjectId("5b96b7645f2a8c0001530f61")
},
{
"lsd" : {
"$id" : ObjectId("floripa")
},
"stateDate" : ISODate("2018-09-10T19:26:44.501Z"),
"assetId" : ObjectId("5b96b7645f2a8c0001530f61")
}
]
I would like get max "stateDate", so I need get LSD from same row(document).
Expected result:
{
"lsd" : {
"$id" : ObjectId("lermen")
},
"stateDate" : ISODate("2018-09-10T20:26:44.501Z")
}
I tried to do:
db.getCollection('assetState').aggregate([
{
$group: {
"_id": {"assetId": "$assetId"},
"stateDate": {
"$max": "$stateDate"
},
"lsd": {$last: "$lsd"} // I tried change $max to $min and $last it din't work :(
}
]);
Result:
{
"lsd" : {
"$id" : ObjectId("floripa")
},
"stateDate" : ISODate("2018-09-10T20:26:44.501Z")
}
Many Thanks
Try this query
db.getCollection('assetState').aggregate([
{$sort:{"stateDate":-1}},
]).limit(1)
you can $sort descending before $group and gets the first item of each group with $arrayElemAt
db.getCollection('assetState').aggregate([
{ $sort: { stateDate: -1 } },
{ $group: { _id: { "assetId" : "$assetId" },
states: { $push: "$$ROOT" }
}
},
{ $project: { "last_asset": { $arrayElemAt: [ "$states", 0 ] }, _id:0 } },
])
Result:
/* 1 */
{
"last_asset" : {
"_id" : ObjectId("5db2b34fa1b70230bba9c4d9"),
"lsd" : "denner",
"stateDate" : ISODate("2018-09-10T17:26:44.501Z"),
"assetId" : "5b96b7645f2b3c0101520s60"
}
}
/* 2 */
{
"last_asset" : {
"_id" : ObjectId("5db2b34fa1b70230bba9c4db"),
"lsd" : "lermen",
"stateDate" : ISODate("2018-09-10T20:26:44.501Z"),
"assetId" : "5b96b7645f2a8c0001530f61"
}
}
You could use $unwind (aggregation)
https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/
I have this for the group by phase:
{
_id: {"sourceOfEvent":"$sourceOfEvent","vehicleId":"$vehicleId"},
maxTimeOfEvent: { $max: "$timeOfEvent" }
}
So group by on 2 fields and I use the max to get the max of the timeOfEvent (which is a date)
How I can get the total number of seats available for a particular movie (seats present in all the theatres for that movie) from the mongodb schema below.
I need to write a mongo query to get the results
{
"_id" : ObjectId("5d637b5ce27c7d60e5c42ae7"),
"name" : "Bangalore",
"movies" : [
{
"name" : "KGF",
"theatres" : [
{
"name" : "PVR",
"seats" : 45
},
{
"name" : "IMAX",
"seats" : 46
}
]
},
{
"name" : "Avengers",
"theatres" : [
{
"name" : "IMAX",
"seats" : 50
}
]
}
],
"_class" : "com.BMS_mongo.ZZ_BMS_mongo_demo.Entity.CityInfo"
}
I have written this code :
db.cities.aggregate( [
{ "$unwind" : "$movies" }, { "$unwind" : "$theatres" } ,
{ "$group" : { _id : "$movies.theatre`enter code here`s.seats" ,
total : { "$sum" : "$seats" } }
}
] )
My schema:
The following query can get us the expected output:
db.collection.aggregate([
{
$unwind:"$movies"
},
{
$unwind:"$movies.theatres"
},
{
$group:{
"_id":"$movies.name",
"movie":{
$first:"$movies.name"
},
"totalSeats":{
$sum:"$movies.theatres.seats"
}
}
},
{
$project:{
"_id":0
}
}
]).pretty()
Data set:
{
"_id" : ObjectId("5d637b5ce27c7d60e5c42ae7"),
"name" : "Bangalore",
"movies" : [
{
"name" : "KGF",
"theatres" : [
{
"name" : "PVR",
"seats" : 45
},
{
"name" : "IMAX",
"seats" : 46
}
]
},
{
"name" : "Avengers",
"theatres" : [
{
"name" : "IMAX",
"seats" : 50
}
]
}
],
"_class" : "com.BMS_mongo.ZZ_BMS_mongo_demo.Entity.CityInfo"
}
Output:
{ "movie" : "Avengers", "totalSeats" : 50 }
{ "movie" : "KGF", "totalSeats" : 91 }
Query:
db.movie.aggregate([{ $unwind: { path: "$movies",} },
{ $unwind: { path: "$movies.theatres",} },
{ $group: { _id: "$movies.name", "moviename": { $first: "$movies.name" },
"totalSeats": { $sum: "$movies.theatres.seats" }} }])
I got the answer using this query ...
db.cities.aggregate( [
{ "$match" : { "name" : "Bangalore" } },
{ "$unwind" : "$movies" } ,
{ "$match" : {"movies.name" : "KGF"} },
{ "$unwind" : "$theatres" },
{ "$group" : { _id : "$movies.name", total : { "$sum" : "$movies.theatres.seats"
} } }
] )
I have a collection as below what I want is to fetch the items that has exact match of Tag="dolore", I tried different ways but I am getting all the elements if any of the embedded element has tag as dolore
{
"_id" : 123,
"vendor" : "ut",
"boxes" : [
{
"boxRef" : 321,
"items" : [
{
"Tag" : "dolore",
},
{
"Tag" : "irure",
},
{
"Tag" : "labore",
}
]
},
{
"boxRef" : 789,
"items" : [
{
"Tag" : "incididunt",
},
{
"Tag" : "magna",
},
{
"Tag" : "laboris",
}
]
},
{
"boxRef" : 456,
"items" : [
{
"Tag" : "reprehenderit",
},
{
"Tag" : "reprehenderit",
},
{
"Tag" : "enim",
}
]
}
]
}
If you are expecting to get only the matching embedded documents you have $unwind, $match and then $group to reverse the $unwind. Like this:
db.getCollection('collectionName').aggregate([
{
$unwind:"$boxes"
},
{
$unwind:"$boxes.items"
},
{
$match:{
"boxes.items.Tag":"dolore"
}
},
{
$group:{
_id:{
boxRef:"$boxes.boxRef",
_id:"$_id"
},
vendor:{
"$first":"$vendor"
},
boxRef:{
"$first":"$boxes.boxRef"
},
items:{
$push:"$boxes.items"
}
}
},
{
$group:{
_id:"$_id._id",
vendor:{
"$first":"$vendor"
},
boxes:{
$push:{
boxRef:"$boxRef",
items:"$items"
}
}
}
},
])
Output:
{
"_id" : 123.0,
"vendor" : "ut",
"boxes" : [
{
"boxRef" : 321.0,
"items" : [
{
"Tag" : "dolore"
}
]
}
]
}
I have following collection. "addedDetails" is a embedded array document. i want to match HM project and retrieve corresponding value in kk dash board
{
"_id" : "eJHHpB4DkBfLh9kQH",
"dashBoardName" : "kk",
"addedDetails" : [
{
"jid" : "reZYYfWxP9Da9FdZP",
"job" : "job1",
"project" : "HM",
"buildStatus" : "FAILURE"
},
{
"jid" : "KvBcagCuB9DtZa9Wm",
"job" : "job 2",
"project" : "HM",
"buildStatus" : "SUCCESS"
},
{
"jid" : "raiTB4mQ5TmE2d2Jn",
"job" : "job3",
"project" : "CEI",
"buildStatus" : "FAILURE"
},
{
"jid" : "rEmuq6Shtz2vW6Pf3",
"job" : "job4",
"project" : "RI",
"buildStatus" : "FAILURE"
}
]
}
{
"_id" : "muzA3wjGYfk9Ye5pE",
"dashBoardName" : "ss",
"addedDetails" : [
{
"jid" : "MkTsPB5xgkZKGShSq",
"job" : "job1",
"project" : "HM",
"buildStatus" : "SUCCESS"
}
]
}
Expected retun value:
{
"_id" : "eJHHpB4DkBfLh9kQH",
"dashBoardName" : "kk",
"addedDetails" : [
{
"jid" : "reZYYfWxP9Da9FdZP",
"job" : "job1",
"project" : "HM",
"buildStatus" : "FAILURE"
},
{
"jid" : "KvBcagCuB9DtZa9Wm",
"job" : "job2",
"project" : "HM",
"buildStatus" : "SUCCESS"
}
]}
}
my query:
'listjobName': function(){
return dashBoard.find({"dashBoardName":"kk","addedDetails.project":"HM"},{addedDetails: { $all: [{ "$elemMatch" : { project: "HM" }}]}} );
}
Please some one help me to correct the query. here all the value in dash board kk is returned.
db.collection.aggregate(
// Pipeline
[
// Stage 1
{
$match: {
"dashBoardName": "kk"
}
},
// Stage 2
{
$unwind: {
path: '$addedDetails'
}
},
// Stage 3
{
$match: {
'addedDetails.project': 'HM'
}
},
// Stage 4
{
$group: {
_id: {
_id: '$_id',
dashboardName: '$dashBoardName'
},
addedDetails: {
$addToSet: '$addedDetails'
}
}
},
// Stage 5
{
$project: {
dashBoardName: '$_id.dashboardName',
_id: '$_id._id',
addedDetails: 1
}
},
]
);
db.collection_name.aggregate( [
{ $unwind : "$addedDetails" },
{ $match :
{
$and: [ { "dashBoardName" : "kk" }, { "addedDetails.project" : "HM" } ]
}
},
{ $group :
{ _id : " $_id",
dashBoardName : { $first : "$dashBoardName"},
addedDetails : { $push : "$addedDetails" }
}
}
])
Outputs:
{
"_id" : "eJHHpB4DkBfLh9kQH",
"dashBoardName" : "kk",
"addedDetails" : [
{
"jid" : "reZYYfWxP9Da9FdZP",
"job" : "job1",
"project" : "HM",
"buildStatus" : "FAILURE"
},
{
"jid" : "KvBcagCuB9DtZa9Wm",
"job" : "job 2",
"project" : "HM",
"buildStatus" : "SUCCESS"
}
]
}