bottomCalc sum value not in alignment with header - tabulator

Issue description
I have field Fat with a bottomCalc:
{ title: "Fat", field: "Fat", bottomCalc: "sum" },
Note that the sum value doesn't align with the header, Fat:
Question
Is there something I can do to resolve this? Or is it possibly a bug?
Runnable snippet
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Index - NutritionTrackerRazorPages</title>
<link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" href="/">NutritionTracker</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" href="/FoodCategories">Food Categories</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" href="/SimpleFoods">Simple Foods</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" href="/ComplexFoods">Complex Foods</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" href="/ComplexFoodComponents/IndexGrouped">Complex Food Components</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" href="/FoodRecords">Food Records</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" href="/FoodRecords/IndexGrouped">Food Records Grouped</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" href="/FoodRecords/IndexTabulator">Food Records Tabulator</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
<link href="https://unpkg.com/tabulator-tables#4.9.3/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables#4.9.3/dist/js/tabulator.min.js"></script>
<h1>Index</h1>
<p>
Create New
</p>
<div id="data-table"></div>
<script>
//var table_data = [
// { id: 1, name: "Oli Bob", age: "12", col: "red", dob: "" },
// { id: 2, name: "Mary May", age: "1", col: "blue", dob: "14/05/1982" },
// { id: 3, name: "Christine Lobowski", age: "42", col: "green", dob: "22/05/1982" },
// { id: 4, name: "Brendon Philips", age: "125", col: "orange", dob: "01/08/1980" },
// { id: 5, name: "Margret Marmajuke", age: "16", col: "yellow", dob: "31/01/1999" }
//];
var table_data = [
{
id: 3,
date: "1/1/2021",
time: "12:00 PM",
food: "Avocado (g)",
amount: "50",
Calories : "84",
Fat : "7.70",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "4.30",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "1.00",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
{
id: 4,
date: "1/1/2021",
time: "12:00 PM",
food: "Basmati Rice (g)",
amount: "300",
Calories : "1067",
Fat : "3.33",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "240",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "20",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
{
id: 5,
date: "1/1/2021",
time: "12:00 PM",
food: "Ground Beef 80/20 (g)",
amount: "200",
Calories : "508",
Fat : "32",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "0",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "51",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
{
id: 1,
date: "1/1/2021",
time: "6:00 PM",
food: "Onion (g)",
amount: "150",
Calories : "60",
Fat : "0.15",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "14",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "1.65",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
{
id: 2,
date: "1/1/2021",
time: "6:00 PM",
food: "Lentils (g)",
amount: "250",
Calories : "714",
Fat : "3.57",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "164",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "57",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
{
id: 6,
date: "1/1/2021",
time: "6:00 PM",
food: "Basmati Rice (g)",
amount: "350",
Calories : "1244",
Fat : "3.89",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "280",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "23",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
{
id: 14,
date: "1/3/2021",
time: "12:00 PM",
food: "Salmon (g)",
amount: "150",
Calories : "230",
Fat : "11",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "0",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "33",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
{
id: 17,
date: "1/3/2021",
time: "12:00 PM",
food: "LimaYamOnion",
amount: "300",
Calories : "13",
Fat : "0.03",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "3.07",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "0.36",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
{
id: 18,
date: "1/3/2021",
time: "6:00 PM",
food: "LimaYamOnion",
amount: "500",
Calories : "22",
Fat : "0.06",
MonounsaturatedFat: "0",
PolyunsaturatedFat: "0",
Omega3 : "0",
Omega6 : "0",
SaturatedFat : "0",
TransFat : "0",
Cholesterol : "0",
Carbohydrates : "5.12",
Fiber : "0",
SolubleFiber : "0",
InsolubleFiber : "0",
Starch : "0",
Sugars : "0",
AddedSugars : "0",
Protein : "0.61",
VitaminB1 : "0",
VitaminB2 : "0",
VitaminB3 : "0",
VitaminB5 : "0",
VitaminB6 : "0",
VitaminB12 : "0",
Folate : "0",
VitaminA : "0",
VitaminC : "0",
VitaminD : "0",
VitaminE : "0",
VitaminK : "0",
Calcium : "0",
Copper : "0",
Iron : "0",
Magnesium : "0",
Manganese : "0",
Phosphorus : "0",
Potassium : "0",
Selenium : "0",
Sodium : "0",
Zinc : "0",
},
];
var table = new Tabulator("#data-table", {
height: 500,
//height: "auto",
//maxHeight:"100%",
data: table_data,
//layout: "fitColumns",
//headerHozAlign:"center",
groupBy: ["date", "time"],
//responsiveLayout: "hide",
//columnCalcs: "group",
//columnCalcs: "both",
groupHeader: function (value, count, data, group) {
return value;
},
groupToggleElement: "header",
columns: [
//{ title: "Date", field: "date" },
//{ title: "Time", field: "time" },
//{ title: "Food", field: "food" },
{ title: "Food", field: "food", frozen:true },
{ title: "Amt", field: "amount" },
//{ title: "Cals", field: "calories" },
{ title: "Cals", field: "Calories" },
{
title: "Lipids",
columns: [
{ title: "Fat", field: "Fat", bottomCalc: "sum" },
//{ title: "Fat", field: "Fat" },
{ title: "Mono", field: "MonounsaturatedFat" },
{ title: "Poly", field: "PolyunsaturatedFat" },
{ title: "Om3", field: "Omega3" },
{ title: "Om6", field: "Omega6" },
{ title: "Sat", field: "SaturatedFat" },
{ title: "Trans", field: "TransFat" },
{ title: "Chol", field: "Cholesterol" },
]
},
{
title: "Carbohydrates",
columns: [
{ title: "Carbs", field: "Carbohydrates" },
{ title: "Fiber", field: "Fiber" },
{ title: "Sol", field: "SolubleFiber" },
{ title: "Ins", field: "InsolubleFiber" },
{ title: "Starch", field: "Starch" },
{ title: "Sug", field: "Sugars" },
{ title: "Add", field: "AddedSugars" },
]
},
{ title: "Pro", field: "Protein" },
{
title: "Vitamins",
columns: [
{ title: "B1", field: "VitaminB1" },
{ title: "B2", field: "VitaminB2" },
{ title: "B3", field: "VitaminB3" },
{ title: "B5", field: "VitaminB5" },
{ title: "B6", field: "VitaminB6" },
{ title: "B12", field: "VitaminB12" },
{ title: "Fol", field: "Folate" },
{ title: "A", field: "VitaminA" },
{ title: "C", field: "VitaminC" },
{ title: "D", field: "VitaminD" },
{ title: "E", field: "VitaminE" },
{ title: "K", field: "VitaminK" },
]
},
{
title: "Minerals",
columns: [
{ title: "Calc", field: "Calcium" },
{ title: "Cop", field: "Copper" },
{ title: "Iron", field: "Iron" },
{ title: "Mag", field: "Magnesium" },
{ title: "Mang", field: "Manganese" },
{ title: "Phos", field: "Phosphorus" },
{ title: "Pot", field: "Potassium" },
{ title: "Sel", field: "Selenium" },
{ title: "Sod", field: "Sodium" },
{ title: "Zin", field: "Zinc" }
]
}
],
//rowClick: function (e, row) {
// alert("Row " + row.getData().id + " Clicked!!!!");
//},
});
</script>
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2021 - NutritionTrackerRazorPages - Privacy
</div>
</footer>
<script src="/lib/jquery/dist/jquery.min.js"></script>
<script src="/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="/js/site.js?v=4q1jwFhaPaZgr8WAUSrux6hAuh0XDg9kPS3xIVq36I0"></script>
<script src="/_framework/aspnetcore-browser-refresh.js"></script></body>
</html>

Not able to repoduce your problem in jsfiddle If you're able to modify to reproduce, it would help. or its a browser version issue ? What browser/OS combo are you using (I tested on Chrome/Rasbian and Firefox/Windows8)
<div></div>

Related

DocuSign API Set Signer Language Not Working for First Signer

In my tests so far when creating and sending an envelope using the DocuSign API v2.1 it is not respecting the setting for the supportedLanguage for the first signer, but does for the 2nd signer.
Here's the JSON for Signers array that I'm using:
"signers" :
[
{
"email" : "test#acme.com",
"emailNotification" :
{
"emailBody" : "",
"emailSubject" : "Please Sign the Agreement",
"supportedLanguage" : "de"
},
"name" : "Elisabeth Kiefer",
"recipientId" : "1",
"routingOrder" : "1",
"tabs" :
{
"dateSignedTabs" :
[
{
"anchorIgnoreIfNotPresent" : false,
"anchorString" : "/DateSigned1/",
"anchorUnits" : "pixels",
"anchorXOffset" : "0",
"anchorYOffset" : "0",
"documentId" : "1",
"locked" : "false",
"name" : "Date Signed",
"pageNumber" : "1",
"tabLabel" : "Data Signed",
"value" : ""
}
],
"fullNameTabs" :
[
{
"anchorIgnoreIfNotPresent" : false,
"anchorString" : "/SigName1/",
"anchorUnits" : "pixels",
"anchorXOffset" : "0",
"anchorYOffset" : "0",
"documentId" : "1",
"locked" : "false",
"name" : "Name",
"pageNumber" : "1",
"tabLabel" : "Name",
"value" : ""
}
],
"signHereTabs" :
[
{
"anchorIgnoreIfNotPresent" : false,
"anchorString" : "/Signature1/",
"anchorUnits" : "pixels",
"anchorXOffset" : "0",
"anchorYOffset" : "0",
"documentId" : "1",
"locked" : "false",
"name" : "Sign Here",
"pageNumber" : "1",
"tabLabel" : "Signature",
"value" : ""
}
]
}
},
{
"email" : "test2#random.com",
"emailNotification" :
{
"emailBody" : "",
"emailSubject" : "Please Sign the Agreement",
"supportedLanguage" : "es"
},
"name" : "Fabio Gallego",
"recipientId" : "2",
"routingOrder" : "1",
"tabs" :
{
"dateSignedTabs" :
[
{
"anchorIgnoreIfNotPresent" : false,
"anchorString" : "/DateSigned2/",
"anchorUnits" : "pixels",
"anchorXOffset" : "0",
"anchorYOffset" : "0",
"documentId" : "1",
"locked" : "false",
"name" : "Date Signed",
"pageNumber" : "1",
"tabLabel" : "Data Signed",
"value" : ""
}
],
"fullNameTabs" :
[
{
"anchorIgnoreIfNotPresent" : false,
"anchorString" : "/SigName2/",
"anchorUnits" : "pixels",
"anchorXOffset" : "0",
"anchorYOffset" : "0",
"documentId" : "1",
"locked" : "false",
"name" : "Name",
"pageNumber" : "1",
"tabLabel" : "Name",
"value" : ""
}
],
"signHereTabs" :
[
{
"anchorIgnoreIfNotPresent" : false,
"anchorString" : "/Signature2/",
"anchorUnits" : "pixels",
"anchorXOffset" : "0",
"anchorYOffset" : "0",
"documentId" : "1",
"locked" : "false",
"name" : "Sign Here",
"pageNumber" : "1",
"tabLabel" : "Signature",
"value" : ""
}
]
}
}
]
You can see that the first signer's supportedLanguage is set to "de" and the second signer is set to "es". When I receive the emails for these and view the document only the second signer has the language set for the website and email. The first signer has everything in English and not German as expected.
The language setting is superseded by the user's selected lang that comes from the account.
So, you may want to change the email address to both to some random/unused emails that you know for sure are not part of any account user and check again.
There's no way for you to force the languages of the enduser, they can always overwrite your selection.

"How to get 'parent to child' relation with mongodb aggregation"

I have one collection called "location". in this collection all child and parent collection are stores. now I want to create a query who returns me parent to child spaces separated string.
Collection
businessId: { type: mongoose.Schema.Types.ObjectId, ref: 'admin' },
parentId: { type: mongoose.Schema.Types.ObjectId, ref: 'location' },
name: { type: String },
image: { type: String },
imageManipulation: { type: String },
locationColor: [{ range: { type: String }, color: { type: String } }],
area: {},
settings: {},
status: { type: String, enum: [0, 1], default: 1 },
isChild: { type: String, enum: [0, 1] },
parentPosition: { type: String }
In the above collection, you can see parentId field. if the location is a child then it have parentId. if the location is a parent then parentId will null. parent location can N level child location.
Collection Data
[{
"_id" : ObjectId("5cee1002a01ad50f5c222982"),
"status" : "1",
"name" : "Ground Floor",
"settings" : {
"zoom" : "0",
"positionX" : "0",
"positionY" : "0",
"width" : "498",
"height" : "498"
},
"image" : "1559105538977.jpg",
"businessId" : ObjectId("5cbd61dc3b56b902284ea388"),
"locationColor" : [],
"updatedAt" : ISODate("2019-05-29T04:52:18.999Z"),
"createdAt" : ISODate("2019-05-29T04:52:18.999Z"),
"__v" : 0
},
{
"_id" : ObjectId("5cee103ca01ad50f5c222983"),
"status" : "1",
"name" : "Kitchen",
"settings" : {
"zoom" : "0",
"positionX" : "0",
"positionY" : "0",
"width" : "498",
"height" : "498"
},
"area" : "{\"type\":3,\"points\":[{\"x\":20,\"y\":178},{\"x\":19,\"y\":75},{\"x\":56,\"y\":71},{\"x\":57,\"y\":52},{\"x\":80,\"y\":18},{\"x\":138,\"y\":17},{\"x\":165,\"y\":52},{\"x\":165,\"y\":94},{\"x\":174,\"y\":96},{\"x\":173,\"y\":179}],\"fill\":\"rgba(178,40,40,0.58)\"}",
"parentId" : ObjectId("5cee1002a01ad50f5c222982"),
"image" : "1559105596975.jpg",
"businessId" : ObjectId("5cbd61dc3b56b902284ea388"),
"locationColor" : [],
"updatedAt" : ISODate("2019-05-29T04:53:16.990Z"),
"createdAt" : ISODate("2019-05-29T04:53:16.990Z"),
"__v" : 0
},
{
"_id" : ObjectId("5cee1078a01ad50f5c222984"),
"status" : "1",
"name" : "Cbot",
"settings" : {
"zoom" : "0",
"positionX" : "0",
"positionY" : "0",
"width" : "498",
"height" : "498"
},
"area" : "{\"type\":3,\"points\":[{\"x\":20,\"y\":311},{\"x\":17,\"y\":59},{\"x\":84,\"y\":58},{\"x\":88,\"y\":312}],\"fill\":\"rgba(20,205,123,0.67)\"}",
"parentId" : ObjectId("5cee103ca01ad50f5c222983"),
"image" : "1559105656049.jpg",
"businessId" : ObjectId("5cbd61dc3b56b902284ea388"),
"locationColor" : [],
"updatedAt" : ISODate("2019-05-29T04:54:16.070Z"),
"createdAt" : ISODate("2019-05-29T04:54:16.070Z"),
"__v" : 0
},
{
"_id" : ObjectId("5cee10c1a01ad50f5c222985"),
"status" : "1",
"name" : "Drower 1",
"settings" : {
"zoom" : "5",
"positionX" : "470",
"positionY" : "70",
"width" : "498",
"height" : "498"
},
"area" : "{\"type\":3,\"points\":[{\"x\":21,\"y\":102},{\"x\":81,\"y\":104},{\"x\":79,\"y\":43},{\"x\":21,\"y\":43}],\"fill\":\"rgba(16,77,193,0.5)\"}",
"parentId" : ObjectId("5cee1078a01ad50f5c222984"),
"image" : "1559105729881.jpg",
"businessId" : ObjectId("5cbd61dc3b56b902284ea388"),
"locationColor" : [],
"updatedAt" : ISODate("2019-05-29T04:55:29.901Z"),
"createdAt" : ISODate("2019-05-29T04:55:29.901Z"),
"__v" : 0
},
{
"_id" : ObjectId("5cee110ea01ad50f5c222986"),
"status" : "1",
"name" : "Drawer 2",
"settings" : {
"zoom" : "5",
"positionX" : "484",
"positionY" : "103",
"width" : "498",
"height" : "498"
},
"area" : "{\"type\":1,\"coordinates\":{\"x\":23,\"y\":125,\"width\":58,\"height\":56},\"points\":[{\"x\":23,\"y\":125},{\"x\":81,\"y\":181}],\"fill\":\"rgba(117,37,109,0.74)\"}",
"parentId" : ObjectId("5cee1078a01ad50f5c222984"),
"image" : "1559105806551.jpg",
"businessId" : ObjectId("5cbd61dc3b56b902284ea388"),
"locationColor" : [],
"updatedAt" : ISODate("2019-05-29T04:56:46.574Z"),
"createdAt" : ISODate("2019-05-29T04:56:46.574Z"),
"__v" : 0
},
{
"_id" : ObjectId("5cee1148a01ad50f5c222987"),
"status" : "1",
"name" : "Drawer 3",
"settings" : {
"zoom" : "5",
"positionX" : "477",
"positionY" : "94",
"width" : "498",
"height" : "498"
},
"area" : "{\"type\":3,\"points\":[{\"x\":22,\"y\":205},{\"x\":20,\"y\":290},{\"x\":84,\"y\":288},{\"x\":85,\"y\":205}],\"fill\":\"rgba(164,108,54,0.57)\"}",
"parentId" : ObjectId("5cee1078a01ad50f5c222984"),
"image" : "1559105864947.jpg",
"businessId" : ObjectId("5cbd61dc3b56b902284ea388"),
"locationColor" : [],
"updatedAt" : ISODate("2019-05-29T04:57:44.972Z"),
"createdAt" : ISODate("2019-05-29T04:57:44.972Z"),
"__v" : 0
},
{
"_id" : ObjectId("5cee5e683b9f67a9f501f818"),
"status" : "1",
"name" : "Washroom",
"settings" : {
"zoom" : "5",
"positionX" : "477",
"positionY" : "94",
"width" : "498",
"height" : "498"
},
"area" : "{\"type\":3,\"points\":[{\"x\":22,\"y\":205},{\"x\":20,\"y\":290},{\"x\":84,\"y\":288},{\"x\":85,\"y\":205}],\"fill\":\"rgba(164,108,54,0.57)\"}",
"parentId" : ObjectId("5cee1002a01ad50f5c222982"),
"image" : "1559105864947.jpg",
"businessId" : ObjectId("5cbd61dc3b56b902284ea388"),
"locationColor" : [],
"updatedAt" : ISODate("2019-05-29T04:57:44.972Z"),
"createdAt" : ISODate("2019-05-29T04:57:44.972Z"),
"__v" : 0
},
{
"_id" : ObjectId("5cee5f593b9f67a9f501fa01"),
"status" : "1",
"name" : "Third Floor",
"settings" : {
"zoom" : "0",
"positionX" : "0",
"positionY" : "0",
"width" : "498",
"height" : "498"
},
"image" : "1559105538977123.jpg",
"businessId" : ObjectId("5cbd61dc3b56b902284ea388"),
"locationColor" : [],
"updatedAt" : ISODate("2019-05-29T04:52:18.999Z"),
"createdAt" : ISODate("2019-05-29T04:52:18.999Z"),
"__v" : 0
}]
Expected result in JSON
[
{
"_id": "5cee1002a01ad50f5c222982",
"name": "Ground Floor"
},
{
"_id": "5cee103ca01ad50f5c222983",
"name": " Kitchen"
},
{
"_id": "5cee1078a01ad50f5c222984",
"name": " Cbot"
},
{
"_id": "5cee110ea01ad50f5c222986",
"name": " Drawer 2"
},
{
"_id": "5cee1148a01ad50f5c222987",
"name": " Drawer 3"
},
{
"_id": "5cee10c1a01ad50f5c222985",
"name": " Drower 1"
},
{
"_id": "5cee5e683b9f67a9f501f818",
"name": " Washroom"
},
{
"_id": "5cee5f593b9f67a9f501fa01",
"name": "Third Floor"
}
]
I do not think you should let mongodb take care of name formatting. So my solution is about finding how many spaces a certain name needs before, so that js can deal with formatting. This is the query:
db.collection.aggregate([
{
$graphLookup: {
from: "collection",
startWith: "$parentId",
connectFromField: "parentId",
connectToField: "_id",
as: "hierarchy"
}
},
{
$project: {
"_id": 1,
"name": 1,
"hierarchy_size": { $size: "$hierarchy" }
}
}
]);
With the $graphLookup, the db is building an in memory graph of edges between connectFromField and connectToField. From the graph you only need the depth of your hierarchy, so I computed hierarchy_size. This is the output:
/* 1 */
{
"_id" : ObjectId("5cee1002a01ad50f5c222982"),
"name" : "Ground Floor",
"hierarchy_size" : 0
}
/* 2 */
{
"_id" : ObjectId("5cee103ca01ad50f5c222983"),
"name" : "Kitchen",
"hierarchy_size" : 1
}
/* 3 */
{
"_id" : ObjectId("5cee1078a01ad50f5c222984"),
"name" : "Cbot",
"hierarchy_size" : 2
}
/* 4 */
{
"_id" : ObjectId("5cee10c1a01ad50f5c222985"),
"name" : "Drower 1",
"hierarchy_size" : 3
}
/* 5 */
{
"_id" : ObjectId("5cee110ea01ad50f5c222986"),
"name" : "Drawer 2",
"hierarchy_size" : 3
}
/* 6 */
{
"_id" : ObjectId("5cee1148a01ad50f5c222987"),
"name" : "Drawer 3",
"hierarchy_size" : 3
}
/* 7 */
{
"_id" : ObjectId("5cee5e683b9f67a9f501f818"),
"name" : "Washroom",
"hierarchy_size" : 1
}
/* 8 */
{
"_id" : ObjectId("5cee5f593b9f67a9f501fa01"),
"name" : "Third Floor",
"hierarchy_size" : 0
}
The only problem here might be query performances, but that depends on how much data you need to process. Also consider the memory limit.

Mongodb Count on multiple sub arrays

Calculate All the number of agreements, contacts and the group by it on company name. Note. Agreements and Contacts are array of objects
This is a sample document of my data:
{
"_id" : ObjectId("57c7bd8f51666ac615bc837d"),
"ITBCompanyId" : 2296,
"updatedAt" : ISODate("2016-09-01T05:38:02.843Z"),
"createdAt" : ISODate("2016-09-01T05:33:03.604Z"),
"identifier" : "SOU10",
"name" : "Source One Rehabilitation",
"addressLine2" : null,
"city" : "Plano",
"state" : "TX",
"zip" : "75024",
"faxNumber" : "",
"territoryId" : "11",
"marketId" : null,
"accountNumber" : "Source One Rehabilitation",
"dateAcquired" : ISODate("2014-09-15T04:00:00.000Z"),
"parentCompany" : null,
"annualRevenue" : null,
"numberOfEmployees" : null,
"ownershipType" : null,
"deletedBy" : null,
"orgId" : "1",
"deletedAt" : null,
"_info" : {
"lastUpdated" : ISODate("2016-09-01T05:12:32.000Z"),
"updatedBy" : "LabTechInt"
},
"whois" : [],
"configuration" : [],
"contact" : [
{
"mobileGuid" : "6435d4c0-12fa-41d1-9db0-e4f9b1b4e7b2",
"presence" : null,
"gender" : null,
"title" : null,
"country" : null,
"zip" : "75024",
"state" : "TX",
"city" : "Plano",
"addressLine2" : null,
"lastName" : null,
"firstName" : "Dee Williams",
"id" : 821,
"_id" : ObjectId("57c7beba51666ac615bc8576"),
"communicationItems" : [],
"customFields" : null,
"relationship" : {
"name" : null,
"id" : 0
}
},
{
"id" : 829,
"firstName" : "Jesy",
"lastName" : "Leon",
"addressLine2" : null,
"city" : "Plano",
"state" : "TX",
"zip" : "75024",
"country" : null,
"title" : null,
"gender" : null,
"presence" : null,
"mobileGuid" : "2b0754c1-c256-4063-9cea-c40f9ca77084",
"_id" : ObjectId("57c7beba51666ac615bc857d"),
"communicationItems" : [
{
"communicationType" : "Email",
"defaultFlag" : true,
"extension" : null,
"type" : {
"_info" : null,
"name" : "Email",
"id" : 1
},
"id" : 45587
},
{
"communicationType" : "Phone",
"defaultFlag" : true,
"extension" : null,
"value" : "9728012190",
"type" : {
"_info" : null,
"name" : "Direct",
"id" : 2
},
"id" : 45590
}
],
"customFields" : null,
"company" : {
"id" : "19390",
"name" : "Source One Rehabilitation",
},
"relationship" : {
"id" : 0,
"name" : null
}
},
{
"id" : 850,
"firstName" : "Dr.",
"lastName" : "Gabriel",
"addressLine2" : null,
"city" : "Plano",
"state" : "TX",
"zip" : "75024",
"country" : null,
"title" : null,
"gender" : null,
"presence" : null,
"mobileGuid" : "8c833a31-369d-4499-87cb-b5598826e653",
"_id" : ObjectId("57c7beba51666ac615bc8592"),
"communicationItems" : [
{
"communicationType" : "Email",
"defaultFlag" : true,
"extension" : null,
"type" : {
"_info" : null,
"name" : "Email",
"id" : 1
},
"id" : 45627
},
{
"communicationType" : "Phone",
"defaultFlag" : true,
"extension" : null,
"value" : "9728012190",
"type" : {
"_info" : null,
"name" : "Direct",
"id" : 2
},
"id" : 45628
}
],
"customFields" : null,
"relationship" : {
"id" : 0,
"name" : null
}
}
],
"agreement" : [
{
"periodType" : null,
"billAmount" : "0",
"billTermsId" : 12,
"billOneTimeFlag" : false,
"billCycleId" : "2",
"expiredDays" : "0",
"coverAgreementExpense" : false,
"coverAgreementProduct" : false,
"coverAgreementTime" : true,
"oneTimeFlag" : false,
"applicationUnlimitedFlag" : true,
"applicationCycle" : "CalendarMonth",
"applicationLimit" : "0",
"applicationUnits" : "Hours",
"internalNotes" : "",
"workOrder" : "",
"slaId" : "2",
"reasonCancelled" : "",
"dateCancelled" : null,
"cancelledFlag" : false,
"noEndingDateFlag" : false,
"endDate" : "2016-01-31T05:00:00Z",
"startDate" : "2015-02-01T05:00:00Z",
"businessUnitId" : 2,
"locationId" : "11",
"customerPO" : "",
"parentAgreementId" : null,
"name" : "Complete Managed Services Agreement",
"id" : "109",
"_id" : ObjectId("57c7bd9051666ac615bc83e3"),
"_info" : {
"updatedBy" : "Ali ",
"lastUpdated" : ISODate("2016-05-12T20:55:21.000Z")
},
"workType" : {
"_info" : null,
"name" : "Remote",
"id" : "3"
},
"workRole" : {
"_info" : null,
"name" : "Support Engineer",
"id" : "5"
},
"contact" : {
"name" : "Heather B",
"id" : "539"
},
"agreementType" : {
"name" : "Complete Managed Services",
"id" : "19"
}
},
{
"id" : "121",
"name" : "Complete Managed Services: IMV",
"parentAgreementId" : null,
"customerPO" : "",
"locationId" : "11",
"businessUnitId" : 2,
"startDate" : "2016-03-01T05:00:00Z",
"endDate" : null,
"noEndingDateFlag" : true,
"cancelledFlag" : false,
"dateCancelled" : null,
"reasonCancelled" : "",
"slaId" : "2",
"workOrder" : "",
"internalNotes" : "",
"applicationUnits" : "Hours",
"applicationLimit" : "0",
"applicationCycle" : "CalendarMonth",
"applicationUnlimitedFlag" : true,
"oneTimeFlag" : false,
"coverAgreementTime" : true,
"coverAgreementProduct" : false,
"coverAgreementExpense" : false,
"expiredDays" : "0",
"billCycleId" : "2",
"billOneTimeFlag" : false,
"billTermsId" : 12,
"billAmount" : "0",
"periodType" : null,
"_id" : ObjectId("57c7bd9051666ac615bc83fa"),
"_info" : {
"lastUpdated" : ISODate("2016-05-12T22:11:59.000Z"),
"updatedBy" : "Ali "
},
"workType" : {
"id" : "3",
"name" : "Remote",
"_info" : null
},
"workRole" : {
"id" : "5",
"name" : "Support Engineer",
"_info" : null
},
"contact" : {
"id" : "810",
"name" : "Jarrod Rogers"
},
"agreementType" : {
"id" : "19",
"name" : "Complete Managed Services"
}
},
{
"id" : "122",
"name" : "Complete Managed Servicess: FTW",
"parentAgreementId" : null,
"customerPO" : "",
"locationId" : "11",
"businessUnitId" : 2,
"startDate" : "2016-03-01T05:00:00Z",
"endDate" : null,
"noEndingDateFlag" : true,
"cancelledFlag" : false,
"dateCancelled" : null,
"reasonCancelled" : "",
"slaId" : "2",
"workOrder" : "",
"internalNotes" : "",
"applicationUnits" : "Hours",
"applicationLimit" : "0",
"applicationCycle" : "CalendarMonth",
"applicationUnlimitedFlag" : true,
"oneTimeFlag" : false,
"coverAgreementTime" : true,
"coverAgreementProduct" : false,
"coverAgreementExpense" : false,
"expiredDays" : "0",
"billCycleId" : "2",
"billOneTimeFlag" : false,
"billTermsId" : 12,
"billAmount" : "0",
"periodType" : null,
"_id" : ObjectId("57c7bd9051666ac615bc83fd"),
"_info" : {
"lastUpdated" : ISODate("2016-05-12T22:14:29.000Z"),
"updatedBy" : "Ali "
},
"workType" : {
"id" : "3",
"name" : "Remote",
"_info" : null
},
"workRole" : {
"id" : "5",
"name" : "Support Engineer",
"_info" : null
},
"contact" : {
"id" : "810",
"name" : "Jarrod Rogers"
},
"agreementType" : {
"id" : "19",
"name" : "Complete Managed Services"
}
},
{
"id" : "123",
"name" : "Complete Managed Servicess: SC",
"parentAgreementId" : null,
"customerPO" : "",
"locationId" : "11",
"businessUnitId" : 2,
"startDate" : "2016-03-01T05:00:00Z",
"endDate" : null,
"noEndingDateFlag" : true,
"cancelledFlag" : false,
"dateCancelled" : null,
"reasonCancelled" : "",
"slaId" : "2",
"workOrder" : "",
"internalNotes" : "",
"applicationUnits" : "Hours",
"applicationLimit" : "0",
"applicationCycle" : "CalendarMonth",
"applicationUnlimitedFlag" : true,
"oneTimeFlag" : false,
"coverAgreementTime" : true,
"coverAgreementProduct" : false,
"coverAgreementExpense" : false,
"expiredDays" : "0",
"billCycleId" : "2",
"billOneTimeFlag" : false,
"billTermsId" : 12,
"billAmount" : "0",
"periodType" : null,
"_id" : ObjectId("57c7bd9051666ac615bc83ff"),
"_info" : {
"lastUpdated" : ISODate("2016-05-12T22:13:59.000Z"),
"updatedBy" : "Ali "
},
"workType" : {
"id" : "3",
"name" : "Remote",
"_info" : null
},
"workRole" : {
"id" : "5",
"name" : "Support Engineer",
"_info" : null
},
"contact" : {
"id" : "810",
"name" : "Jarrod Rogers"
},
"agreementType" : {
"id" : "19",
"name" : "Complete Managed Services"
}
},
{
"id" : "124",
"name" : "Complete Managed Servicess: Psych",
"parentAgreementId" : null,
"customerPO" : "",
"locationId" : "11",
"businessUnitId" : 2,
"startDate" : "2016-03-01T05:00:00Z",
"endDate" : "2016-05-31T04:00:00Z",
"noEndingDateFlag" : false,
"cancelledFlag" : false,
"dateCancelled" : null,
"reasonCancelled" : "",
"slaId" : "2",
"workOrder" : "",
"internalNotes" : "",
"applicationUnits" : "Hours",
"applicationLimit" : "0",
"applicationCycle" : "CalendarMonth",
"applicationUnlimitedFlag" : true,
"oneTimeFlag" : false,
"coverAgreementTime" : true,
"coverAgreementProduct" : false,
"coverAgreementExpense" : false,
"expiredDays" : "0",
"billCycleId" : "2",
"billOneTimeFlag" : false,
"billTermsId" : 12,
"billAmount" : "0",
"periodType" : null,
"_id" : ObjectId("57c7bd9051666ac615bc8402"),
"_info" : {
"lastUpdated" : ISODate("2016-06-01T16:48:03.000Z"),
"updatedBy" : "Ali "
},
"workType" : {
"id" : "3",
"name" : "Remote",
"_info" : null
},
"workRole" : {
"id" : "5",
"name" : "Support Engineer",
"_info" : null
},
"contact" : {
"id" : "810",
"name" : "Jarrod Rogers"
},
"agreementType" : {
"id" : "19",
"name" : "Complete Managed Services"
}
},
{
"id" : "125",
"name" : "Managed Services Agreement",
"parentAgreementId" : null,
"customerPO" : "",
"locationId" : "11",
"businessUnitId" : 2,
"startDate" : "2016-06-01T04:00:00Z",
"endDate" : null,
"noEndingDateFlag" : true,
"cancelledFlag" : false,
"dateCancelled" : null,
"reasonCancelled" : "",
"slaId" : null,
"workOrder" : "",
"internalNotes" : "",
"applicationUnits" : null,
"applicationLimit" : "0",
"applicationCycle" : null,
"applicationUnlimitedFlag" : false,
"oneTimeFlag" : false,
"coverAgreementTime" : false,
"coverAgreementProduct" : false,
"coverAgreementExpense" : false,
"expiredDays" : "0",
"billCycleId" : "2",
"billOneTimeFlag" : false,
"billTermsId" : 12,
"billAmount" : "0",
"periodType" : null,
"_id" : ObjectId("57c7bd9051666ac615bc8404"),
"_info" : {
"lastUpdated" : ISODate("2016-06-01T16:17:45.000Z"),
"updatedBy" : "Ali "
},
"contact" : {
"id" : "810",
"name" : "Jarrod Rogers"
},
"agreementType" : {
"id" : "32",
"name" : "Agreement Template"
}
}
],
"companySite" : [
{
"id" : 1125,
"name" : "Main",
"addressLine1" : "5425 W. Spring Creek Pkwy #270",
"addressLine2" : null,
"city" : "Plano",
"state" : "TX",
"zip" : "75024",
"faxNumber" : null,
"taxCodeId" : null,
"expenseReimbursement" : null,
"primaryAddressFlag" : true,
"defaultShippingFlag" : false,
"defaultBillingFlag" : true,
"defaultMailingFlag" : false,
"_id" : ObjectId("57c7beb951666ac615bc84e5"),
"country" : {
"id" : 1,
"name" : "United States",
"_info" : null
},
"_info" : {
"lastUpdated" : ISODate("2016-09-01T05:12:28.000Z"),
"updatedBy" : "LabTechInt"
}
}
],
"status" : {
"id" : "1",
"name" : "Active"
},
"country" : {
"id" : 1,
"name" : "United States",
"_info" : null
},
"id" : "19390",
"__v" : 0
}
I want to count both the number of agreements and the number of contacts in a document, and group the data on company name and orgId.
This is my desired output:
{
"Company": "Multi-Metal Manufacturing",
"Organization": "1",
"AgreementCount" : 1,
"ContactCount" : 4
}
I tried this solution, but result is not as expected; it gives me a general count of all fields, but I want a specific count:
return Company.aggregate(
{"$unwind":"$agreement"},
{"$unwind":"$contact"},
{"$unwind":"$companySite"},
{ $group: {
_id: {
"Organization": "$orgId",
"Company": "$name"
},
"count": { $sum: 1 } } },
{"$project": {
"_id": 0,
"Company": "$_id.Company",
"Organization":"$_id.Organization",
"Count": "$count"
}
})
Assuming Companyname/orgId is unique, you can use $size which will get the size of your arrays to make this pretty simple:
[
{
$project:{
_id:false,
Company: "$name",
Organization: "$orgId",
AgreementCount: {
$size: {$ifNull: ["$agreement",[]]}
},
ContactCount: {
$size: {$ifNull: ["$contact",[]]}
}
}
}
]
If Companyname/orgId are not unique, then you can introduce a $group stage to sum the counts produced by $size:
[
{
$project:{
_id:false,
Company: "$name",
Organization: "$orgId",
AgreementCount: {
$size: {$ifNull: ["$agreement",[]]}
},
ContactCount: {
$size: {$ifNull: ["$contact",[]]}
}
}
},
{
$group: {
_id:{
Company: "$Company",
Organization: "$Organization"
},
AgreementCount: {$sum:"$AgreementCount"},
ContactCount: {$sum:"$ContactCount"}
}
},
{
$project:{
_id: false,
Company: "$_id.Company",
Organization: "$_id.Organization",
AgreementCount: true,
ContactCount: true
}
}
]
db.company.aggregate(
{$unwind:"$contact"},
{$unwind:"$agreement"},
{$group:
{_id: {"company":"$name","organization":"$orgId"},
"Contact":{$addToSet:"$contact._id"},
"Agreement":{$addToSet:"$agreement._id"}
}
},
{"$project":
{_id:0,
"Company":"$_id.company",
"Organization":"$_id.organization",
"AgreementCount":{$size:"$Agreement"},
"ContactCount":{$size:"$Contact"}
}
}).pretty();
return Company.aggregate([{
$project: {
ITBCompanyId: 1,
name: 1,
contact: {
$size: "$contact"
},
agreement: {
$size: "$agreement"
},
whoIs: {
$size: "$whois"
},
companySite: {
$size: "$companySite"
}
}
}])

Mongodb Count multiple subobjects and group by

Hello This query is running fine for me I want to add other arrays as well like contact and I want separate count for them. If I add contact it add the result in count P.S : I used array name with $sum it start showing me zero count
return Company.aggregate(
{"$unwind":"$agreement"},
{"$unwind":"$contact"},
{"$unwind":"$companySite"},
{ $group: { _id: {
"Organization": "$orgId",
"Company": "$name"
}, "count": { $sum: 1 } } },{
"$project": {
"_id": 0,
"Company": "$_id.Company",
"Organization":"$_id.Organization",
"Count": "$count"
}
})
expected output
"Company": "Multi-Metal Manufacturing",
"Organization": "1",
"AgreementCount": 1,
"ContactCount" : 4
"ABC Count": 5
Sample Document is :
{
"_id" : ObjectId("57c6a97a90c933a2d54117dc"),
"ITBCompanyId" : 1034,
"updatedAt" : ISODate("2016-08-31T12:47:03.679Z"),
"createdAt" : ISODate("2016-08-31T09:55:06.217Z"),
"identifier" : "INL10",
"name" : "Inline Data Systems",
"addressLine1" : "7 Park Place",
"addressLine2" : "Ste D",
"city" : "Swansea",
"orgId" : "1",
"deletedAt" : null,
"_info" : {
"lastUpdated" : ISODate("2016-02-10T21:22:15.000Z"),
"updatedBy" : "Clarissa"
},
"whois" : [],
"configuration" : [],
"contact" : [
{
"email" : "justin#inlinedatasystems.com",
"mobileGuid" : "d8852942-8f5a-406f-a646-a5f8697f7885",
"presence" : null,
"gender" : null,
"title" : null,
"country" : null,
"zip" : null,
"state" : null,
"city" : null,
"addressLine2" : null,
"addressLine1" : null,
"lastName" : "Wilkerson",
"firstName" : "Justin",
"id" : 191,
"_id" : ObjectId("57c6abdfb966968130b1671c"),
"communicationItems" : [],
"customFields" : null,
"company" : {
"name" : "Inline Data Systems",
"id" : "19331"
},
"relationship" : {
"name" : null,
"id" : 0
}
}
],
"agreement" : [
{
"periodType" : null,
"billAmount" : "0",
"billTermsId" : 12,
"billOneTimeFlag" : false,
"billCycleId" : "2",
"expiredDays" : "0"
},
{
"id" : "40",
"name" : "Managed Hosted Server Agreement",
"billAmount" : "0",
"periodType" : null,
"_id" : ObjectId("57c6ab96b966968130b16616"),
"_info" : {
"lastUpdated" : ISODate("2014-10-03T14:25:42.000Z"),
"updatedBy" : "ali "
},
"contact" : {
"id" : "191",
"name" : "Justin Wilkerson"
},
"agreementType" : {
"id" : "33",
"name" : "HDCloud - Hosted Server"
}
},
{
"id" : "41",
"name" : "Managed Backup Agreement",
"parentAgreementId" : "42",
"customerPO" : ""
}
],
"companySite" : [
{
"id" : 1037,
"name" : "Main",
"city" : "Swansea",
"state" : "IL",
"zip" : "62226",
}
]

MongoDB can't get all the rows

I've got a project using the MEAN stack and inside the Mongo DB collection I've got a collection called 'services' with the following rows (a small subset):
{ "_id" : ObjectId("55af611de2be6d817d000001"), "client" : "55a8e5fa586f94752a000002", "collaborators" : null, "date" : ISODate("2015-07-22T09:23:41.917Z"), "latestUpdateDate" : ISODate("2015-07-22T09:23:41.917Z"), "backofficeRequested" : false, "description" : "Uma descrição qq", "wasSeen" : 0, "currentStatusId" : "0", "lastStatusId" : 4, "__v" : 2, "addresses" : [ ], "dates" : [ ], "estimateDuration" : 61, "estimatePrice" : "55", "notes" : [ ], "serviceDate" : ISODate("2015-07-23T10:07:00Z"), "totalPrice" : "600", "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" }
{ "_id" : ObjectId("55afa8662c1e98c80d2fd2ff"), "client" : "55a8e5fa586f94752a000002", "estimatePrice" : "12", "estimateDuration" : NumberLong(720), "description" : "12", "serviceDate" : ISODate("1212-12-12T12:48:45Z"), "lastStatusId" : null, "currentStatusId" : "0", "collaborators" : [ { "id" : "55acb6022c1e98d05f2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" }
{ "_id" : ObjectId("55afa9f62c1e98370e2fd2ff"), "client" : "55ae68d82c1e98ac782fd303", "estimatePrice" : "12", "estimateDuration" : NumberLong(720), "description" : "12", "serviceDate" : ISODate("1212-12-12T12:48:45Z"), "lastStatusId" : null, "currentStatusId" : "1", "collaborators" : [ { "id" : "55acb6022c1e98d05f2fd2ff" } ], "dates" : [ ], "totalPrice" : "500", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" }
{ "_id" : ObjectId("55afaa7e2c1e98bc0d2fd2ff"), "client" : "55a8e5fa586f94752a000002", "estimatePrice" : "12", "estimateDuration" : NumberLong(720), "description" : "12", "serviceDate" : ISODate("1212-12-12T12:48:45Z"), "lastStatusId" : null, "currentStatusId" : "0", "collaborators" : [ { "id" : "55acb6022c1e98d05f2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" }
{ "_id" : ObjectId("55afaf582c1e98d80d2fd303"), "client" : "55ae68d82c1e98ac782fd303", "estimatePrice" : "1", "estimateDuration" : NumberLong(60), "description" : "1", "serviceDate" : ISODate("1111-11-11T11:47:45Z"), "lastStatusId" : null, "currentStatusId" : "4", "collaborators" : [ { "id" : "55ae40772c1e98007a2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" }
{ "_id" : ObjectId("55afb8472c1e98370e2fd300"), "client" : "55a8e5fa586f94752a000002", "estimatePrice" : "1", "estimateDuration" : NumberLong(60), "description" : "1", "serviceDate" : ISODate("1111-11-11T11:47:45Z"), "lastStatusId" : null, "currentStatusId" : NumberLong(2), "collaborators" : [ { "id" : "55ae40772c1e98007a2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" }
{ "_id" : ObjectId("55aff2dc2c1e98ac112fd2ff"), "client" : "55a8e5fa586f94752a000002", "estimatePrice" : "1", "estimateDuration" : NumberLong(720), "description" : "1", "serviceDate" : ISODate("1989-12-12T12:12:00Z"), "lastStatusId" : null, "currentStatusId" : NumberLong(2), "collaborators" : [ { "id" : "55ae40772c1e98007a2fd2ff" } ], "dates" : [ ], "totalPrice" : "", "backofficeRequested" : NumberLong(0), "serviceInvoiced" : "No", "evaluation" : "", "positiveFeatures" : "", "negativeFeatures" : "" }
I want to get all the rows belonging to the client with the ID "55a8e5fa586f94752a000002".
It's pretty easy to do in the mongo Shell:
db.services.find({"client":"55a8e5fa586f94752a000002"})
But I am having a hard time inside Node.js because it either returns no services at all or it ends up returning just one service.
Here's what I'm using right now:
(...)
console.log(checkDatas[0]._id);
Service.find().where('client', checkDatas[0]._id)
.exec(function(err, services)
{
console.log("output: " + services);
(...)
And finally, here's the output:
**55a8e5fa586f94752a000002**
**output**: { _id: 55aff719af19b58913000001,
client: 55a8e5fa586f94752a000002,
__v: 1,
collaborators: [ { id: '55acb6022c1e98d05f2fd2ff' } ],
notes: [],
dates: [],
addresses: [],
date: Wed Jul 22 2015 22:03:37 GMT+0200 (CEST),
latestUpdateDate: Wed Jul 22 2015 22:03:37 GMT+0200 (CEST),
backofficeRequested: false,
totalPrice: null,
estimatePrice: null,
estimateDuration: null,
description: '',
serviceDate: null,
wasSeen: 0,
currentStatusId: 3,
lastStatusId: 1 }
It's as if I were using findOne but I'm not.
On top of that, yesterday I had a .sort() method also in that query and things worked fine after commenting it but today the problem's back.
Do MongoDB / Node have some sort of caching system I'm not aware of?
Thanks
EDIT:
Running
Service.find({'client': checkDatas[0]._id})
yields the same result
It looks like you're mixing ObjectId's and strings, which are two different things.
Your Mongoose query is looking for an ObjectId:
Service.find().where('client', checkDatas[0]._id)
Quick tip: you can distinguish both types in the output:
// ObjectId, because it doesn't have quotes around it
client: 55a8e5fa586f94752a000002
// String
collaborators: [ { id: '55acb6022c1e98d05f2fd2ff' } ]
Your MongoDB shell query is looking for a string:
db.services.find({"client":"55a8e5fa586f94752a000002"})
The subset you're posting shows only strings, but I think that your database may actually contain both types. You can check and see if this works better:
Service.find().or([
{ client : checkDatas[0]._id },
{ client : String(checkDatas[0]._id) }
])...
Obviously this isn't ideal, you should consider normalizing your database if both types are mixed.
EDIT: the or query probably won't work when using Mongoose, since it will cast both clauses to the type defined in the schema. You can still check from the Mongo shell:
db.services.find({ $or : [
{ client : "55a8e5fa586f94752a000002" },
{ client : ObjectId("55a8e5fa586f94752a000002") }
]})

Resources