Filter on a mongo document and project specific fields - mongodb

I am quite a beginner in MongoDb. Tried a lot but stuck on one query.
Please find a sample mongo document on the given below:
{
"_id" : ObjectId("5dc158a60566e14c5190db72"),
"hotel_id" : NumberInt(45),
"plans" : [
{
"plan_type" : "AP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0Y280000023LFwEAM",
"breakfast_start_time" : null,
"breakfast_end_time" : null,
"lunch_start_time" : null,
"lunch_end_time" : null,
"dinner_start_time" : null,
"dinner_end_time" : null,
"created_at" : NumberLong(1468577718000),
"updated_at" : NumberLong(1481074321000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(400),
"non_veg_price" : null,
"additional_non_veg_price" : null,
"discount" : null
}
]
},
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0W28000004GqL4",
"breakfast_start_time" : "7:30",
"breakfast_end_time" : "10:00",
"lunch_start_time" : "12:30",
"lunch_end_time" : "15:00",
"dinner_start_time" : "19:00",
"dinner_end_time" : "22:30",
"created_at" : NumberLong(1505516604000),
"updated_at" : NumberLong(1505516604000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(425),
"non_veg_price" : NumberInt(425),
"additional_non_veg_price" : 0.0,
"discount" : null
}
]
},
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0W28000004GqL4",
"breakfast_start_time" : "7:30",
"breakfast_end_time" : "10:00",
"lunch_start_time" : "12:30",
"lunch_end_time" : "15:00",
"dinner_start_time" : "19:00",
"dinner_end_time" : "22:30",
"created_at" : NumberLong(1505709978000),
"updated_at" : NumberLong(1542162045000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(425),
"non_veg_price" : NumberInt(425),
"additional_non_veg_price" : 0.0,
"discount" : null
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
},
{
"plan_type" : "EP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : null,
"ep_price_reduction" : NumberInt(80),
"salesforce_id" : "a0Y280000023LG1EAM",
"breakfast_start_time" : null,
"breakfast_end_time" : null,
"lunch_start_time" : null,
"lunch_end_time" : null,
"dinner_start_time" : null,
"dinner_end_time" : null,
"created_at" : NumberLong(1468577718000),
"updated_at" : NumberLong(1481074321000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : null,
"non_veg_price" : null,
"additional_non_veg_price" : null,
"discount" : null
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
},
{
"plan_type" : "MAP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0Y280000023LG6EAM",
"breakfast_start_time" : null,
"breakfast_end_time" : null,
"lunch_start_time" : null,
"lunch_end_time" : null,
"dinner_start_time" : null,
"dinner_end_time" : null,
"created_at" : NumberLong(1468577718000),
"updated_at" : NumberLong(1481074321000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(200),
"non_veg_price" : null,
"additional_non_veg_price" : null,
"discount" : null
}
]
},
{
"channel_name" : "default",
"status" : "Pending",
"is_active" : false,
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"non_veg_available" : null,
"oyo_commission" : 20.0,
"ep_price_reduction" : null,
"salesforce_id" : "a0W28000004GqL4",
"breakfast_start_time" : "7:30",
"breakfast_end_time" : "10:00",
"lunch_start_time" : "12:30",
"lunch_end_time" : "15:00",
"dinner_start_time" : "19:00",
"dinner_end_time" : "22:30",
"created_at" : NumberLong(1505709978000),
"updated_at" : NumberLong(1541476001000),
"prices" : [
{
"occupancy" : NumberInt(1),
"guest_type" : null,
"veg_price" : NumberInt(225),
"non_veg_price" : NumberInt(225),
"additional_non_veg_price" : 0.0,
"discount" : null
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
}
]
}
I want only selected fields if:
hotel id is 45, price of occupancy 1, is_active is false and status is Pending and plan type is in ['AP', 'EP'].
Desired output:
{
"_id" : ObjectId("5dc158a60566e14c5190db72"),
"hotel_id" : NumberInt(45),
"plans" : [
{
"plan_type" : "AP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"prices" : [
{
"veg_price" : NumberInt(425)
}
]
},
{
"channel_name" : "default",
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"prices" : [
{
"veg_price" : NumberInt(425)
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
},
{
"plan_type" : "EP",
"sub_plans" : [
{
"channels" : [
{
"channel_name" : "default",
"start_date" : NumberLong(1468521000000),
"end_date" : NumberLong(1901125800000),
"prices" : [
{
"veg_price" : null,
}
]
}
],
"sub_plan_type" : "Standard",
"is_sub_plan_default" : false
}
]
},
]
}
My Query:
db.collection.find({
"hotel_id": 45,
"plans.plan_type": {$in : ["AP", "EP"]},
"plans.sub_plans.channels.prices.occupancy": 1,
"plans.sub_plans.channels.is_active": false,
"plans.sub_plans.channels.status": "Pending"
})
Note - I need to search in only one document where hotel id is 45.
Please help.
Thanks in advance.

You may be looking for a query like this.
db.collection.aggregate([
{
"$unwind": "$plans",
},
{
"$group": {
"_id": "$hotel_id",
"hotel_id": {
"$first": "$hotel_id"
},
"plans": {
"$push": {
"$cond": [
{
"$or": [
{
"$eq": [
"$plans.plan_type",
"AP"
]
},
{
"$eq": [
"$plans.plan_type",
"EP"
]
}
]
},
{
"plan_type": "$plans.plan_type",
"sub_plans": "$plans.sub_plans",
"sub_plan_type": "$plans.sub_plan_type",
"is_sub_plan_default": "$is_sub_plan_default"
},
null
]
}
}
}
},
{
"$project": {
"_id": 1,
"hotel_id": 1,
"plans": {
"$filter": {
input: "$plans",
as: "plan",
cond: {
$ne: [
"$$plan",
null
]
}
}
}
}
}
])

You need to apply aggregation.
db.collection_name.aggregate([
{
"$match":{
"hotel_id": 45,
"plans.plan_type": "AP",
"plans.sub_plans.channels.prices.occupancy": 1,
"plans.sub_plans.channels.is_active": false,
"plans.sub_plans.channels.status": "Pending"
}
},
{
"$project":{
"_id":"$_id",
"hotel_id":"$hotel_id",
"plans":"plans"
}
}
])

Related

Find and sort array of array in nested documents

My collection schema is like this:
{
"_id" : ObjectId("5f0c64e4dd0a36b93c7deafa"),
"name" : "Asd",
"email" : "asd#asd.com",
"password" : "$2b$12$66OTK8mSWELMF5YiF9HMUuHEeOVLI61aINjWs1Cmn1699lLJfz/7y",
"auto_ml" : true,
"notification" : true,
"photo" : null,
"tariff_id" : NumberInt(1),
"city" : null,
"sub_district" : null,
"village" : null,
"latitude" : null,
"longitude" : null,
"created_at" : ISODate("2020-07-13T20:43:00.871+0000"),
"updated_at" : ISODate("2020-07-13T23:08:26.149+0000"),
"family_members" : [
],
"rooms" : [
{
"_id" : ObjectId("5f0c98826f0321f6986755da"),
"name" : "Ruang Makan",
"created_at" : ISODate("2020-07-14T00:23:14.839+0000"),
"updated_at" : ISODate("2020-07-14T00:23:14.840+0000"),
"devices" : [
]
},
{
"_id" : ObjectId("5f0c98876f0321f6986755dd"),
"name" : "Ruang Tamu",
"created_at" : ISODate("2020-07-14T00:23:19.693+0000"),
"updated_at" : ISODate("2020-07-14T19:00:08.281+0000"),
"devices" : [
{
"serial_number" : "ST9L0CY4A2AVY7HFWWUE",
"used_relay" : NumberInt(0),
"created_at" : ISODate("2020-07-14T16:56:22.156+0000"),
"updated_at" : ISODate("2020-07-14T16:56:22.156+0000"),
"sensors" : [
{
"_id" : ObjectId("5f0db478ca203bde99d2438e"),
"name" : "Temperature",
"value" : null,
"created_at" : ISODate("2020-07-14T20:34:48.134+0000"),
"updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
},
{
"_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"),
"name" : "Motion",
"value" : null,
"created_at" : ISODate("2020-07-14T21:15:33.135+0000"),
"updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
},
{
"_id" : ObjectId("5f0dc0412022e93af338316f"),
"name" : "Humidity",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:05.126+0000"),
"updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
},
{
"_id" : ObjectId("5f0dc0442022e93af3383170"),
"name" : "Light",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:08.451+0000"),
"updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
}
],
"switches" : [
]
}
]
}
]
}
The question is, how could i access the array sensors inside device with "serial_number": "ST9L0CY4A2AVY7HFWWUE" and inside room with id ObjectId("5f0c98876f0321f6986755dd"), and the sort the sensors by id? I've tried the projection way and aggregate way but the result is not as i expected. My expected result is only showing the array of sensors like:
"sensors" : [
{
"_id" : ObjectId("5f0db478ca203bde99d2438e"),
"name" : "Temperature",
"value" : null,
"created_at" : ISODate("2020-07-14T20:34:48.134+0000"),
"updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
},
{
"_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"),
"name" : "Motion",
"value" : null,
"created_at" : ISODate("2020-07-14T21:15:33.135+0000"),
"updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
},
{
"_id" : ObjectId("5f0dc0412022e93af338316f"),
"name" : "Humidity",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:05.126+0000"),
"updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
},
{
"_id" : ObjectId("5f0dc0442022e93af3383170"),
"name" : "Light",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:08.451+0000"),
"updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
}
],
Query that i have tried:
db.users.aggregate([
{
$match: { "_id": ObjectId("5f0c64e4dd0a36b93c7deafa") }
},
{ $unwind: "$rooms" },
{
$match: {
"rooms._id": ObjectId("5f0c98876f0321f6986755dd")
}
},
{ $unwind: "$rooms.devices" },
{
$match: {
"rooms.devices.serial_number": "ST9L0CY4A2AVY7HFWWUE"
}
},
{
$project: { "rooms.devices.sensors": 1 }
},
{
$group: {
_id: "$_id",
sensors: { $first: "$rooms.devices.sensors" }
}
},
{
$sort: { "rooms.devices.sensors._id": -1}
},
])
But the sensors doesn't seem to be sorted.
Thanks in advance for your help~

