How to perform lookup in subdocument array of mongo collection - mongodb

I have 2 collection named ServiceProvider and Parents.
ServiceProvider:
{
"_id" : ObjectId("57235dc9588aec3e318b4567"),
"ServiceProviderID" : 3,
"Title" : "test",
"PostedMessage" : "test",
"TotalComments" : 0,
"TotalShares" : 0,
"TotalThanks" : 0,
"AddedOn" : "2016-04-29",
"LastModifiedOn" : "2016-04-29",
"PostAttachment" : {
"ImagePath" : ""
},
"PostComments" : [
{
"_id" : "14619355772980176",
"UserID" : 3,
"CommentMessage" : "test",
"TotalThanks" : 0,
"AddedOn" : "2016-04-29",
"LastModifiedOn" : "2016-04-29",
"CommentAttachment" : {
"ImagePath" : ""
}
},
{
"_id" : "1461935582156657330",
"UserID" : 3,
"CommentMessage" : "123",
"TotalThanks" : 0,
"AddedOn" : "2016-04-29",
"LastModifiedOn" : "2016-04-29",
"CommentAttachment" : {
"ImagePath" : ""
}
},
{
"_id" : "14619892452027996767",
"UserID" : 3,
"CommentMessage" : "45",
"TotalThanks" : 0,
"AddedOn" : "2016-04-30",
"LastModifiedOn" : "2016-04-30",
"CommentAttachment" : {
"ImagePath" : ""
}
},
{
"_id" : "14619896331049580283",
"UserID" : 3,
"CommentMessage" : "46",
"TotalThanks" : 0,
"AddedOn" : "2016-04-30",
"LastModifiedOn" : "2016-04-30",
"CommentAttachment" : {
"ImagePath" : ""
}
}
],
"Subscriber" : [ ]
}
Parents:
{
"35" : {
"ID" : "133",
"UID" : "7Cv1y3kjutSWgvhtH1Lp9yEDOeiFR0aAbB77uTnEJCR3HSH0s6eC7rEt",
"Name" : "Ash Nan",
"ProfileImagePath" : null
},
"36" : {
"ID" : "134",
"UID" : "2BRcj3xBnNzse7YiudouwmYprilzjkbrC4Yk7v9gPBcs6eC7rEt",
"Name" : "Test 45",
"ProfileImagePath" : "Chrysanthemum_134_1450777239#SIZE#.jpg"
},
"37" : {
"ID" : "135",
"UID" : "BCMMdFjfO7WXzESWQ8Wlnge4gMQoumBxXyY99NIou5UzKrRgs6eC7rEt",
"Name" : "test 45",
"ProfileImagePath" : "Penguins_135_1450777743#SIZE#.jpg"
},
"38" : {
"ID" : "136",
"UID" : "HxFmh7T4balaAbB77UTEgJtrxXyY99zCUEYeaAbB77SF11rKv8ad9FS6Tss6eC7rEt",
"Name" : "test1 test1",
"ProfileImagePath" : null
},
}
How to perform lookup query on Postcomments subdocument with Parents collection?
I want Name field from Parents collection to be inserted in ServiceProvider Postcomments subdocument.
I want query to return following:
{
"_id" : ObjectId("57235dc9588aec3e318b4567"),
"ServiceProviderID" : 3,
"Title" : "test",
"PostedMessage" : "test",
"TotalComments" : 0,
"TotalShares" : 0,
"TotalThanks" : 0,
"AddedOn" : "2016-04-29",
"LastModifiedOn" : "2016-04-29",
"PostAttachment" : {
"ImagePath" : ""
},
"PostComments" : [
{
"_id" : "14619355772980176",
"UserID" : 3,
"UserName" : "Chinmay Kulkarni",
"CommentMessage" : "test",
"TotalThanks" : 0,
"AddedOn" : "2016-04-29",
"LastModifiedOn" : "2016-04-29",
"CommentAttachment" : {
"ImagePath" : ""
}
},
{
"_id" : "1461935582156657330",
"UserID" : 3,
"UserName" : "Chinmay Kulkarni",
"CommentMessage" : "123",
"TotalThanks" : 0,
"AddedOn" : "2016-04-29",
"LastModifiedOn" : "2016-04-29",
"CommentAttachment" : {
"ImagePath" : ""
}
},
{
"_id" : "14619892452027996767",
"UserID" : 3,
"UserName" : "Chinmay Kulkarni",
"CommentMessage" : "45",
"TotalThanks" : 0,
"AddedOn" : "2016-04-30",
"LastModifiedOn" : "2016-04-30",
"CommentAttachment" : {
"ImagePath" : ""
}
},
{
"_id" : "14619896331049580283",
"UserID" : 3,
"UserName" : "Chinmay Kulkarni",
"CommentMessage" : "46",
"TotalThanks" : 0,
"AddedOn" : "2016-04-30",
"LastModifiedOn" : "2016-04-30",
"CommentAttachment" : {
"ImagePath" : ""
}
}
],
"Subscriber" : [ ]
}
I refer to this link but didn't get through. Any help would be appreciated. Thanks!

