How to merge records in mongodb with aggregate - mongodb

I have a collection named "timestamp" and each time_in, time_out, break_start, and break_end has separate record.
What i want is to merge records with the same date.
From this:
Unmerged Data:
{
"_id" : ObjectId("5a94f3e461613268eaecc59c"),
"staff_id" : NumberInt(173),
"time_in" : ISODate("2018-02-27T00:40:00.000+0000"),
"time_out" : null,
"break_start" : null,
"break_end" : null,
"break_type" : null,
"train_delay" : true,
"createdAt" : ISODate("2018-02-27T00:40:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T00:40:00.000+0000"),
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5a94f411e5e2d568f1dae527"),
"staff_id" : NumberInt(173),
"time_in" : null,
"time_out" : null,
"break_start" : ISODate("2018-02-27T04:00:00.000+0000"),
"break_end" : null,
"break_type" : "60m",
"train_delay" : false,
"createdAt" : ISODate("2018-02-27T04:00:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T04:00:00.000+0000"),
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5a94f41ae5e2d568f1dae528"),
"staff_id" : NumberInt(173),
"time_in" : null,
"time_out" : null,
"break_start" : null,
"break_end" : ISODate("2018-02-27T04:59:00.000+0000"),
"break_type" : null,
"train_delay" : false,
"createdAt" : ISODate("2018-02-27T04:59:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T04:59:00.000+0000"),
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5a94f421e5e2d568f1dae529"),
"staff_id" : NumberInt(173),
"time_in" : null,
"time_out" : ISODate("2018-02-27T10:05:00.000+0000"),
"break_start" : null,
"break_end" : null,
"break_type" : null,
"train_delay" : false,
"createdAt" : ISODate("2018-02-27T10:05:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T10:05:00.000+0000"),
"__v" : NumberInt(0)
}
Here's the image of:
Not yet merge records.
Into this:
Merged Data:
{
"_id" : ObjectId("5a94f3e461613268eaecc59c"),
"staff_id" : NumberInt(173),
"time_in" : {
"_id" : ObjectId("5a94f3e461613268eaecc59c"),
"staff_id" : NumberInt(173),
"time_in" : ISODate("2018-02-27T00:40:00.000+0000"),
"time_out" : ISODate("2018-02-27T10:05:00.000+0000"),
"break_start" : ISODate("2018-02-27T04:00:00.000+0000"),
"break_end" : ISODate("2018-02-27T04:59:00.000+0000"),
"break_type" : null,
"train_delay" : true,
"createdAt" : ISODate("2018-02-27T00:40:00.000+0000"),
"updatedAt" : ISODate("2018-02-27T00:40:00.000+0000"),
"__v" : NumberInt(0)
}
Here's the image of:
Merged records

Related

Deleting an object inside another object in Mongodb and for GOlang

I wanna delete a specific object inside an object, My object:
{
"_id" : ObjectId("5f577f3cce031ee00f5e32c9"),
"UserId" : 0,
"firstname" : "user 1",
"lastname" : "user 1",
"finishedTrainings" : [
{
"itemId" : 3,
"validationScore" : 1,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:57:31.275Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
},
{
"itemId" : 0,
"validationScore" : 0.6666666666666666,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:59:04.268Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
}
],
"biography" : ""
}
and I wanna delete the finishedTraining(id=3),what would be the syntax in mongodb
//code output from mongo shell 4.2, windows10
//data set as given in problem statement
> db.userTraining.find().pretty();
{
"_id" : ObjectId("5f577f3cce031ee00f5e32c9"),
"UserId" : 0,
"firstname" : "user 1",
"lastname" : "user 1",
"finishedTrainings" : [
{
"itemId" : 3,
"validationScore" : 1,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:57:31.275Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
},
{
"itemId" : 0,
"validationScore" : 0.6666666666666666,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:59:04.268Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
}
],
"biography" : ""
}
//assuming objectID is unique for the document, you can query your own business field
> db.userTraining.update(
... {_id:ObjectId("5f577f3cce031ee00f5e32c9")},
... {$pull:{"finishedTrainings":{itemId:3}}},
... false,
... true
... );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.userTraining.find().pretty();
{
"_id" : ObjectId("5f577f3cce031ee00f5e32c9"),
"UserId" : 0,
"firstname" : "user 1",
"lastname" : "user 1",
"finishedTrainings" : [
{
"itemId" : 0,
"validationScore" : 0.6666666666666666,
"timestamps" : {
"createdat" : ISODate("2020-09-09T12:59:04.268Z"),
"createdby" : 0,
"updatedat" : ISODate("0001-01-01T00:00:00Z"),
"updatedby" : 0
},
"isValidated" : true
}
],
"biography" : ""
}
>
//above id=3 is deleted object from the array, other document object remain intact by using "$pull" in the update statement

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);
}
}
}