$map upto three nested array mongodb aggregation

I´m facing a challenge here. I have this collection here
{
"_id" : ObjectId("5e0ff6d424f9fc12bc3d9464"),
"name" : "Pizzaria Don Juan",
"active" : true,
"branches" : [
{
"location" : {
"type" : "Point",
"coordinates" : [ ]
},
"_id" : ObjectId("5e19cafc31d60216b8dbd649"),
"name" : "Parque da Mooca",
"address" : "Rua Dianópolis",
"addressNumber" : 1283,
"federalId" : "10.445.089/0001-44",
"complement" : "Ap 55",
"postalCode" : "03126-007",
"coveredArea" : 0,
"neighborhood" : "Parque da Mooca",
"deliveryTime" : 0,
"deliveryRate" : 0,
"standard" : false,
"city" : "Mococa",
"state" : "RJ",
"emails" : [ ],
"phones" : [ ],
"daysWeek" : [ ],
"socialMedias" : [ ],
"paymentTerms" : [ ],
"sections" : [ ]
},
{
"location" : {
"type" : "Point",
"coordinates" : [ ]
},
"_id" : ObjectId("5e19c9a531d60216b8dbd639"),
"name" : "Principal",
"address" : "Rua Nicolau Filizola",
"addressNumber" : null,
"federalId" : "10.445.089/0001-53",
"complement" : "",
"postalCode" : "05547-010",
"coveredArea" : 0,
"neighborhood" : "Jardim Rosa Maria",
"deliveryTime" : 0,
"deliveryRate" : 0,
"standard" : true,
"city" : "São Paulo",
"state" : "SP",
"emails" : [
{
"_id" : ObjectId("5e19ca9531d60216b8dbd643"),
"name" : "Contato",
"address" : "contato#pizzariadonjuan.com.br"
},
{
"_id" : ObjectId("5e19ca9531d60216b8dbd642"),
"name" : "Contato2",
"address" : "contato2#pizzariadonjuan.com.br"
}
],
"phones" : [
{
"_id" : ObjectId("5e19ca9531d60216b8dbd645"),
"name" : "Principal",
"number" : "(11) 99740-2216"
},
{
"_id" : ObjectId("5e19ca9531d60216b8dbd644"),
"name" : "Secundario",
"number" : "(11) 2562-2759"
}
],
"daysWeek" : [
{
"_id" : ObjectId("5e1cf99741c52d4a587a9162"),
"startsAt" : 64800000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a124a17fd054900a1afb2")
},
{
"_id" : ObjectId("5e1cf99741c52d4a587a9161"),
"startsAt" : 0,
"endsAt" : 0,
"opens" : false,
"dayWeekId" : ObjectId("5e1a126817fd054900a1afb3")
},
{
"_id" : ObjectId("5e1cfbed41c52d4a587a9170"),
"startsAt" : 64980000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a126e17fd054900a1afb4")
},
{
"_id" : ObjectId("5e1b8fac96516432845e364c"),
"startsAt" : 64980000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a127517fd054900a1afb5")
},
{
"_id" : ObjectId("5e1cfbed41c52d4a587a916f"),
"startsAt" : 64980000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a127a17fd054900a1afb6")
},
{
"_id" : ObjectId("5e1cfbed41c52d4a587a916e"),
"startsAt" : 64800000,
"endsAt" : 82800000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a23f8bf353f493c74e8ae")
},
{
"_id" : ObjectId("5e1cfbed41c52d4a587a916d"),
"startsAt" : 61380000,
"endsAt" : 83154000,
"opens" : true,
"dayWeekId" : ObjectId("5e1a2407bf353f493c74e8af")
}
],
"socialMedias" : [
{
"_id" : ObjectId("5e1d082641c52d4a587a9191"),
"socialMediaId" : ObjectId("5e10089a3330ad05d4e1867d"),
"url" : "rewrwerwerwerwerwerwerwer"
}
],
"paymentTerms" : [
{
"_id" : ObjectId("5e1d143041c52d4a587a91b7"),
"paymentTermId" : ObjectId("5e1a2277bf353f493c74e8a7")
},
{
"_id" : ObjectId("5e1d143041c52d4a587a91b6"),
"paymentTermId" : ObjectId("5e1a228cbf353f493c74e8a8")
},
{
"_id" : ObjectId("5e1d143041c52d4a587a91b5"),
"paymentTermId" : ObjectId("5e1a229ebf353f493c74e8a9")
}
],
"sections" : [
{
"_id" : ObjectId("5e1e535441c52d4a587a9208"),
"name" : "Camisetas",
"products" : [
{
"_id" : ObjectId("5e1e662f044582129844ffd5"),
"name" : "DonJuan M",
"description" : "",
"quantityAvailable" : 0,
"image" : "",
"price" : 0,
"validFrom" : ISODate("2020-01-15T01:08:49.552Z"),
"validTo" : ISODate("2020-01-15T01:08:49.552Z"),
"enabled" : true
}
]
},
{
"_id" : ObjectId("5e20ec889c05f229a484ea3d"),
"name" : "Imãs",
"products" : [
{
"_id" : ObjectId("5e20ec889c05f229a484ea3e"),
"name" : "Imã",
"description" : "Imã",
"quantityAvailable" : 0,
"image" : "",
"price" : 0,
"validFrom" : ISODate("0001-01-01T00:00:00Z"),
"validTo" : ISODate("9999-12-31T00:00:00Z"),
"enabled" : true
}
]
}
]
}
],
"users" : [
{
"_id" : ObjectId("5e10fc2adc147a373c312144")
},
{
"_id" : ObjectId("5e11ff8003eb832ef84342a6")
}
],
"socialMedias" : [
{
"_id" : ObjectId("5e165672a2204b49c892db74"),
"socialMediaId" : ObjectId("5e10089a3330ad05d4e1867d"),
"url" : "uuuutt"
},
{
"_id" : ObjectId("5e15385fb3a0aa1004ac3598"),
"socialMediaId" : ObjectId("5e1009043330ad05d4e1867f"),
"url" : "jkkjkjkjkjk"
}
],
"sections" : [
{
"_id" : ObjectId("5e15313b2e985e16ec4e7413"),
"name" : "Bebidas",
"products" : [
{
"_id" : ObjectId("5e1e6381044582129844ffc2"),
"name" : "Coca Cola Zero 2 Litros",
"description" : "",
"quantityAvailable" : 0,
"image" : "",
"price" : 18.39,
"validFrom" : ISODate("1970-01-01T00:00:00Z"),
"validTo" : ISODate("1970-01-01T00:00:00Z"),
"enabled" : true
},
{
"_id" : ObjectId("5e1e6381044582129844ffc3"),
"name" : "Coca Cola 2 Litros",
"description" : "",
"quantityAvailable" : 0,
"image" : "",
"price" : 21.42,
"validFrom" : ISODate("1970-01-01T00:00:00Z"),
"validTo" : ISODate("1970-01-01T00:00:00Z"),
"enabled" : true
},
{
"_id" : ObjectId("5e1e662f044582129844ffda"),
"name" : "Cerveja Heineken Lata 350ml",
"description" : "Cerveja Heineken Lata 350ml",
"quantityAvailable" : 0,
"image" : "volkswagen-polo.jpg",
"price" : 1.55,
"validFrom" : ISODate("2020-01-01T00:00:00Z"),
"validTo" : ISODate("1970-01-01T00:00:00Z"),
"enabled" : true
}
]
},
{
"_id" : ObjectId("5e20e8de9c05f229a484ea27"),
"name" : "Esfihas",
"products" : [
{
"_id" : ObjectId("5e20e8de9c05f229a484ea28"),
"name" : "Esfiha de carne",
"description" : "Esfiha de carne",
"quantityAvailable" : 0,
"image" : "",
"price" : 5,
"validFrom" : ISODate("2020-01-01T00:00:00Z"),
"validTo" : null,
"enabled" : true
}
]
}
],
"__v" : 0
}
{
"_id" : ObjectId("5e0ffd23991918424c8d7c3b"),
"name" : "Pizza Ruth",
"active" : true,
"users" : [ ],
"socialMedias" : [ ],
"branches" : [ ],
"sections" : [ ],
"__v" : 0
}
{
"_id" : ObjectId("5e0ffd3d991918424c8d7c3c"),
"name" : "Feijão de Corda",
"active" : true,
"users" : [ ],
"socialMedias" : [ ],
"branches" : [ ],
"sections" : [ ],
"__v" : 0
}
The fields validFrom and validTo (Date fields) from the collection products nested in branches.sections need to be converted to the format yyyy-mm-dd. I can do that with this aggregation pipeline:
{ $unwind: { path: "$branches.sections", preserveNullAndEmptyArrays: true } },
{
"$addFields": {
"branches.sections.products": {
$map: {
input: "$branches.sections.products",
as: "product",
in: {
'_id': "$$product._id",
'name': "$$product.name",
'description': "$$product.description",
'quantityAvailable': "$$product.quantityAvailable",
'image': "$$product.image",
'imageUrl': "$$product.imageUrl",
'price': "$$product.price",
'validFrom' : {"$dateToString": { "date": "$$product.validFrom", "format": "%Y-%d-%m" }},
'validTo' : {"$dateToString": { "date": "$$product.validTo", "format": "%Y-%d-%m" }},
'enabled': "$$product.enabled",
}
}
}
}
}
I can successfully convert those date fields, but I need now to "re-unwind" the array products, in order to be just like before the unwind.
Any clue in how to proceed? Or even a different way to format those dates without having to unwind? Tried dozens of ways of $group, but without any success.
You just need to $map over each nested array to drill upload the validTo and validFrom field
.aggregate([
{ "$addFields": {
"branches": {
"$map": {
"input": "$branches",
"as": "branch",
"in": {
"$mergeObjects": [
"$$branch",
{ "section": {
"$map": {
"input": "$$branch.sections",
"as": "section",
"in": {
"$mergeObjects": [
"$$section",
{ "product": {
"$map": {
"input": "$$section.products",
"as": "product",
"in": {
"$mergeObjects": [
"$$product",
{
"validFrom": { "$dateToString": { "date": "$$product.validFrom", "format": "%Y-%d-%m" }},
"validTo": { "$dateToString": { "date": "$$product.validTo", "format": "%Y-%d-%m" }}
}
]
}
}
}}
]
}
}
}}
]
}
}
}
}}
])
MongoPlayground