Related

Update nested documents inside array based on condition

i have the next document and i have to update promotion object according to promotionId
executing i can update, but this query erases the other products that have no the promotion in the condition:
if i use update many i get Error: the update operation document must contain atomic operators.
Please help me to get the error to update but keeping the products that does not have the promo i am updating.
/* 1 */
db.fulFillmentCenter.update({},[
{
$addFields: {
exclusiveProducts: {
$filter: {
input: "$exclusiveProducts",
as: "product",
cond: {
$eq: [
"$$product.promotion.promotionId",
'promo1'
]
}
}
}
}
},
{$set: { "exclusiveProducts.promotion.description": "multi 2"}}
/* 2 */
{
"_id" : 1,
"_class" : "com.walmart.labsmx.exclusiveproducts.model.FulFillmentCenter",
"minDateDelivery" : 9,
"maxDateDelivery" : 2,
"deliveryCost" : 10.0,
"origin" : "string",
"active" : true,
"exclusiveProducts" : [
{
"_id" : NumberLong(1585867035599),
"upc" : "p1",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "debe aparece",
"startDate" : ISODate("2020-02-10T02:47:03.472Z"),
"endDate" : ISODate("2020-04-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035613),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035642),
"upc" : "p2",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-02-10T02:47:03.472Z"),
"endDate" : ISODate("2020-04-10T02:47:03.472Z"),
"active" : false,
"promotion" : {
"_id" : NumberLong(1585867035654),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035607),
"upc" : "p3",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-02-10T02:47:03.472Z"),
"endDate" : ISODate("2020-03-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035668),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035644),
"upc" : "p4",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "debe aparece",
"startDate" : ISODate("2020-02-10T02:47:03.472Z"),
"endDate" : ISODate("2020-05-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035632),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035666),
"upc" : "p5",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-05-10T02:47:03.472Z"),
"endDate" : ISODate("2020-07-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035591),
"promotionId" : "promo1",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035669),
"upc" : "p20",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-05-10T02:47:03.472Z"),
"endDate" : ISODate("2020-07-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035661),
"promotionId" : "promo2",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
},
{
"_id" : NumberLong(1585867035643),
"upc" : "p21",
"totalStock" : 10,
"availableStock" : 10,
"price" : 0.0,
"origin" : "no debe aparece",
"startDate" : ISODate("2020-05-10T02:47:03.472Z"),
"endDate" : ISODate("2020-07-10T02:47:03.472Z"),
"active" : true,
"promotion" : {
"_id" : NumberLong(1585867035612),
"promotionId" : "promo3",
"description" : "string",
"startDate" : ISODate("2020-04-30T17:49:27.205Z"),
"endDate" : ISODate("2020-04-30T17:49:27.205Z"),
"active" : true
}
}
],
"exclusiveStores" : [
{
"storeId" : 123,
"origin" : "string"
}
]
}
This update query updates all the nested documents promotion within the array exclusiveProducts. The value of "promotion.description" is modified with the new value "multi 2", where the "promotion.promotionId" = "promo1".
var new_desc = "multi 2";
db.collection.updateMany(
{ "exclusiveProducts.promotion.promotionId": "promo1" },
{ $set: { "exclusiveProducts.$[pr].promotion.description": new_desc } },
{ arrayFilters: [ { "pr.promotion.promotionId": "promo1" } ] }
)

Inserting values into an array in a nested data mongodb

I have a data set where a documents looks like this.
{
"_id" : ObjectId("5c9873aa0fd62b0c9c84e749"),
"account_id" : "1",
"company_tree" : [
{
"id" : "5d561a07-6d2c-45ae-b249-757aa41e9b2b",
"name" : "dept12",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 1,
"items" : [
{
"id" : "a2aae8b2-24d1-44ee-98a6-29d78835fcfd",
"name" : "dept3",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 0,
"users" : [
{
"id" : "1",
"name" : "admin",
"email" : "admin#admin.com",
"primary_locale" : "nb",
"role_membership" : {
"id" : 1,
"active_status" : true,
"name" : "leader"
}
},
{
"id" : "f18ed780-c3c4-4a43-b1e2-f7b5deef2660",
"name" : "user",
"email" : "tromesh#tromesh.com",
"primary_locale" : "nb",
"role_membership" : {
"id" : 1,
"active_status" : true,
"name" : "user"
}
}
],
"items" : [
{
"id" : "83923ba0-8d0f-4c47-8953-0f9a86a7fcbf",
"name" : "dept4",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 0,
"users" : [
{
"id" : "1",
"name" : "admin",
"email" : "admin#test.com",
"primary_locale" : "nb",
"role_membership" : {
"id" : 1,
"active_status" : true,
"name" : "leader"
}
},
{
"id" : "f18ed780-c3c4-4a43-b1e2-f7b5deef2660",
"name" : "user",
"email" : "tromesh#tromesh.com",
"primary_locale" : "nb",
"role_membership" : {
"id" : 2,
"active_status" : true,
"name" : "user"
}
}
],
"items" : [
{
"id" : "34bfbd43-0d48-4ccf-a1a8-27770ee9048f",
"name" : "dept5",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 0,
"items" : [
{
"id" : "1b8f13af-ff80-46c1-ad59-df454268b4fd",
"name" : "dept6",
"privacy_advisor" : {
"name" : "wfewf",
"email" : "fwefw",
"phone" : "fwef"
},
"controller" : {
"name" : "qwqwerf",
"email" : "fwefew",
"phone" : "fwefwe"
},
"index" : 0,
"statistics" : {
"insight" : {
"value" : "88"
},
"privacyQuality" : {
"value" : "44"
}
}
}
],
"expanded" : true,
"statistics" : {
"privacyQuality" : {
"value" : "56"
}
}
}
],
"expanded" : true,
"selected" : true,
"isAccount" : true
}
],
"expanded" : true,
"statistics" : {
"insight" : {
"value" : "44"
},
"privacyQuality" : {
"value" : "89"
}
},
"isAccount" : true,
"details" : {
"company_name" : "New Company"
},
"public_id" : "e4f5ed70-3be6-11e9-b57b-ed1bd06820e4",
"internal_id" : "e4f5ed70-3be6-11e9-8fde-3df658dfaac7",
"is_public_link_published" : true,
"theme" : {
"primary" : "#3c99df",
"secondary" : "#5cc1ed",
"tertiary" : "#2176b6"
}
}
],
"expanded" : true,
"statistics" : {
"insight" : {
"value" : "75"
}
}
},
//more objects with nested properties.
];
I need to insert data to "users" array in a object in company_tree array checking the name property ("name" : "dept12"). Each object in company_tree array has nested object in "items". I already have the document _id,user data, name of department ("name" : "dept12").
This is the code that i have tried and im kind a stuck here. It would be greate if someone can guide me on hos to update this.
db.getCollection('organization').find({"_id" : ObjectId("5cdbc8d4a3a4280fe86b9085")},{"company_tree":1,'_id': false}).toArray(function (err, result) {
console.log(result);
};

Is there a way to use $Set in inside a $map in a projection

I m trying to update a value inside a map in projection. How can i update a field inside the map? I m trying to do calculation on chargeAmount which will be updated in the loop.
I m using MongoDB shell version v4.0.6 in ubuntu 18.04
{
$project : {
"initialAmount": "$chargeAmount",
"taxedData":{
$map:
{
input: "$rateTaxLevel",
as: "taxIds",
in: {
$map:
{
input: "$taxValidity",
as: "levels",
in: {
//$add: [ "$$taxIds.taxid", "$$levels.taxid" ]
taxingAmount:
{
$cond: [ { $eq: [ "$$taxIds.taxid", "$$levels.taxid" ] },
{ $divide: [ "initialAmount",{ $add: [ 1, "$$levels.tax_validityval" ]} ]
//"$$levels.tax_validityval"
} ,"$$REMOVE"
]
},
"chargeAmount" : "$taxingAmount"
}
}
}
}
}
}
}
data set which used in the project stage
{
"_id" : ObjectId("5caae957beca0f79bddc46d9"),
"uuid" : "urn:uuid:dbc692c7-898c-42c0-ae4d-73f03ed39434:0001PAYMENT93",
"responseTime" : NumberLong(1552589459734),
"direction" : "nb",
"api" : "payment",
"apiID" : "payment_v1",
"applicationName" : "buddhitest",
"applicationId" : NumberInt(93),
"serviceProvider" : "buddhinipun#carbon.super",
"serviceProviderId" : "buddhinipun#carbon.super",
"operatorName" : "OPERATOR1",
"operatorId" : NumberInt(1),
"operation" : "Charge",
"operatorRef" : "ABC-11111113589301",
"chargeAmount" : NumberInt(100),
"category" : "111",
"subcategory" : "222",
"merchant" : "111",
"responseCount" : "1",
"rateCard" : "common_percentage",
"price" : -9.1,
"year" : NumberInt(2019),
"month" : NumberInt(3),
"day" : NumberInt(14),
"hour" : NumberInt(19),
"status" : NumberInt(0),
"statusInfo" : "ok",
"userType" : " Postpaid",
"subRateNB" : [
{
"_id" : ObjectId("5cac45a037c54a0f4ce2b717"),
"sub_rate_nbid" : NumberInt(17),
"api_operationid" : NumberInt(1),
"api_version" : "v1",
"applicationid" : NumberInt(93),
"rate_defid" : NumberInt(30),
"sub_rate_nbactdate" : null,
"sub_rate_nbdisdate" : null,
"createdby" : null,
"createddate" : ISODate("2019-01-16T18:31:18.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2019-01-16T18:31:18.000+0000")
}
],
"rateTaxLevel" : [
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a7"),
"rate_taxesid" : NumberInt(177),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(40),
"level" : NumberInt(1)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a8"),
"rate_taxesid" : NumberInt(178),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(41),
"level" : NumberInt(2)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a9"),
"rate_taxesid" : NumberInt(179),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(1),
"level" : NumberInt(3)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7aa"),
"rate_taxesid" : NumberInt(180),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(66),
"level" : NumberInt(4)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7ab"),
"rate_taxesid" : NumberInt(181),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(68),
"level" : NumberInt(5)
}
],
"taxValidity" : [
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7be"),
"idtax_validityid" : NumberInt(1),
"tax_validityactdate" : ISODate("2018-10-09T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2020-10-09T18:30:00.000+0000"),
"tax_validityval" : 0.02,
"taxid" : NumberInt(1),
"createdby" : null,
"createddate" : ISODate("2018-10-26T13:05:58.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-26T13:05:58.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d5"),
"idtax_validityid" : NumberInt(27),
"tax_validityactdate" : ISODate("2018-10-29T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2035-11-10T18:30:00.000+0000"),
"tax_validityval" : 0.5,
"taxid" : NumberInt(40),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:31:40.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:31:40.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d6"),
"idtax_validityid" : NumberInt(28),
"tax_validityactdate" : ISODate("2018-10-31T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-11-07T18:30:00.000+0000"),
"tax_validityval" : 2.0,
"taxid" : NumberInt(41),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:42:20.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:42:20.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ec"),
"idtax_validityid" : NumberInt(50),
"tax_validityactdate" : ISODate("2018-11-11T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-11-30T18:30:00.000+0000"),
"tax_validityval" : 0.23,
"taxid" : NumberInt(66),
"createdby" : null,
"createddate" : ISODate("2018-11-12T20:22:47.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-12T20:22:47.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ee"),
"idtax_validityid" : NumberInt(52),
"tax_validityactdate" : ISODate("2018-11-30T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-12-07T18:30:00.000+0000"),
"tax_validityval" : 0.01,
"taxid" : NumberInt(68),
"createdby" : null,
"createddate" : ISODate("2018-11-26T15:02:51.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-26T15:02:51.000+0000")
}
]
}
// ----------------------------------------------
{
"_id" : ObjectId("5caae957beca0f79bddc46da"),
"uuid" : "urn:uuid:a566d97b-8032-47d2-aef3-ad5807af3742:0001PAYMENT93",
"responseTime" : NumberLong(1552895806695),
"direction" : "nb",
"api" : "payment",
"apiID" : "payment_v1",
"applicationName" : "buddhitest",
"applicationId" : NumberInt(93),
"serviceProvider" : "buddhinipun#carbon.super",
"serviceProviderId" : "buddhinipun#carbon.super",
"operatorName" : "OPERATOR1",
"operatorId" : NumberInt(1),
"operation" : "Charge",
"operatorRef" : "ABC-123",
"chargeAmount" : NumberInt(10),
"category" : "",
"subcategory" : "",
"merchant" : "",
"responseCount" : "1",
"rateCard" : "common_percentage",
"price" : -0.91,
"year" : NumberInt(2019),
"month" : NumberInt(3),
"day" : NumberInt(18),
"hour" : NumberInt(8),
"status" : NumberInt(0),
"statusInfo" : "ok",
"userType" : " Postpaid",
"subRateNB" : [
{
"_id" : ObjectId("5cac45a037c54a0f4ce2b717"),
"sub_rate_nbid" : NumberInt(17),
"api_operationid" : NumberInt(1),
"api_version" : "v1",
"applicationid" : NumberInt(93),
"rate_defid" : NumberInt(30),
"sub_rate_nbactdate" : null,
"sub_rate_nbdisdate" : null,
"createdby" : null,
"createddate" : ISODate("2019-01-16T18:31:18.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2019-01-16T18:31:18.000+0000")
}
],
"rateTaxLevel" : [
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a7"),
"rate_taxesid" : NumberInt(177),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(40),
"level" : NumberInt(1)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a8"),
"rate_taxesid" : NumberInt(178),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(41),
"level" : NumberInt(2)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a9"),
"rate_taxesid" : NumberInt(179),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(1),
"level" : NumberInt(3)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7aa"),
"rate_taxesid" : NumberInt(180),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(66),
"level" : NumberInt(4)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7ab"),
"rate_taxesid" : NumberInt(181),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(68),
"level" : NumberInt(5)
}
],
"taxValidity" : [
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7be"),
"idtax_validityid" : NumberInt(1),
"tax_validityactdate" : ISODate("2018-10-09T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2020-10-09T18:30:00.000+0000"),
"tax_validityval" : 0.02,
"taxid" : NumberInt(1),
"createdby" : null,
"createddate" : ISODate("2018-10-26T13:05:58.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-26T13:05:58.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d5"),
"idtax_validityid" : NumberInt(27),
"tax_validityactdate" : ISODate("2018-10-29T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2035-11-10T18:30:00.000+0000"),
"tax_validityval" : 0.5,
"taxid" : NumberInt(40),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:31:40.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:31:40.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d6"),
"idtax_validityid" : NumberInt(28),
"tax_validityactdate" : ISODate("2018-10-31T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-11-07T18:30:00.000+0000"),
"tax_validityval" : 2.0,
"taxid" : NumberInt(41),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:42:20.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:42:20.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ec"),
"idtax_validityid" : NumberInt(50),
"tax_validityactdate" : ISODate("2018-11-11T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-11-30T18:30:00.000+0000"),
"tax_validityval" : 0.23,
"taxid" : NumberInt(66),
"createdby" : null,
"createddate" : ISODate("2018-11-12T20:22:47.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-12T20:22:47.000+0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ee"),
"idtax_validityid" : NumberInt(52),
"tax_validityactdate" : ISODate("2018-11-30T18:30:00.000+0000"),
"tax_validitydisdate" : ISODate("2018-12-07T18:30:00.000+0000"),
"tax_validityval" : 0.01,
"taxid" : NumberInt(68),
"createdby" : null,
"createddate" : ISODate("2018-11-26T15:02:51.000+0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-26T15:02:51.000+0000")
}
]
}
// ----------------------------------------------
I got this result using java script function
"------------------------------------"
"l1Tax% : 1.02"
"l1Tax : 98.0392156862745"
"l2Tax% : 1.5"
"l2Tax : 65.359477124183"
"l3Tax% : 3"
"l3Tax : 21.78649237472767"
"l4Tax% : 1.23"
"l4Tax : 17.712595426607862"
"l5Tax% : 1.01"
"l5Tax : 17.537223194661248"
"------------------------------------"
"l1Tax% : 1.02"
"l1Tax : 9.803921568627452"
"l2Tax% : 1.5"
"l2Tax : 6.535947712418301"
"l3Tax% : 3"
"l3Tax : 2.1786492374727673"
"l4Tax% : 1.23"
"l4Tax : 1.7712595426607864"
"l5Tax% : 1.01"
"l5Tax : 1.7537223194661251"
using the below java script function, I was able to get the desired output but there is significant performance drop in the java script approach.
forEach(function (doc) {
var taxableamount = doc.chargeAmount;
var taxedAmmount =0;
printjson("------------------------------------");
for (index = 0; index < doc.rateTaxLevel.length; index++) {
for (index = 0; index < doc.taxValidity.length; index++) {
taxedAmmount = taxableamount / (1+doc.taxValidity[index].tax_validityval);
taxableamount = taxedAmmount;
printjson("l"+doc.rateTaxLevel[index].level+"Tax% : "+(1+doc.taxValidity[index].tax_validityval));
printjson("l"+doc.rateTaxLevel[index].level+"Tax : "+taxableamount);
}
}
}

mongoDb field count query

I have MongoDB collection with the following format:
{
"_id" : ObjectId("5b6b112d1bbd972848b97df4"),
"Game" : [
{
"City" : "Nizhny Novgorod",
"T2N" : "Costa Rica",
"T1N" : "Switzerland",
"ST" : "Nizhny Novgorod Stadium",
"T1S" : "2",
"Date" : "6/27/2018",
"T2S" : "2"
},
{
"City" : "Kaliningrad",
"T2N" : "Serbia",
"T1N" : "Switzerland",
"ST" : "Kaliningrad Stadium",
"T1S" : "2",
"Date" : "6/22/2018",
"T2S" : "1"
},
{
"City" : "Rostov-on-Don",
"T2N" : "Brazil",
"T1N" : "Switzerland",
"ST" : "Rostov Arena",
"T1S" : "1",
"Date" : "6/17/2018",
"T2S" : "1"
},
{
"City" : "Saint Petersburg",
"T2N" : "Sweden",
"T1N" : "Switzerland",
"ST" : "Saint Petersburg Stadium",
"T1S" : "0",
"Date" : "7/3/2018",
"T2S" : "1"
}
],
"team" : "Switzerland"
}
How can I get the total of T1S and Total of T2S for each team?
I am trying the following query
db.test3.aggregate([{$match:{}},{$group:{_id:"$team", goalScored:{$sum:"Game.T1S"},goalConceaded:{$sum:"Game.T2S"}}}])
But I am getting the following output:
{ "_id" : "Tunisia", "goalScored" : 0, "goalConceaded" : 0 }
{ "_id" : "Uruguay", "goalScored" : 0, "goalConceaded" : 0 }
{ "_id" : "Egypt", "goalScored" : 0, "goalConceaded" : 0 }
In the above, the goalscored and goalconceded are always 0 which is not expected. Any help is appreciated.

MongoDB shell mongoimport not importing document from json file

Can someone help me figure out what the problem is with this cos i have been trying to get it work but all efforts avail abortive
mongoimport --db test --collection products --drop --file"C:\Program Files\MongoDB\Server\scripts\products.json"
running the above on my shell keeps given me the below error
2018-01-13T19:55:55.019+0100 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:14
======================Update=========================
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "warranty_years" : 1, "available" : false }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bf9" }, "name" : "AC3 Series Charger", "type" : [ "accessory", "charger" ], "price" : 19, "warranty_years" : 0.25, "for" : [ "ac3", "ac7", "ac9" ] }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfa" }, "name" : "AC3 Case Green", "type" : [ "accessory", "case" ], "color" : "green", "price" : 12, "warranty_years" : 0 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfb" }, "name" : "Phone Extended Warranty", "type" : "warranty", "price" : 38, "warranty_years" : 2, "for" : [ "ac3", "ac7", "ac9", "qp7", "qp8", "qp9" ] }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfc" }, "name" : "AC3 Case Black", "type" : [ "accessory", "case" ], "color" : "black", "price" : 12.5, "warranty_years" : 0.25, "available" : false, "for" : "ac3" }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfd" }, "name" : "AC3 Case Red", "type" : [ "accessory", "case" ], "color" : "red", "price" : 12, "warranty_years" : 0.25, "available" : true, "for" : "ac3" }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bfe" }, "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15bff" }, "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15c00" }, "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "sales_tax" : true, "term_years" : 2 }
{ "_id" : { "$oid" : "507d95d5719dbef170f15c01" }, "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] }