How to retrieve document from collection if the ObjectId is null?

I have two collections FormSubmissions and SubmissionLogs
FormSubmissions schema has following record
{
"_id" : ObjectId("5b56bc8b9b16f0172828f951"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "89cd1929-bff9-9a6c-a60b-08bec5966d13",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : ObjectId("5ac5b80b19e1011e64766698"),
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}{
"_id" : ObjectId("5b56c06a9b16f0172828f956"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "05f79cc4-ae85-7bab-4882-9dac416063f9",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}{
"_id" : ObjectId("5b56ef414efb640d104105f9"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "d7219aff-3b8d-2931-8ca7-b790e0d1b1f4",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}{
"_id" : ObjectId("5b56ef4d4efb640d104105fd"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "ca213664-976e-8c5d-6d3c-272931ad2dfa",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}{
"_id" : ObjectId("5b595c2999c26d061ceff3d1"),
"LanguageId" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
"FolderName" : "cde6767f-ebf2-6892-97d8-d8c9339b389d",
"IsSync" : false,
"UpdatedBy" : 0,
"IsCompleted" : true,
"IsDeleted" : false,
"EmployeeId" : null,
"Longitude" : null,
"Latitude" : null,
"DeviceId" : "null",
"AppInfoId" : null,
"__v" : 0
}
SubmissionLogs schema has following record
{
"_id" : ObjectId("5b56bc8c9b16f0172828f954"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b56bc8b9b16f0172828f951"),
"EmployeeId" : ObjectId("5ac5b80b19e1011e64766698"),
"UpdatedOn" : ISODate("2018-07-24T05:43:40.290Z"),
"CreatedOn" : ISODate("2018-07-24T05:43:40.290Z"),
"__v" : 0
}{
"_id" : ObjectId("5b56c06b9b16f0172828f959"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b56c06a9b16f0172828f956"),
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"UpdatedOn" : ISODate("2018-07-24T06:00:11.449Z"),
"CreatedOn" : ISODate("2018-07-24T06:00:11.449Z"),
"__v" : 0
}{
"_id" : ObjectId("5b56ef414efb640d104105fc"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b56ef414efb640d104105f9"),
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"UpdatedOn" : ISODate("2018-07-24T09:20:01.930Z"),
"CreatedOn" : ISODate("2018-07-24T09:20:01.930Z"),
"__v" : 0
}{
"_id" : ObjectId("5b56ef4d4efb640d10410600"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b56ef4d4efb640d104105fd"),
"EmployeeId" : ObjectId("5b56bc429b16f0172828f94d"),
"UpdatedOn" : ISODate("2018-07-24T09:20:13.948Z"),
"CreatedOn" : ISODate("2018-07-24T09:20:13.948Z"),
"__v" : 0
}{
"_id" : ObjectId("5b595c2999c26d061ceff3d4"),
"FormId" : ObjectId("5b56bbb19b16f0172828f948"),
"SubmissionId" : ObjectId("5b595c2999c26d061ceff3d1"),
"EmployeeId" : null,
"UpdatedOn" : ISODate("2018-07-26T05:29:13.991Z"),
"CreatedOn" : ISODate("2018-07-26T05:29:13.991Z"),
"__v" : 0
}
I want only that SubmissionLogs which i'm passing EmployeeId (in array-null also)
for that i'm doing this,
db.FormSubmissions.aggregate(
[{
$match : {
$and : [{
FormId : ObjectId("5b56bbb19b16f0172828f948")
}, {
IsDeleted : false
}
]
}
}, {
$lookup : {
from : "SubmissionLogs",
localField : "_id",
foreignField : "SubmissionId",
as : "SubmissionLogs"
}
}, {
$addFields : {
SubmissionLog : {
$filter : {
input : "$SubmissionLogs",
as : "submissionLogs_field",
cond : {
$setIsSubset : [
["$$submissionLogs_field.EmployeeId"],
["5b56bc429b16f0172828f94d", null]
]
}
}
}
}
}, {
$project : {
SubmissionLog : "$SubmissionLog"
}
}
]).pretty()
I want only that records which have ["5b56bc429b16f0172828f94d", null] EmployeeId in SubmissionLogs.I also want that records if EmployeeId has null.
It's working fine $group $push $in
db.FormSubmissions.aggregate([
{
$match : { IsDeleted : false}
},
{
$lookup : {
from : "SubmissionLogs",
localField : "_id",
foreignField : "SubmissionId",
as : "SubmissionLogs"
}
},
{
"$unwind" : "$SubmissionLogs"
},
{
"$match" : {"SubmissionLogs.EmployeeId" : {"$in" : [ObjectId("5b56bc429b16f0172828f94d"), null]}}
},
{"$group" : {"_id" : "$_id",SubmissionLog : {"$push" : "$SubmissionLogs"}}}
])

Issue with firebase query

My query:
let query = recentRef.queryOrderedByChild(FRECENT_GROUPID).queryEqualToValue(group_id)
query.observeSingleEventOfType(.Value, withBlock: { snapshot in
And database structure is :
And my query looks like:
(/Recent {
ep = fb343534ca520c70fe35b0a316ea8e4c;
i = groupId;
sp = fb343534ca520c70fe35b0a316ea8e4c;
})
and getting Snap (Recent) <null> when I print(snapshot).
Its strange that it was working fine but now its suddenly stopped working.
EDIT:
Complete JSON:
{
"Message" : {
"fb343534ca520c70fe35b0a316ea8e4c" : {
"-Kp0jed1EZ5BLllL5_cm" : {
"createdAt" : 1.500046597341153E9,
"groupId" : "fb343534ca520c70fe35b0a316ea8e4c",
"objectId" : "-Kp0jed1EZ5BLllL5_cl",
"senderId" : "lI6SRppSboScWo5xVjcfLL82Ogr2",
"senderName" : "Test1 Test1",
"status" : "",
"text" : "hi",
"type" : "text",
"updatedAt" : 1.50004659734136E9
}
}
},
"Recent" : {
"-Kp0jecwejhzQbbm62CW" : {
"counter" : 0,
"createdAt" : 1.500046600967624E9,
"description" : "Test1 Test1",
"groupId" : "fb343534ca520c70fe35b0a316ea8e4c",
"lastMessage" : "hi",
"members" : [ "lI6SRppSboScWo5xVjcfLL82Ogr2", "fnRvHFpaoDhXqM1se7NoTSiWZIZ2" ],
"objectId" : "-Kp0jecwejhzQbbm62CV",
"picture" : "",
"type" : "private",
"updatedAt" : 1.500046600967647E9,
"userId" : "fnRvHFpaoDhXqM1se7NoTSiWZIZ2"
},
"-Kp0jed-FU1PXt1iPr29" : {
"counter" : 0,
"createdAt" : 1.500046600971885E9,
"description" : "Srikant Root",
"groupId" : "fb343534ca520c70fe35b0a316ea8e4c",
"lastMessage" : "hi",
"members" : [ "lI6SRppSboScWo5xVjcfLL82Ogr2", "fnRvHFpaoDhXqM1se7NoTSiWZIZ2" ],
"objectId" : "-Kp0jed-FU1PXt1iPr28",
"picture" : "https://s3.amazonaws.com/top500golfdev/uploads/profile/srikant.yadav#rootinfosol.com/profilepicture.jpg",
"type" : "private",
"updatedAt" : 1.500046600971896E9,
"userId" : "lI6SRppSboScWo5xVjcfLL82Ogr2"
}
},
"User" : {
"fnRvHFpaoDhXqM1se7NoTSiWZIZ2" : {
"createdAt" : 1.500045753102713E9,
"email" : "srikant.yadav#rootinfosol.com",
"firstname" : "Srikant",
"fullname" : "Srikant Yadav",
"handle" : "Srikant",
"lastname" : "Yadav",
"networkImage" : "https://s3.amazonaws.com/top500golfdev/uploads/profile/srikant.yadav#rootinfosol.com/profilepicture.jpg",
"objectId" : "fnRvHFpaoDhXqM1se7NoTSiWZIZ2",
"online" : false,
"updatedAt" : 1.500045753102731E9
},
"lI6SRppSboScWo5xVjcfLL82Ogr2" : {
"createdAt" : 1.500045791892967E9,
"email" : "test1#gmail.com",
"firstname" : "Test1",
"fullname" : "Test1 Test1",
"handle" : "test1",
"lastname" : "Test1",
"networkImage" : "",
"objectId" : "lI6SRppSboScWo5xVjcfLL82Ogr2",
"online" : false,
"updatedAt" : 1.500046571456235E9
}
}
}

$nearSphere returns too many data

I noticed that $near and $nearSphere returns completely different data and I got suspicious. After all, when latitude is near equator it sphere distance shouldn't differ a lot from euclidean distance.
db.tablebusiness.find({ "LongitudeLatitude" : { "$nearSphere" : [106.772835, -6.186753], "$maxDistance" : 0.053980478460939611 }, "Prominent" : { "$gte" : 15 }, "indexContents" : { "$all" : [/^soto/, /^nasi/] } }).limit(200);
It returns 48 data.
I'll attached it anyway
/* 0 */
{
"_id" : "nasi-uduk-soto-ayam__-6.18_106.77",
"BuildingID" : null,
"Title" : "Nasi Uduk Soto Ayam",
"InBuildingAddress" : null,
"Building" : null,
"Street" : "Jl. Panjang",
"Districts" : [],
"City" : "Jakarta",
"Country" : "Indonesia",
"LongitudeLatitudeFromGoogle" : null,
"DistanceFromGoogleAddress" : 0.0,
"Checkin" : 0,
"Note" : null,
"PeopleCount" : 0,
"Prominent" : 45.5,
"CountViews" : 0,
"StreetAdditional" : null,
"LongitudeLatitude" : {
"Longitude" : 106.765673160553,
"Latitude" : -6.17522230915668
},
"Rating" : {
"Stars" : 0.0,
"Weight" : 0.0
},
"CurrentlyWorkedURL" : null,
"Reviews" : [],
"ZIP" : null,
"Tags" : ["Restaurant"],
"Phones" : [],
"Website" : null,
"Email" : null,
"Price" : null,
"openingHour" : null,
"Promotions" : [],
"SomethingWrong" : false,
"BizMenus" : [],
"Brochures" : [],
"Aliases" : [],
"indexContents" : ["restaura", "estauran", "staurant", "taurant", "aurant", "urant", "rant", "ant", "nt", "t", "nasi", "asi", "si", "i", "uduk", "duk", "uk", "k", "soto", "oto", "to", "o", "ayam", "yam", "am", "m"]
}
/* 1 */
{
"_id" : "nasi-soto-padang_pasar-slipi-jaya_-6.19_106.80",
"BuildingID" : null,
"Title" : "Nasi Soto Padang",
"InBuildingAddress" : "Lt.1 Los Alas",
"Building" : null,
"Street" : "Jl.Kemanggisan Utama Raya",
"Districts" : [],
"City" : "Jakarta",
"Country" : "Indonesia",
"LongitudeLatitudeFromGoogle" : null,
"DistanceFromGoogleAddress" : 0.0,
"Checkin" : 0,
"Note" : null,
"PeopleCount" : 0,
"Prominent" : 45.5,
"CountViews" : 0,
"StreetAdditional" : null,
"LongitudeLatitude" : {
"Longitude" : 106.79647564888,
"Latitude" : -6.18998465381734
},
"Rating" : {
"Stars" : 0.0,
"Weight" : 0.0
},
"CurrentlyWorkedURL" : null,
"Reviews" : [],
"ZIP" : null,
"Tags" : ["Restaurant"],
"Phones" : [],
"Website" : null,
"Email" : null,
"Price" : null,
"openingHour" : null,
"Promotions" : [],
"SomethingWrong" : false,
"BizMenus" : [],
"Brochures" : [],
"Aliases" : [],
"indexContents" : ["restaura", "estauran", "staurant", "taurant", "aurant", "urant", "rant", "ant", "nt", "t", "nasi", "asi", "si", "i", "soto", "oto", "to", "o", "padang", "adang", "dang", "ang", "ng", "g"]
}
...
/* 47 */
{
"_id" : "nasi-gandul__-7.43_109.24",
"BuildingID" : null,
"Title" : "Nasi Gandul",
"InBuildingAddress" : null,
"Building" : null,
"Street" : "Jl. Brobahan Pr - 40 rt 004 Rw 004",
"Districts" : [],
"City" : "Purwokerto",
"Country" : "Indonesia",
"LongitudeLatitudeFromGoogle" : null,
"DistanceFromGoogleAddress" : 0.0,
"Checkin" : 0,
"Note" : null,
"PeopleCount" : 0,
"Prominent" : 30.5,
"CountViews" : 0,
"StreetAdditional" : null,
"LongitudeLatitude" : {
"Longitude" : 109.239182174206,
"Latitude" : -7.42585664273589
},
"Rating" : {
"Stars" : 3.0,
"Weight" : 1.0
},
"CurrentlyWorkedURL" : null,
"Reviews" : [],
"ZIP" : "53116",
"Tags" : ["Angkringan", "Restaurant Indonesian", "Soto & Sop"],
"Phones" : ["+62(281)7918181"],
"Website" : null,
"Email" : null,
"Price" : null,
"openingHour" : null,
"Promotions" : [],
"SomethingWrong" : false,
"BizMenus" : [],
"Brochures" : [],
"Aliases" : [],
"indexContents" : ["angkring", "ngkringa", "gkringan", "kringan", "ringan", "ingan", "ngan", "gan", "an", "n", "restaura", "estauran", "staurant", "taurant", "aurant", "urant", "rant", "ant", "nt", "t", "indonesi", "ndonesia", "donesian", "onesian", "nesian", "esian", "sian", "ian", "soto", "oto", "to", "o", "&", "sop", "op", "p", "nasi", "asi", "si", "i", "gandul", "andul", "ndul", "dul", "ul", "l"]
}
I put the data into excel and compute the distance using this formula
=SQRT(POWER(E2-$G$1,2)+POWER((F2-$H$1)*COS($H$1*PI()/180),2))
That one should greatly approximate spherical distance. Also notice that latitude is -6 which is not far from equator.
Here is the result
0.013516826
0.023857967
0.037658667
0.038737146
0.042414787
0.046725248
0.051006427
0.053567221
0.057448344
0.061592999
0.062329244
0.065276161
0.066035611
0.076251787
0.109671831
0.112097201
0.13417281
0.136471939
0.172293693
1.058802838
1.078123028
1.079160684
1.080954023
1.081148114
1.081099449
1.092061283
1.094281476
1.094431917
1.096845722
1.097063729
1.096953691
1.097201996
1.105389179
1.105442127
1.10839237
1.108717834
1.108840349
1.111636423
1.113187903
1.118767984
1.118767984
1.133952371
1.135077548
1.154967917
1.161142923
1.185994885
1.199509086
2.756884824
Here is the screenshot of my excel sheet
Here is the actual excel
106.772835 -6.186753
"_id" "nasi-uduk-soto-ayam__-6.18_106.77", 106.7656732 -6.175222309 0.013516826
"_id" "nasi-soto-padang_pasar-slipi-jaya_-6.19_106.80", 106.7964756 -6.189984654 0.023857967
"_id" "nasi-uduk-soto-ayam__-6.22_106.77", 106.7718959 -6.224620499 0.037658667
"_id" "nasi-campur-%26-soto-babat-kenanga_komp.-ruko-permata-senayan%2C_-6.22_106.79", 106.79199 -6.22062 0.038737146
"_id" "soto-mie-nasi_indomaret-(univ.-mercu-buana)_-6.21_106.74", 106.7379928 -6.211082144 0.042414787
"_id" "soto-kudus%2C-soto-ayam-%26amp%3B-nasi-pindang_plaza-senayan_-6.23_106.80", 106.7989969 -6.225694167 0.046725248
"_id" "soto-kudus%2C-soto-ayam-%26amp%3B-nasi-pindang_plaza-bri-ii_-6.22_106.81", 106.8136847 -6.217476287 0.051006427
"_id" "soto-kudus%2C-soto-ayam-%26amp%3B-nasi-pindang_automall-indonesia_-6.23_106.81", 106.8096431 -6.225898946 0.053567221
"_id" "soto-kudus%2C-soto-ayam-%26amp%3B-nasi-pindang_menara-mulia_-6.22_106.82", 106.81656 -6.224232983 0.057448344
"_id" "nasi-gule-gandaria__-6.24_106.80", 106.795285 -6.244444863 0.061592999
"_id" "nasi-soto-monggo-mampir__-6.24_106.80", 106.7971516 -6.244479441 0.062329244
"_id" "nasi-soto-ayam-\"pak-min\"__-6.24_106.82", 106.8151546 -6.236743573 0.065276161
"_id" "nasi-soto-ayam-pak-min---santa__-6.24_106.81", 106.812959 -6.239508 0.066035611
"_id" "soto-mie-nasi-bogor__-6.23_106.83", 106.8322349 -6.234845139 0.076251787
"_id" "nasi-uduk-soto-ayam__-6.24_106.87", 106.8693781 -6.239089942 0.109671831
"_id" "soto-ayam-%2F-nasi-uduk-betawi__-6.28_106.71", 106.714092 -6.282785 0.112097201
"_id" "soto-minang-roda-jaya_ruko-tongkol-indah_-6.12_106.89", 106.889044 -6.119294471 0.13417281
"_id" "nasi-bebek-dan-soto-mas-muchlis_summarecon-kelapa-gading_-6.16_106.91", 106.9059021 -6.156281135 0.136471939
"_id" "nasi-liwet-%26-soto-kwali-kalimalang__-6.25_106.93", 106.9338112 -6.248527508 0.172293693
"_id" "soto-ayam-nasi-rames__-6.88_107.58", 107.5751638 -6.881692483 1.058802838
"_id" "nasi-liwet-soto-solo_rumah-mode_-6.88_107.60", 107.5998616 -6.882452292 1.078123028
"_id" "nasi-soto-ayam-madura__-6.91_107.57", 107.5749707 -6.912900474 1.079160684
"_id" "nasi-soto-mie__-6.89_107.60", 107.5969434 -6.890355658 1.080954023
"_id" "nasi-rames-soto-bandung-enjoi__-6.89_107.60", 107.5970721 -6.890504777 1.081148114
"_id" "nasi-soto-ayam__-6.91_107.58", 107.5769877 -6.913560825 1.081099449
"_id" "nasi-soto-ayam__-6.91_107.60", 107.5977552 -6.90656 1.092061283
"_id" "nasi-soto-ayam-madura__-6.89_107.62", 107.6157832 -6.888615935 1.094281476
"_id" "nasi-goreng-soto-ayam__-6.90_107.61", 107.6093245 -6.89662571 1.094431917
"_id" "nasi-soto-gulai-kambing__-6.90_107.61", 107.6090455 -6.900715755 1.096845722
"_id" "putra-bengawan-nasi-goreng-%26-soto__-6.89_107.62", 107.617187 -6.891298 1.097063729
"_id" "nasi-soto-ayam__-6.90_107.61", 107.6088095 -6.901163102 1.096953691
"_id" "nasi-uduk-%26amp%3B-soto-kikil-cak-khohar__-6.89_107.61", 107.6144958 -6.894765295 1.097201996
"_id" "nasi-soto-ayam__-6.90_107.62", 107.6204395 -6.900453028 1.105389179
"_id" "nasi-goreng-soto-ayam__-6.90_107.62", 107.6206326 -6.900303912 1.105442127
"_id" "nasi-soto-ayam-madura__-6.91_107.62", 107.6169634 -6.909279174 1.10839237
"_id" "nasi-soto_pasar-baru_-6.92_107.61", 107.605226 -6.923423387 1.108717834
"_id" "nasi-soto-ayam-madura_pasar-baru_-6.92_107.61", 107.6053333 -6.92348729 1.108840349
"_id" "nasi-soto-sarinah__-6.90_107.63", 107.631175 -6.897282 1.111636423
"_id" "nasi-uduk-soto__-6.89_107.64", 107.6361465 -6.893636262 1.113187903
"_id" "nasi-soto-ayam-madura__-6.91_107.63", 107.629376 -6.910677 1.118767984
"_id" "nasi-soto-ayam__-6.91_107.63", 107.629376 -6.910677 1.118767984
"_id" "nasi-uduk-soto__-6.94_107.62", 107.6248813 -6.93937772 1.133952371
"_id" "nasi-soto-ayam-khas-madura__-6.94_107.63", 107.625749 -6.940099 1.135077548
"_id" "nasi-uduk-%26amp%3B-soto__-6.96_107.64", 107.638936 -6.955310213 1.154967917
"_id" "nasi-soto-ayam_ruko-metro-trade-center_-6.94_107.66", 107.6600504 -6.940208438 1.161142923
"_id" "nasi-soto-ayam-khas-madura__-6.94_107.69", 107.6948118 -6.937141164 1.185994885
"_id" "nasi-soto-ayam-khas-madura-cak-nonk__-6.93_107.72", 107.7156472 -6.932668019 1.199509086
"_id" "nasi-gandul__-7.43_109.24", 109.2391822 -7.425856643 2.756884824
Look again the term in the query
"$maxDistance" : 0.053980478460939611
Go figure.
By the way $maxDistance is DEFINETLY used properly. Reducing it does lead to less results. The issue seems to come from the fact that whatever formula mongodb use to compute distance is inaccurate.
I answered the same post you made on stackexchange:
https://dba.stackexchange.com/questions/23869/nearsphere-returns-too-many-data-what-am-i-missing-am-i-wrong-is-it-a-bug-d
Does that help?
Thanks,
andrew