MongoDB Redash - FieldPath field names may not contain '.'

I am new to mongodb. I am trying to take the daily count. I want count of those element where the fields I_C or L2_C exist and for the last 3 months.
I have written this code, but it is showing error -
"FieldPath field names may not contain '.'".
What would be the correct way to go for this?
Here is the schema of the data I am trying to query on.
{
"_id" : ObjectId("5b83af839bc195a7cfbabefb"),
"firstName" : null,
"lastName" : null,
"realm" : null,
"username" : null,
"password" : "$2a$10$WVTWLevvoJXcMs/x9o/qMOEV56owh2ppjZXJt4pSR8PIDuBeRehwW",
"credentials" : null,
"challenges" : null,
"email" : "ritesh.rnagpure#gmail.com",
"emailVerified" : null,
"verificationToken" : null,
"status" : null,
"created" : null,
"lastUpdated" : ISODate("2019-08-28T12:15:07.808Z"),
"dateCreated" : ISODate("2018-08-27T08:00:02.595Z"),
"source" : "web",
"isFromAPUrl" : false,
"personal" : {
"phone" : "9975891779",
"referralcode" : "",
"iccode" : "221930",
"isiccode" : "yes",
"referrerEmail" : "zohaibrulz#gmail.com",
"referrerName" : "Momin Zohaib Hasanali",
"residency" : {
"type" : "Indian",
"status" : "Status_Resident_Individual",
"nationality" : "Nationality_Indian",
"nationalityOther" : ""
},
"isNameFromIT" : true,
"name" : "RITESH RAJENDRA NAGPURE",
"birthdate" : {
"date" : "7",
"month" : "4",
"year" : "1993"
},
"previousPan" : "AJEPN0774B",
"previousDOB" : "07/04/1993",
"address" : {
"correspondence" : {
"line1" : "Flat No-404, G Wing, Sai Satyam Residency,",
"line2" : "Near Don Bosco School, Kalyan West, Opposite Raunak city, Kalyan, Thane",
"city" : "Kalyan",
"state" : "Maharashtra",
"pincode" : "421301",
"proof" : ""
},
"permanent" : {
"line1" : "Flat No-404, G Wing, Sai Satyam Residency,",
"line2" : "Near Don Bosco School, Kalyan West, Opposite Raunak city, Kalyan, Thane",
"city" : "Kalyan",
"state" : "Maharashtra",
"pincode" : "421301",
"proof" : "",
"proofOtherText" : ""
},
"sameAsCorrespondence" : true
},
"isaddressFromQR" : false,
"isaadhar" : false,
"iskra" : false,
"fatherOrSpouseName" : "RAJENDRA",
"ipvdetails" : {
"ipvrequire" : false,
"ipvstatus" : "done"
},
"aadharNumber" : "875847348082",
"mothername" : "SAVITA",
"gender" : "male",
"polexposed" : "no",
"maritalStatus" : "single",
"incomeType" : "annual",
"income" : "below_1L",
"occupation" : "Private Sector",
"tradingExperience" : "< 1 Year",
"promocode" : "CASHBACK",
"requireddoc" : {
"ispanrequire" : true,
"isuidrequire" : true,
"ischequerequire" : true,
"isphotorequire" : true,
"iscoraddressrequire" : false,
"issignaturerequire" : true,
"isaddressrequire" : false,
"isbankstatementrequire" : true,
"isoptionaldoc" : false
},
"uploadeddoc" : {
"ispan" : true,
"isuid" : true,
"isaddress" : false,
"isphoto" : true,
"iscoraddress" : false,
"ischeque" : true,
"issignature" : true,
"isoptionaldoc" : false,
"isbankstatement" : true,
"issignaturebmp" : true
},
"docname" : {
"uid" : "5b83af839bc195a7cfbabefb_uid.pdf",
"pan" : "5b83af839bc195a7cfbabefb_pan.jpg",
"photo" : "5b83af839bc195a7cfbabefb_photo.jpg",
"address" : "",
"coraddress" : "",
"cheque" : "5b83af839bc195a7cfbabefb_cheque.pdf",
"bankstatement" : "5b83af839bc195a7cfbabefb_bankstm.pdf",
"signature" : "5b83af839bc195a7cfbabefb_sign.jpg",
"other" : "",
"optionaldoc" : ""
},
"doctype" : {
"pan" : "pan",
"uid" : "uid",
"add" : "uid",
"cheque" : "bankstatement",
"bankstm" : "bankstatement",
"photo" : "photo"
},
"isaddressmodified" : true,
"isAllEsigned" : true,
"isEsignMerged" : true,
"iskycFile" : true,
"isKycEsigned" : true,
"isApplicationCompleted" : true,
"applicationStatus" : "completed",
"isPanVerified" : true,
"panStatus" : "E",
"panName" : "RITESH RAJENDRA NAGPURE",
"rejectionReasonList" : [],
"makerAssigned" : false,
"checkerAssigned" : true,
"checkerOfficer" : "robot",
"checkerTime" : ISODate("2019-05-29T11:30:33.004Z")
},
"txn" : {
"L1_L2" : ISODate("2018-08-27T11:06:35.251Z"),
"L1_L2_officer" : "prajakta.rane",
"L2_R" : ISODate("2018-08-27T10:43:19.063Z"),
"L2_R_officer" : "avilon.pereira",
"R_L1" : ISODate("2018-08-27T10:48:51.377Z"),
"L2_I" : ISODate("2018-08-27T11:43:12.718Z"),
"L2_I_officer" : "rajkumar.kesari",
"I_C" : ISODate("2018-08-27T12:58:52.343Z"),
"I_C_officer" : "suraj.pandey"
},
"sales" : {
"openStateOfficer" : "bhaskar.geera",
"aadhaar" : false,
"assignStep" : "registration",
"assignTo" : "bhaskar.geera",
"callTime" : "2018-08-27T11:35:15.446Z",
"calledMessage" : "Ringing",
"calledStatus" : "Ringing_Not_Picked",
"iscalled" : true,
"callCount" : 5
},
"utm_params" : {},
"landingPage" : "",
"suiteID" : "b9245931-a66f-3647-11b8-5b83af881208",
"_scopeMeta" : {},
"panPageVisited" : true,
"kra" : {
"pan" : "AJEPN0774B",
"dob" : "07/04/1993",
"name" : "",
"fatherOrSpouseName" : "",
"agency" : "",
"email" : "",
"mobile" : "",
"address" : {
"correspondence" : {
"line1" : "",
"line2" : "",
"city" : "",
"state" : "",
"pincode" : "",
"proof" : ""
},
"permanent" : {
"line1" : "",
"line2" : "",
"city" : "",
"state" : "",
"pincode" : "",
"proof" : ""
}
},
"gender" : "",
"maritalStatus" : "",
"appStatus" : "005",
"appUpdateStatus" : "005",
"authMode" : "0"
},
"aadhar" : {},
"overlayOpenData" : {
"showSegmentsOverlay" : false,
"showBrokerageOverlay" : false,
"showReviewOverlay" : true,
"showVerifyOverlay" : true
},
"currentStep" : "review/verify",
"namePageVisited" : true,
"options" : {
"BSE_EQ" : true,
"NSE_EQ" : true,
"BCD_FO" : false,
"NCD_FO" : true,
"NSE_FO" : true,
"MCX" : true,
"MF" : true
},
"pricingPlan" : "BASIC-D0",
"bank" : {
"primary" : {
"ifsc" : "SBIN0017460",
"name" : "STATE BANK OF INDIA",
"address" : "MADHUBAN TIRTHDHAM COMPLEX,SHOP NO 4,ADHARWADI ROAD,KALYAN WEST,DISTT.THANE.MAHARASHTRA 421301",
"branch" : "ADHARWADI ROAD BRANCH",
"city" : "THANE",
"state" : "MAHARASHTRA",
"country" : "INDIA",
"micr" : "",
"pincode" : "421301",
"type" : "savings",
"account" : "31527410599"
}
},
"kycSplitStart" : 63,
"kycDocsLength" : 4,
"eSignProperties" : {
"signername" : "Ritesh Rajendra Nagpure"
},
"formSubmittedDate" : ISODate("2018-08-27T10:48:51.377Z"),
"ucccode" : "177762",
"crmPaymentSynced" : true,
"passwordResetCode" : "",
"lastPaymentDate" : ISODate("2018-10-26T09:55:24.149Z"),
"registrationWorkflowId" : 0,
"currentWorkflowId" : 0,
"isPaymentFromLedger" : false,
"lastLoginDate" : ISODate("2019-08-28T12:14:58.569Z"),
"viewedPreSales" : true
}
I have written this query -
"collection": "user",
"aggregate": [
{
"$match": {
"$or" : [
{
"$and" : [
{"txn.I_C" : {"$exists": true}}, {"txn.I_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
},
{
"$and" : [
{"txn.L2_C" : {"$exists": true}}, {"txn.L2_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
}
]}
},
{ "$addFields" : {"date" :{"$cond": { "if": {"txn.I_C" : {"$exists" : true}}, "then": "txn.I_C", "else": "txn.L2_C"}}}},
{
"$project" : {
"_id" : 0,
"datePartDay": {
"$concat": [
{
"$substr": [
{
"$year": "$date"
},
0,
4
]
},
"-",
{
"$substr": [
{
"$month": "$date"
},
0,
2
]
},
"-",
{
"$substr": [
{
"$dayOfMonth": "$date"
},
0,
2
]
}
]
}
}
},
{
"$group": {
"_id": "$datePartDay",
"count": {
"$sum": 1
}
}
}
]
}
Try this way, please:
"collection": "user",
"aggregate": [
{
"$match": {
"$or" : [
{
"$and" : [
{"txn.I_C" : {"$exists": true}}, {"txn.I_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
},
{
"$and" : [
{"txn.L2_C" : {"$exists": true}}, {"txn.L2_C" : {"$gt" : {"$humanTime" :"2019-07-01"}}}]
}
]}
},
{ "$addFields" : {"date" :{"$cond": {"if": {"$ne" : ["$txn.I_C", undefined]}, "then": "$txn.I_C", "else": "$txn.L2_C"}}}},
{
"$project" : {
"_id" : 0,
"datePartDay": {
"$concat": [
{
"$substr": [
{
"$year": "$date"
},
0,
4
]
},
"-",
{
"$substr": [
{
"$month": "$date"
},
0,
2
]
},
"-",
{
"$substr": [
{
"$dayOfMonth": "$date"
},
0,
2
]
}
]
}
}
},
{
"$group": {
"_id": "$datePartDay",
"count": {
"$sum": 1
}
}
}
]
}

MongoDB how we can group data by field value

I have the following query, what I want is to have a combined group of custom group field names, with field value.
db.getCollection('mycollection').aggregate([
{"$match":{
"expireDate":{"$gte":"2018-02-06T00:00:00.000Z"},
"publishDate":{"$lte":"2018-02-06T00:00:00.000Z"},
"isPublished":true,"isDrafted":false,
"deletedAt":{"$eq":null},"deleted":false
}},
{"$group":{
"twentyFourHourAgo":{
"$sum":{
"$cond":[
{"$gt":["$publishDate","2018-02-04T08:48:16.892Z"]},1,0
]
}
},
"fortyEightHourAgo":{
"$sum":{
"$cond":[
{"$gt":["$publishDate","2018-02-01T08:48:16.892Z"]},1,0
]
}
},
"thirtyDaysAgo":{
"$sum":{
"$cond":[
{"$gt":["$publishDate","2017-12-31T08:48:16.892Z"]},1,0
]
}
},
"_id":{
"position":{"$ifNull":["$position","Unknown"]},
"workType":{"$ifNull":["$workType","Unknown"]},
"functionalArea":{"$ifNull":["$functionalArea","Unknown"]},
"minimumEducation":{"$ifNull":["$minimumEducation","Unknown"]},
"gender":{"$ifNull":["$gender","Unknown"]},
"contractType":{"$ifNull":["$contractType","Unknown"]},
"locations":{"$ifNull":["$locations","Unknown"]},
"requiredLanguages":{"$ifNull":["$requiredLanguages","Unknown"]},
"company":{"$ifNull":["$company.name","Unknown"]}},"count":{"$sum":1}
}
},
{"$group":{
"_id":null,
"twentyFourHourAgo":{
"$sum":"twentyFourHourAgo"
},
"fortyEightHourAgo":{
"$sum":"$fortyEightHourAgo"
},
"thirtyDaysAgo":{
"$sum":"$thirtyDaysAgo"
},
"position":{"$addToSet":{"Name":"$_id.position","Count":"$count"}},
"workType":{"$addToSet":{"Name":"$_id.workType","Count":"$count"}},
"functionalArea":{
"$addToSet":{"Name":"$_id.functionalArea","Count":"$count"}
},
"minimumEducation":{
"$addToSet":{"Name":"$_id.minimumEducation","Count":"$count"}
},
"gender":{"$addToSet":{"Name":"$_id.gender","Count":"$count"}},"contractType":{"$addToSet":{"Name":"$_id.contractType","Count":"$count"}},"locations":{"$addToSet":{"Name":"$_id.locations","Count":"$count"}},"requiredLanguages":{"$addToSet":{"Name":"$_id.requiredLanguages","Count":"$count"}},"company":{"$addToSet":{"Name":"$_id.company","Count":"$count"}}}}]
)
my document inside collection schema is like:
/* 1 */
{
"_id" : ObjectId("59e4540bf14f1607b90ffb81"),
"vacancyNumber" : "1",
"position" : "Software Tester",
"publishDate" : ISODate("2018-01-02T00:00:00.000Z"),
"expireDate" : ISODate("2018-05-29T00:00:00.000Z"),
"yearsOfExperience" : 40,
"minimumEducation" : "Doctorate",
"functionalArea" : "Education",
"company" : {
"id" : ObjectId("59e453fbf14f1607b90ffb80"),
"name" : "First Company",
"profile" : "profile",
"logo" : {
"container" : "companyFiles",
"name" : "abbbff58cd3fda2c59ab2ee620ea5aa0",
"mime" : ".png",
"size" : 5806
}
},
"durations" : {
"years" : 3,
"months" : 4
},
"probationPeriod" : {
"duration" : 34,
"unit" : "month"
},
"salary" : {
"minSalary" : 1000,
"maxSalary" : 2000,
"currency" : "USD",
"period" : "monthly",
"isNegotiable" : true
},
"locations" : [
"Germany",
"Itly",
"Iran"
],
"canApplyOnline" : true,
"skills" : [
"Skill1",
"Skill2",
"Skill3",
"Skill4"
],
"requiredLanguages" : [
"Arabic",
"English",
"Russian",
"Dari",
"French"
],
"keywords" : [
"Key1",
"Key2"
],
"deleted" : false,
"deletedAt" : null,
"isDrafted" : false,
"isPublished" : true,
"requiresTravel" : true,
"gender" : "male",
"nationalities" : [
"afghan"
],
"workType" : "Full Time",
"contractType" : "Permanent",
}
/* 2 */
{
"_id" : ObjectId("59f9402e05d04ebe5653d98f"),
"vacancyNumber" : "1",
"position" : "Software Engineer",
"publishDate" : ISODate("2018-01-03T00:00:00.000Z"),
"expireDate" : ISODate("2018-11-10T00:00:00.000Z"),
"yearsOfExperience" : 40,
"minimumEducation" : "Doctorate",
"functionalArea" : "Education",
"company" : {
"id" : ObjectId("59e453fbf14f1607b90ffb80"),
"name" : "First Company",
"profile" : "profile",
"logo" : {
"container" : "logo container",
"name" : "logo name",
"mime" : "logo mime type",
"size" : 1
}
},
"durations" : {
"years" : 3,
"months" : 4
},
"probationPeriod" : {
"duration" : 34,
"unit" : "month"
},
"salary" : {
"minSalary" : 1000,
"maxSalary" : 2000,
"currency" : "USD",
"period" : "monthly",
"isNegotiable" : true
},
"locations" : [
"Afghanistan",
"Itly",
"Iran"
],
"skills" : [
"Skill1",
"Another Skill"
],
"requiredLanguages" : [
"Arabic",
"English",
"Russian",
"Dari",
"French"
],
"keywords" : [
"Keyword",
"Key1"
],
"deleted" : false,
"deletedAt" : null,
"isDrafted" : false,
"isPublished" : true,
"gender" : "male",
"nationalities" : [
"afghan",
"iranian"
],
"workType" : "Full Time",
"contractType" : "Short-Term",
}
/* 3 */
{
"_id" : ObjectId("5a03235234f7504f13970abd"),
"vacancyNumber" : "1",
"position" : "Software Tester",
"publishDate" : ISODate("2017-10-10T00:00:00.000Z"),
"expireDate" : ISODate("2018-11-25T00:00:00.000Z"),
"yearsOfExperience" : 40,
"minimumEducation" : "Doctorate",
"functionalArea" : "IT Software",
"company" : {
"id" : ObjectId("59e453fbf14f1607b90ffb80"),
"name" : "My First Company",
"profile" : "profile",
"logo" : {
"container" : "logo container",
"name" : "logo name",
"mime" : "logo mime type",
"size" : 1
}
},
"durations" : {
"years" : 3,
"months" : 4
},
"probationPeriod" : {
"duration" : 34,
"unit" : "month"
},
"salary" : {
"minSalary" : 1000,
"maxSalary" : 2000,
"currency" : "USD",
"period" : "monthly",
"isNegotiable" : true
},
"locations" : [
"Germany",
"Itly",
"Iran"
],
"skills" : [
"Skill1",
"Test Skill"
],
"requiredLanguages" : [
"Arabic",
"English",
"Russian",
"Dari",
"French"
],
"keywords" : [
"Test Key",
"Keyword"
],
"deleted" : false,
"deletedAt" : null,
"isDrafted" : false,
"isPublished" : true,
"gender" : "female",
"nationalities" : [
"afghan"
],
"workType" : "Part Time",
"contractType" : "Permanent",
}
Now I want to count the group of data by my custom expression check 'twentyFourHourAgo, fortyEightHourAgo, thirtyDaysAgo', and also by the value of a field (functionalArea, position, locations, keywords, workType).
My current query result is
{
"_id" : null,
"twentyFourHourAgo" : 0,
"fortyEightHourAgo" : 0.0,
"thirtyDaysAgo" : 2.0,
"position" : [
{
"Name" : "Software Engineer",
"Count" : 1.0
},
{
"Name" : "Software Tester",
"Count" : 1.0
}
],
"workType" : [
{
"Name" : "Full Time",
"Count" : 1.0
},
{
"Name" : "Part Time",
"Count" : 1.0
}
],
"functionalArea" : [
{
"Name" : "Education",
"Count" : 1.0
},
{
"Name" : "IT Software",
"Count" : 1.0
}
],
"minimumEducation" : [
{
"Name" : "Doctorate",
"Count" : 1.0
}
],
"gender" : [
{
"Name" : "male",
"Count" : 1.0
},
{
"Name" : "female",
"Count" : 1.0
}
],
"contractType" : [
{
"Name" : "Short-Term",
"Count" : 1.0
},
{
"Name" : "Permanent",
"Count" : 1.0
}
],
"locations" : [
{
"Name" : [
"Afghanistan",
"Itly",
"Iran"
],
"Count" : 1.0
},
{
"Name" : [
"Germany",
"Itly",
"Iran"
],
"Count" : 1.0
}
],
"requiredLanguages" : [
{
"Name" : [
"Arabic",
"English",
"Russian",
"Dari",
"French"
],
"Count" : 1.0
}
],
"company" : [
{
"Name" : "First Company",
"Count" : 1.0
},
{
"Name" : "My First Company",
"Count" : 1.0
}
]
}
As you see, I have three document that has following properties:
Two document that has the same position Software Tester, but query return 1 Software Tester (It means if I have multiple documents that have some common values in specific columns, their count result is wrong). The same problem exists for other fields 'contractType, workType, etc...'.
In array-type fields such as locations, my first document has Germany, Italy, Iran values in locations array, my second document has Afghanistan, Italy, Iran, and my third document has Germany, Italy, Iran. But query result is like this:
"locations" : [
{
"Name" : [
"Afghanistan",
"Itly",
"Iran"
],
"Count" : 1.0
},
{
"Name" : [
"Germany",
"Itly",
"Iran"
],
"Count" : 1.0
}
],
This should be like: Germany => 2, Italy,Iran => 3, and Afghanistan => 1
The same problem exists for other array type fields.
This give you what you were asking:
db.getCollection('foo').aggregate([
{"$match":{
"expireDate":{"$gte": ISODate("2018-01-02T00:00:00.000Z")},
"publishDate":{"$lte": ISODate("2018-05-29T00:00:00.000Z")},
"isPublished":true,"isDrafted":false,
"deletedAt":{"$eq":null},
"deleted":false
}},
{"$group":{
"twentyFourHourAgo":{
"$sum":{
"$cond":[
{"$gte":["$publishDate", ISODate("2018-01-02T00:00:00.000Z")]},1,0
]
}
},
"fortyEightHourAgo":{
"$sum":{
"$cond":[
{"$gte":["$publishDate", ISODate("2018-01-02T00:00:00.000Z")]},1,0
]
}
},
"thirtyDaysAgo":{
"$sum":{
"$cond":[
{"$gte":["$publishDate", ISODate("2018-01-02T00:00:00.000Z")]},1,0
]
}
},
"_id":{
"$ifNull":["$functionalArea","Unknown"]
},
/* Changes start from here */
"count" : { "$sum" : 1 } } },
{ "$group" : {
"_id" : "null", "fortyEightHourAgo" : { "$sum" : "$fortyEightHourAgo"},
"thirtyDaysAgo" : { "$sum" : "$thirtyDaysAgo"},
"twentyFourHourAgo" : { "$sum" : "$twentyFourHourAgo"},
"functionalArea" : { "$addToSet" : { "Name": "$_id", "Count" : "$count" } } }}
])
Output:
{
"_id" : null,
"fortyEightHourAgo" : 3.0,
"thirtyDaysAgo" : 3.0,
"twentyFourHourAgo" : 3.0,
"functionalArea" : [
{
"Name" : "Education",
"Count" : 1.0
},
{
"Name" : "IT Software",
"Count" : 2.0
}
]
}

MongoDB group by multiple fields in a way to not affect each other result

I have the following query, what I want is to have a combined group of custom group field names, with field value.
db.getCollection('mycollection').aggregate([
{"$match":{
"expireDate":{"$gte":"2018-02-06T00:00:00.000Z"},
"publishDate":{"$lte":"2018-02-06T00:00:00.000Z"},
"isPublished":true,"isDrafted":false,
"deletedAt":{"$eq":null},"deleted":false
}},
{"$group":{
"twentyFourHourAgo":{
"$sum":{
"$cond":[
{"$gt":["$publishDate","2018-02-04T08:48:16.892Z"]},1,0
]
}
},
"fortyEightHourAgo":{
"$sum":{
"$cond":[
{"$gt":["$publishDate","2018-02-01T08:48:16.892Z"]},1,0
]
}
},
"thirtyDaysAgo":{
"$sum":{
"$cond":[
{"$gt":["$publishDate","2017-12-31T08:48:16.892Z"]},1,0
]
}
},
"_id":{
"position":{"$ifNull":["$position","Unknown"]},
"workType":{"$ifNull":["$workType","Unknown"]},
"functionalArea":{"$ifNull":["$functionalArea","Unknown"]},
"minimumEducation":{"$ifNull":["$minimumEducation","Unknown"]},
"gender":{"$ifNull":["$gender","Unknown"]},
"contractType":{"$ifNull":["$contractType","Unknown"]},
"locations":{"$ifNull":["$locations","Unknown"]},
"requiredLanguages":{"$ifNull":["$requiredLanguages","Unknown"]},
"company":{"$ifNull":["$company.name","Unknown"]}},"count":{"$sum":1}
}
},
{"$group":{
"_id":null,
"twentyFourHourAgo":{
"$sum":"twentyFourHourAgo"
},
"fortyEightHourAgo":{
"$sum":"$fortyEightHourAgo"
},
"thirtyDaysAgo":{
"$sum":"$thirtyDaysAgo"
},
"position":{"$addToSet":{"Name":"$_id.position","Count":"$count"}},
"workType":{"$addToSet":{"Name":"$_id.workType","Count":"$count"}},
"functionalArea":{
"$addToSet":{"Name":"$_id.functionalArea","Count":"$count"}
},
"minimumEducation":{
"$addToSet":{"Name":"$_id.minimumEducation","Count":"$count"}
},
"gender":{"$addToSet":{"Name":"$_id.gender","Count":"$count"}},"contractType":{"$addToSet":{"Name":"$_id.contractType","Count":"$count"}},"locations":{"$addToSet":{"Name":"$_id.locations","Count":"$count"}},"requiredLanguages":{"$addToSet":{"Name":"$_id.requiredLanguages","Count":"$count"}},"company":{"$addToSet":{"Name":"$_id.company","Count":"$count"}}}}]
)
my document inside collection schema is like:
/* 1 */
{
"_id" : ObjectId("59e4540bf14f1607b90ffb81"),
"vacancyNumber" : "1",
"position" : "Software Tester",
"publishDate" : ISODate("2018-01-02T00:00:00.000Z"),
"expireDate" : ISODate("2018-05-29T00:00:00.000Z"),
"yearsOfExperience" : 40,
"minimumEducation" : "Doctorate",
"functionalArea" : "Education",
"company" : {
"id" : ObjectId("59e453fbf14f1607b90ffb80"),
"name" : "First Company",
"profile" : "profile",
"logo" : {
"container" : "companyFiles",
"name" : "abbbff58cd3fda2c59ab2ee620ea5aa0",
"mime" : ".png",
"size" : 5806
}
},
"durations" : {
"years" : 3,
"months" : 4
},
"probationPeriod" : {
"duration" : 34,
"unit" : "month"
},
"salary" : {
"minSalary" : 1000,
"maxSalary" : 2000,
"currency" : "USD",
"period" : "monthly",
"isNegotiable" : true
},
"locations" : [
"Germany",
"Itly",
"Iran"
],
"canApplyOnline" : true,
"skills" : [
"Skill1",
"Skill2",
"Skill3",
"Skill4"
],
"requiredLanguages" : [
"Arabic",
"English",
"Russian",
"Dari",
"French"
],
"keywords" : [
"Key1",
"Key2"
],
"deleted" : false,
"deletedAt" : null,
"isDrafted" : false,
"isPublished" : true,
"requiresTravel" : true,
"gender" : "male",
"nationalities" : [
"afghan"
],
"workType" : "Full Time",
"contractType" : "Permanent",
}
/* 2 */
{
"_id" : ObjectId("59f9402e05d04ebe5653d98f"),
"vacancyNumber" : "1",
"position" : "Software Engineer",
"publishDate" : ISODate("2018-01-03T00:00:00.000Z"),
"expireDate" : ISODate("2018-11-10T00:00:00.000Z"),
"yearsOfExperience" : 40,
"minimumEducation" : "Doctorate",
"functionalArea" : "Education",
"company" : {
"id" : ObjectId("59e453fbf14f1607b90ffb80"),
"name" : "First Company",
"profile" : "profile",
"logo" : {
"container" : "logo container",
"name" : "logo name",
"mime" : "logo mime type",
"size" : 1
}
},
"durations" : {
"years" : 3,
"months" : 4
},
"probationPeriod" : {
"duration" : 34,
"unit" : "month"
},
"salary" : {
"minSalary" : 1000,
"maxSalary" : 2000,
"currency" : "USD",
"period" : "monthly",
"isNegotiable" : true
},
"locations" : [
"Afghanistan",
"Itly",
"Iran"
],
"skills" : [
"Skill1",
"Another Skill"
],
"requiredLanguages" : [
"Arabic",
"English",
"Russian",
"Dari",
"French"
],
"keywords" : [
"Keyword",
"Key1"
],
"deleted" : false,
"deletedAt" : null,
"isDrafted" : false,
"isPublished" : true,
"gender" : "male",
"nationalities" : [
"afghan",
"iranian"
],
"workType" : "Full Time",
"contractType" : "Short-Term",
}
/* 3 */
{
"_id" : ObjectId("5a03235234f7504f13970abd"),
"vacancyNumber" : "1",
"position" : "Software Tester",
"publishDate" : ISODate("2017-10-10T00:00:00.000Z"),
"expireDate" : ISODate("2018-11-25T00:00:00.000Z"),
"yearsOfExperience" : 40,
"minimumEducation" : "Doctorate",
"functionalArea" : "IT Software",
"company" : {
"id" : ObjectId("59e453fbf14f1607b90ffb80"),
"name" : "My First Company",
"profile" : "profile",
"logo" : {
"container" : "logo container",
"name" : "logo name",
"mime" : "logo mime type",
"size" : 1
}
},
"durations" : {
"years" : 3,
"months" : 4
},
"probationPeriod" : {
"duration" : 34,
"unit" : "month"
},
"salary" : {
"minSalary" : 1000,
"maxSalary" : 2000,
"currency" : "USD",
"period" : "monthly",
"isNegotiable" : true
},
"locations" : [
"Germany",
"Itly",
"Iran"
],
"skills" : [
"Skill1",
"Test Skill"
],
"requiredLanguages" : [
"Arabic",
"English",
"Russian",
"Dari",
"French"
],
"keywords" : [
"Test Key",
"Keyword"
],
"deleted" : false,
"deletedAt" : null,
"isDrafted" : false,
"isPublished" : true,
"gender" : "female",
"nationalities" : [
"afghan"
],
"workType" : "Part Time",
"contractType" : "Permanent",
}
Now I want to count the group of data by my custom expression check 'twentyFourHourAgo, fortyEightHourAgo, thirtyDaysAgo', and also by the value of a field (functionalArea, position, locations, keywords, workType).
My current query result is
{
"_id" : null,
"twentyFourHourAgo" : 0,
"fortyEightHourAgo" : 0.0,
"thirtyDaysAgo" : 2.0,
"position" : [
{
"Name" : "Software Engineer",
"Count" : 1.0
},
{
"Name" : "Software Tester",
"Count" : 1.0
}
],
"workType" : [
{
"Name" : "Full Time",
"Count" : 1.0
},
{
"Name" : "Part Time",
"Count" : 1.0
}
],
"functionalArea" : [
{
"Name" : "Education",
"Count" : 1.0
},
{
"Name" : "IT Software",
"Count" : 1.0
}
],
"minimumEducation" : [
{
"Name" : "Doctorate",
"Count" : 1.0
}
],
"gender" : [
{
"Name" : "male",
"Count" : 1.0
},
{
"Name" : "female",
"Count" : 1.0
}
],
"contractType" : [
{
"Name" : "Short-Term",
"Count" : 1.0
},
{
"Name" : "Permanent",
"Count" : 1.0
}
],
"locations" : [
{
"Name" : [
"Afghanistan",
"Itly",
"Iran"
],
"Count" : 1.0
},
{
"Name" : [
"Germany",
"Itly",
"Iran"
],
"Count" : 1.0
}
],
"requiredLanguages" : [
{
"Name" : [
"Arabic",
"English",
"Russian",
"Dari",
"French"
],
"Count" : 1.0
}
],
"company" : [
{
"Name" : "First Company",
"Count" : 1.0
},
{
"Name" : "My First Company",
"Count" : 1.0
}
]
}
As you see, I have three document that has following properties:
Two document that has the same position Software Tester, but query return 1 Software Tester (It means if I have multiple documents that have some common values in specific columns, their count result is wrong). The same problem exists for other fields 'contractType, workType, etc...'.
In array-type fields such as locations, my first document has Germany, Italy, Iran values in locations array, my second document has Afghanistan, Italy, Iran, and my third document has Germany, Italy, Iran. But query result is like this:
"locations" : [
{
"Name" : [
"Afghanistan",
"Itly",
"Iran"
],
"Count" : 1.0
},
{
"Name" : [
"Germany",
"Itly",
"Iran"
],
"Count" : 1.0
}
],
This should be like: Germany => 2, Italy,Iran => 3, and Afghanistan => 1
The same problem exists for other array type fields.
Apologies I misunderstood your question earlier. To be able to $unwind the location array, but NOT effect your twentyFourHourAgo etc you could look at using $first.
You'll need to $unwind any array if you wish count/sum the individual elements.
Example of how using $first.
db.getCollection('foo').aggregate([
{ $unwind : "$locations" },
{ "$group" : { "_id" : "$_id",
"twentyFourHourAgo":{ $first : {
"$sum" : { "$cond":[
{"$gt":["$publishDate", ISODate("2016-10-10T00:00:00.000Z")]},1,0 ] } } },
"fortyEightHourAgo" : { $first : {
"$sum" : { "$cond" : [
{ "$gt" : [ "$publishDate","2018-01-02T00:00:00.000Z"]},1,0 ] } } },
"thirtyDaysAgo" : { $first : {
"$sum" : { "$cond" : [
{ "$gt" : [ "$publishDate","2017-12-31T08:48:16.892Z"]},1,0 ] } } },
} },
{ "$group" : { "_id" : null,
"twentyFourHourAgo" : { "$sum" : "$twentyFourHourAgo" },
"fortyEightHourAgo" : { "$sum" : "$fortyEightHourAgo" },
"thirtyDaysAgo" : { "$sum" : "$thirtyDaysAgo" },
}}
])
Output:
"_id" : null,
"twentyFourHourAgo" : 0,
"fortyEightHourAgo" : 3.0,
"thirtyDaysAgo" : 3.0,
Please see here $first for further information on why I think it might be of use. I've stuck an $unwind at the beginning to help prove that it will solve the problem in your OP.