JSON DO
{
"_id" : "t6Y596WHx44S",
"pos_txn_type" : "CREDIT_AUTHORIZATION",
"pos_payment_method" : "CREDIT_CARD",
"processing_status" : "APPROVED",
"route" : NumberInt(7),
"audit_info" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:57.000+0000"),
"updated_on" : ISODate("2016-08-08T07:26:57.000+0000")
},
"inflight_transactions" : [
{
"message_exchange" : {
"service_type" : "GATEWAY_SERVICE",
"adapter_id" : "adpIsoUms",
"route1" : NumberInt(7),
"request" : {
"type" : "com.renovite.ripps.kernel.msg.GatewayRequest",
"acquirer_inst_id" : "00000000001",
"host_address" : "127.0.0.1:2000",
"client_address" : "127.0.0.1:58577",
"domain_request" : {
"type" : "com.renovite.ripps.kernel.msg.CardRequestMessage",
"card" : {
"masked_pan" : "411111xxxxx1111",
"card_type" : "VISA"
},
"transactionAmount" : {
"amount" : NumberInt(100),
"amount_type" : "TXN_AMOUNT",
"currency" : "USD",
"currency_iso" : "840",
"currency_minor_unit" : NumberInt(2)
},
"additionalAmountMap" : {
},
"pos" : {
"stan" : "470641215751",
"entry_mode" : "SWIPED",
"pos_environment" : "ATTENDED",
"card_holder_verification_method" : "UNKNOWN",
"pos_card_holder_verification_method" : "MANUAL_SIGNATURE",
"terminal_capability" : "MSR_MANUAL",
"pos_terminal_id" : "0000000000000001",
"pos_transaction_time" : ISODate("2016-08-08T07:26:55.000+0000"),
"pos_transaction_date" : ISODate("2016-08-08T07:26:55.000+0000"),
"pos_function_code" : "100",
"cp" : true
},
"billTo" : {
"postal_code" : "94538"
}
},
"merchant_details" : {
"merchant_code" : "00000000001",
"merchant_category_code" : "5965",
"merchant_postal_code" : "4567",
"device_code" : "0000000000000001"
},
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:57.000+0000")
},
"additionalAttributes" : {
},
"raw_header" : "ISO.UMS.03",
"retrival_reference_number" : "000001215751"
},
"response" : {
"type" : "com.renovite.ripps.kernel.msg.GatewayResponse",
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:27:01.000+0000")
},
"additionalAttributes" : {
},
"domain_response" : {
"type" : "com.renovite.ripps.kernel.msg.CardResponseMessage",
"auth_code" : "831000",
"amount" : 9601.0,
"currency" : "USD",
"transaction_time" : ISODate("2016-08-08T01:57:00.000+0000"),
"processor_ref_number" : "4706412203976900504007",
"approval_code" : "0000",
"avs_response_code" : "2",
"reconciliation_id" : "4706412203976900504007",
"partial_auth" : false
}
}
}
},
{
"message_exchange" : {
"service_type" : "FRAUD_SERVICE",
"adapter_id" : "cartFRAUD",
"request" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderRequest",
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:58.000+0000")
},
"additionalAttributes" : {
"MerchantTransactionIdentifier" : "t6Y596WHx44S"
},
"retrivalReferenceNumber" : "000001215751",
"merchantDetails" : {
"merchant_code" : "00000000001",
"merchant_category_code" : "5965",
"partial_auth" : "Y"
},
"domain_request" : {
"type" : "com.renovite.ripps.kernel.msg.fraud.FraudRequestMessage",
"client_address" : "127.0.0.1:58577"
}
},
"route" : "6",
"processing_status" : "APPROVED",
"response" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderResponse",
"domain_response" : {
"type" : "com.renovite.ripps.kernel.msg.fraud.FraudResponseMessage",
"fraud_score" : NumberInt(22)
},
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:58.000+0000")
},
"additionalAttributes" : {
}
}
}
},
{
"message_exchange" : {
"service_type" : "AUTH_SERVICE",
"adapter_id" : "cartVACP",
"request" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderRequest",
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:58.000+0000")
},
"additionalAttributes" : {
"COMMERCEINDICATOR" : "retail",
"MerchantTransactionIdentifier" : "t6Y596WHx44S",
"ThirdPartyCertificationNumber" : "575357012698"
},
"retrivalReferenceNumber" : "000001215751",
"merchantDetails" : {
"merchant_code" : "00000000004",
"merchant_category_code" : "5965",
"partial_auth" : "Y"
},
"domain_request" : {
"type" : "com.renovite.ripps.kernel.msg.CardRequestMessage",
"card" : {
"masked_pan" : "411111xxxxx1111",
"card_type" : "VISA"
},
"transactionAmount" : {
"amount" : 9601.0,
"amount_type" : "TXN_AMOUNT",
"currency" : "USD",
"currency_iso" : "840",
"currency_minor_unit" : NumberInt(2)
},
"additionalAmountMap" : {
},
"pos" : {
"entry_mode" : "SWIPED",
"pos_terminal_id" : "0000000087654321",
"cp" : true
},
"billTo" : {
"postal_code" : "94538"
}
},
"merchant_code" : "v5p234p575357"
},
"route" : "3",
"processing_status" : "APPROVED",
"response" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderResponse",
"status" : "ACCEPT",
"domain_response" : {
"type" : "com.renovite.ripps.kernel.msg.CardResponseMessage",
"auth_code" : "831000",
"amount" : 9601.0,
"currency" : "USD",
"transaction_time" : ISODate("2016-08-08T01:57:00.000+0000"),
"processor_ref_number" : "4706412203976900504007",
"approval_code" : "100",
"avs_response_code" : "2",
"reconciliation_id" : "4706412203976900504007",
"partial_auth" : false
},
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:27:01.000+0000")
},
"additionalAttributes" : {
"PYMT_NETWORK_TXN_ID" : "016153570198200",
"RECEIPT_NUMBER" : "138115",
"REQUEST_TOKEN" : "Ahj/7wSR/kUuB8C84NOOelmjdg2aMWTJgzct2zlgwasGjBg3S36RRSYgFLfpFFJi0gp1MyMJsMmkmW6QHhMgQJyP8ilwPgXnBpxwvBkj",
"REASONCODE" : NumberInt(100),
"PROCESSORRESPONSE" : "00",
"CARDCATEGORY" : "A",
"MERCHANTREFERENCECODE" : "000001215751",
"CARDGROUP" : "0"
}
}
}
},
{
"message_exchange" : {
"service_type" : "LOYALTY_SERVICE",
"adapter_id" : "cartLOYALTY",
"request" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderRequest",
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:27:01.000+0000")
},
"additionalAttributes" : {
"MerchantTransactionIdentifier" : "t6Y596WHx44S"
},
"retrivalReferenceNumber" : "000001215751",
"merchantDetails" : {
"merchant_code" : "00000000001",
"merchant_category_code" : "5965",
"partial_auth" : "Y"
},
"domain_request" : {
"type" : "com.renovite.ripps.kernel.msg.loyalty.LoyaltyRequestMessage",
"amount" : 9601.0,
"client_address" : "127.0.0.1:58577"
}
},
"route" : "7",
"processing_status" : "APPROVED",
"response" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderResponse",
"domain_response" : {
"type" : "com.renovite.ripps.kernel.msg.loyalty.LoyaltyResponseMessage",
"loyaltyPoint" : NumberInt(677)
},
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:27:01.000+0000")
},
"additionalAttributes" : {
}
}
}
}
]
}
Below is the native mongo query -
db.txnlog.aggregate([
{
$group : {_id : "$pos_txn_type", count : {$sum : 1}, route_sum : {$push : "$inflight_transactions.0.message_exchange.request.domain_request.transactionAmount.amount"}}
}
])
and sheel output-
{ "_id" : "RECONCILE_REQUEST", "count" : 9, "route_sum" : [ [ ], [ ], [ ], [ ], [ ], [ ], [ ], [ ], [ ] ] }
{ "_id" : "CREDIT_CAPTURE", "count" : 2, "route_sum" : [ [ ], [ ] ] }
{ "_id" : "CREDIT_AUTHORIZATION", "count" : 2, "route_sum" : [ [ ], [ ] ] }
Problem i am not getting amount sum ? Please provide some input.
You can not use the dot notation to access an array element on the group stage, you have to add an extra $project stage to $slice the inflight_transactions array. Here is the query:
db.txnlog.aggregate([
{$project: {
pos_txn_type: 1,
inflight_transaction: {$slice: ["$inflight_transactions", 1]}
}},
{$group: {_id: "$pos_txn_type",
count: {$sum: 1},
route_sum: {$push: "$inflight_transaction.message_exchange.request.domain_request.transactionAmount.amount"}
}
}
]);
// output
{ "_id" : "CREDIT_AUTHORIZATION", "count" : 1, "route_sum" : [ [ 100 ] ] }
Related
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
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
}
}
}
]
}
[
{
"user_id" : 12453,
"profile_type" : "demo_type_1",
"records" : [
{
"type" : "typ_11",
"value" : {
"high" : 115,
"low" : 78
},
"_meta" : {
"data_type" : "text"
}
},
{
"type" : "type_1",
"files" : [
{
"title" : "path_prescription_1",
"url" : "/file_name.extension"
},
{
"title" : "path_prescription_2",
"url" : "/file_name__1.extension"
}
],
"_meta" : {
"data_type" : "file"
}
}
]
},
{
"user_id" : 12455,
"profile_type" : "demo_type_1",
"records" : [
{
"type" : "typ_11",
"value" : {
"high" : 115,
"low" : 78
},
"_meta" : {
"data_type" : "text"
}
},
{
"type" : "type_1",
"files" : [
{
"title" : "path_prescription_1",
"url" : "/file_name.extension"
},
{
"title" : "path_prescription_2",
"url" : "/file_name__1.extension"
}
],
"_meta" : {
"data_type" : "file"
}
}
]
},
...
]
i want to append prefix the url value to make it an absolute path while retrieving from the database only when the _meta fields data_type is file not for text. The is stored in the above format and retrieved in the same format only with the appended url.
is there any way to do this by using aggregation pipeline ?
You can try using $addFields like this:
db.getCollection('test').aggregate(
[
{
$unwind: {
path:"$records",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
path:"$records.files",
preserveNullAndEmptyArrays: true
}
},
{
$addFields: {
"records.files.url":{
$cond: {
if: {
$eq: ['$records.files', undefined]
},
then: null,
else: {$concat: ["some_prefix", "$records.files.url"]}
}
}
}
}
]
)
This will give you:
/* 1 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "typ_11",
"value" : {
"high" : 115.0,
"low" : 78.0
},
"_meta" : {
"data_type" : "text"
},
"files" : {
"url" : null
}
}
}
/* 2 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_1",
"url" : "some_prefix/file_name.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}
/* 3 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_2",
"url" : "some_prefix/file_name__1.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}
/* 4 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "typ_11",
"value" : {
"high" : 115.0,
"low" : 78.0
},
"_meta" : {
"data_type" : "text"
},
"files" : {
"url" : null
}
}
}
/* 5 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_1",
"url" : "some_prefix/file_name.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}
/* 6 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_2",
"url" : "some_prefix/file_name__1.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}
But remember to ignore records.files.url field when the record is type text.
Here is my actual database schema.
company_id is reference object of companies collection and booking_days.consultants.consultant_id is reference object of users collection.
I want to join embedded document with company_id and booking_days.consultants.consultant_id.
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec8")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec7")
}
]
},
{
"booking_date" : ISODate("2018-02-02T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec4"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec5")
}
]
},
],
"__v" : 0
}
I am using below query.
db.getCollection('booking_days').aggregate(
[
{ $match: { company_id:ObjectId("5a6eb43f437e6a0d9e00c92f") } },
{
$lookup: {
localField: "company_id",
from: "companies",
foreignField: "_id",
as: "companies"
},
},
{
$lookup: {
localField: "booking_days.consultants.consultant_id",
from: "users",
foreignField: "_id",
as: "userssss"
},
},
{
$unwind:"$companies"
},
]
)
Actual Output
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
]
},
],
"__v" : 0,
"companies" : {
"_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"first_name" : "Adrienne Runolfsson",
},
"users" : [
{
"_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"first_name" : "Christ Hamill",
},
{
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
]
}
Excepted output. consultant data will come in booking_days.consultants array.
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"first_name" : "Christ Hamill",
},
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
]
},
],
"__v" : 0,
"companies" : {
"_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"first_name" : "Adrienne Runolfsson",
},
}
As such you have to $unwind the localField when it is an embedded document array expect in some cases where localField is an array of scalar ids.
$unwind twice as consultant array is two levels deep followed by $lookup to get the name and $group to get back the expected output.
db.getCollection('booking_days').aggregate([
{"$match":{"company_id":ObjectId("5a6eb43f437e6a0d9e00c92f")}},
{"$lookup":{"localField":"company_id","from":"companies","foreignField":"_id","as":"companies"}},
{"$unwind":"$companies"},
{"$unwind":"$booking_days"},
{"$unwind":"$consultants"},
{"$lookup":{
"localField":"booking_days.consultants.consultant_id",
"from":"users",
"foreignField":"_id",
"as":"booking_days.consultants.consultant_id"
}},
{"$group":{
"_id":{"_id":"$_id","booking_days_id":"$booking_days._id"},
"company_id":{"$first":"$company_id"},
"booking_date":{"$first":"$booking_days.booking_date"},
"companies":{"$first":"$companies"},
"consultants":{"$push":"$booking_days.consultants"}
}},
{"$group":{
"_id":"$_id._id",
"company_id":{"$first":"$company_id"},
"companies":{"$first":"$companies"},
"booking_days":{
"$push":{
"_id":"$_id.booking_days_id",
"booking_date":"$booking_date",
"consultants":"$consultants"
}
}
}}
])
{"Id": "5b87a4c79a9c3feac943fc6c",
"comments" : [
{
"likes" : [],
"_id" : ObjectId("5b87a4c79a9c3feac943fc6c"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"commentId" : "7d2a05d1-2026-4a13-a5c1-318ed80d1b38",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
}
]
},
{
"likes" : [],
"_id" : ObjectId("5b87c301c8a07efa2599c29e"),
"comment" : "testing",
"accountId" : "cfd29f53-d73e-480c-9cfa-ea42b4119266",
"commentId" : "0676047b-1712-4f70-89d5-29c1abe03eaf",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
}
]
}
]
}
accountId is in differnt connection
// Expected Out Put
{"Id": "5b87a4c79a9c3feac943fc6c",
"comments" : [
{
"likes" : [],
"_id" : ObjectId("5b87a4c79a9c3feac943fc6c"),
"comment" : "string",
"name" : "apple",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"name" : "apple",
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"name" : "apple",
}
]
},
{
"likes" : [],
"_id" : ObjectId("5b87c301c8a07efa2599c29e"),
"comment" : "testing",
"name" : "ball",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"name" : "apple",
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"name" : "apple", }
]
}
]
}
I want to find all the values from a subdocument like this:
{ "_id" : ObjectId("XXXXXXXXXXXX"), "consumers" : { "AAAAAAAA" : { "CLIENT" : { "AA" : true } } }, "country" : "ES", "history" : [ ], "last_time_updated" : ISODate("2014-11-28T13:32:19.948Z"), "msisdn" : "123", "operator" : "ES", "time_created" : ISODate("2014-11-28T13:32:19.948Z") }
{ "_id" : ObjectId("XXXXXXXXXXXX"), "consumers" : { "AAAAAAAA" : { "CLIENT" : { "BB" : true } } }, "country" : "ES", "history" : [ ], "last_time_updated" : ISODate("2014-11-28T13:32:19.971Z"), "msisdn" : "123", "operator" : "ES", "time_created" : ISODate("2014-11-28T13:32:19.971Z") }
{ "_id" : ObjectId("XXXXXXXXXXXX"), "consumers" : { "AAAAAAAA" : { "CLIENT" : { "CC" : false } } }, "country" : "ES", "history" : [ ], "last_time_updated" : ISODate("2014-11-28T13:32:19.977Z"), "msisdn" : "123", "operator" : "ES", "time_created" : ISODate("2014-11-28T13:32:19.977Z") }
That include all the values from "CLIENT" that i don't know, i am triying with:
db.collection.find({"consumers" : { "AAAAAAAA" : { "CLIENT" : { $exists : true } } }})
But is not a valid query, please some help?
Thank you very much.
Dot notation can be used to match by fields in a sub document.
db.collection.find({"consumers.AAAAAAAA.CLIENT": {"$exists":true}})