Filtering realtime database data - javascript

I have the following firebase database. I would like to filter and list the orders which's deliveryDuration value is "45"
"orders" : [ null, {
"comment" : "",
"date" : "2018-07-09 10:07:18",
"deliveryDuration" : "45",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Dessert",
"details" : "(Hausgemacht)",
"name" : "Warmer Topfenstrudel",
"price" : 3.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}, {
"comment" : "",
"date" : "2018-07-10 10:07:33",
"deliveryDuration" : "30",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Dessert",
"details" : "(Hausgemacht)",
"name" : "Warmer Topfenstrudel",
"price" : 3.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}, {
"comment" : "",
"date" : "2018-07-10 10:13:13",
"deliveryDuration" : "10",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Spezialitäten",
"details" : "Schweinschnitzel mit Parmesan gebacken auf Spaghetti Napoli",
"name" : "Piccata Milanese",
"price" : 12.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}
}
I tried writing the following function:
getNewItems: function (order) {
if (db.ref('orders').child(order).child('deliveryDuration').equalTo(45)){
return db.ref('orders').child(order).child('deliveryDuration')
}}
How could I get the filtered objects? I was thinking that that i write an if-else statement and the function returns the values what i was looking for.

I think you're looking for this:
getNewItems: function (order) {
return db.ref('orders').orderByChild('deliveryDuration').equalTo(45));
}}
This Firebase query takes each child node of the location you query, orders it by the deliveryDuration property, and then filters to only get the ones who are equal to 45.

Related

How to populate an array inside a document which stores uniqueID as strings and that uniqueID is from another collection rather than objectID

I have a user document as follows which holds an array of systemIDs', which is unique for another collection which holds systemID as unikey key. How to populate user with all system ID details?
User document:
{
"_id" : ObjectId("6gfg85993266db5fdgs578"),
"email" : "xyz#gmail.com",
"role" : "user",
"systemIDs" : [
"12345678",
"87654321"
],
"createdAt" : ISODate("2022-02-13T16:31:34.119+0000"),
"updatedAt" : ISODate("2022-02-13T16:31:34.119+0000"),
"__v" : NumberInt(0)
},
{
"_id" : ObjectId("6gfg85993266db5fdgs578"),
"email" : "abc#gmail.com",
"role" : "user",
"systemIDs" : [
"1111111",
"2135684"
],
"createdAt" : ISODate("2022-02-13T16:31:34.119+0000"),
"updatedAt" : ISODate("2022-02-13T16:31:34.119+0000"),
"__v" : NumberInt(0)
}
System IDs document:
{
"_id" : ObjectId("62093fdsfsdfs97e1"),
"systemID" : "12345678",
"err" : [
1, 5, 10
],
"__v" : NumberInt(0)
},
{
"_id" : ObjectId("62093fdsfsdfs97e1"),
"systemID" : "87654321",
"err" : [
3, 7
],
"__v" : NumberInt(0)
},
{
"_id" : ObjectId("62087dsfsdfs97e1"),
"systemID" : "11111111",
"err" : [
],
"__v" : NumberInt(0)
},
I want to find details of all the systemIDs a user holds which results something like this if I query my users collection with email : xyz#gmail.com, I should get the below result or populated result like shown:
{
"_id" : ObjectId("6gfg85993266db5fdgs578"),
"email" : "xyz#gmail.com",
"role" : "user",
"systemIDs" : [
{
"_id" : ObjectId("62093fdsfsdfs97e1"),
"systemID" : "12345678",
"err" : [
1, 5, 10
],
"__v" : NumberInt(0)
},
{
"_id" : ObjectId("62093fdsfsdfs97e1"),
"systemID" : "87654321",
"err" : [
3, 7
],
"__v" : NumberInt(0)
},
]
"createdAt" : ISODate("2022-02-13T16:31:34.119+0000"),
"updatedAt" : ISODate("2022-02-13T16:31:34.119+0000"),
"__v" : NumberInt(0)
},
I can create a foreach loop and call database each time but I suppose that wouldn't be a good practice.
I am new with this so please bear with me and explain it to me in details.
you can use aggregation with $match and $lookup to perform this task
db.users.aggregate([
{
"$match": {
"email": "xyz#gmail.com"
}
},
{
"$lookup": {
"from": "systems",
"localField": "systemIDs",
"foreignField": "systemID",
"as": "systemIDs"
}
}
])
demo

MongoDB aggregation match nested array

I´m striving to get some aggregated data from Mongo DB. I have the following collections:
I´m striving to get some aggregated data from Mongo DB. I have the following collections:
I have 2 collections:
product
{
"_id" : ObjectId("5eda578cb9c3fa1ef8c483c5"),
"brand_name" : "LAVA",
"short_description" : "Specifications",
"offer_price" : 20,
"quantity" : 5,
"variants" : [
"5eb9300438d0b83a3088feec",
"5eb92a7909823240081cd763"
],
"seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
"name" : "LAVA X10",
"createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
"updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
},
{
"_id" : ObjectId("5eda578cb9c3fa1ef8c483c8"),
"brand_name" : "LAVA",
"short_description" : "Specifications",
"offer_price" : 20,
"quantity" : 5,
"variants" : [
"5eb9300438d0b83a3088feed",
"5eb92a7909823240081cd763"
],
"seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
"name" : "LAVA X11",
"createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
"updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
}
2. attribute
{
"_id" : ObjectId("5eb92a4d09823240081cd75f"),
"is_active" : true,
"name" : "Color",
"slug" : [
{
"is_active" : true,
"_id" : ObjectId("5eb92a7909823240081cd763"),
"name" : "Green",
"updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
"createdAt" : ISODate("2020-05-11T10:35:37.357Z")
},
{
"is_active" : true,
"_id" : ObjectId("5eb92a7909823240081cd764"),
"name" : "RED",
"updatedAt" : ISODate("2020-06-01T13:47:39.236Z"),
"createdAt" : ISODate("2020-05-11T10:35:37.357Z")
}
],
"createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
"updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
"__v" : 0
},
{
"_id" : ObjectId("5eb92a5409823240081cd760"),
"is_active" : true,
"name" : "Size",
"slug" : [
{
"is_active" : true,
"_id" : ObjectId("5eb92c4338d0b83a3088feeb"),
"name" : "M",
"updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
"createdAt" : ISODate("2020-05-11T10:43:15.355Z")
},
{
"is_active" : true,
"_id" : ObjectId("5eb9300438d0b83a3088feec"),
"name" : "S",
"updatedAt" : ISODate("2020-05-11T10:59:16.731Z"),
"createdAt" : ISODate("2020-05-11T10:59:16.731Z")
},
{
"is_active" : true,
"_id" : ObjectId("5eb9300438d0b83a3088feed"),
"name" : "XXL",
"updatedAt" : ISODate("2020-06-01T13:46:36.417Z"),
"createdAt" : ISODate("2020-05-11T10:59:16.731Z")
}
],
"createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
"updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
"__v" : 0
}
I want to this type of result
{
"_id" : ObjectId("5eda578cb9c3fa1ef8c483c5"),
"brand_name" : "LAVA",
"short_description" : "Specifications",
"offer_price" : 20,
"quantity" : 5,
"variants" : [
"5eb9300438d0b83a3088feec",
"5eb92a7909823240081cd763"
],
"attributes" : [
{
"_id" : ObjectId("5eb92a5409823240081cd760"),
"is_active" : true,
"name" : "Size",
"slug" : {
"is_active" : true,
"_id" : ObjectId("5eb9300438d0b83a3088feec"),
"name" : "S",
"updatedAt" : ISODate("2020-05-11T10:59:16.731Z"),
"createdAt" : ISODate("2020-05-11T10:59:16.731Z")
},
"createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
"updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
"__v" : 0
},
{
"_id" : ObjectId("5eb92a4d09823240081cd75f"),
"is_active" : true,
"name" : "Color",
"slug" : {
"is_active" : true,
"_id" : ObjectId("5eb92a7909823240081cd763"),
"name" : "Green",
"updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
"createdAt" : ISODate("2020-05-11T10:35:37.357Z")
},
"createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
"updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
"__v" : 0
}
]
"seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
"name" : "LAVA X10",
"createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
"updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
},
{
"_id" : ObjectId("5eda578cb9c3fa1ef8c483c8"),
"brand_name" : "LAVA",
"short_description" : "Specifications",
"offer_price" : 20,
"quantity" : 5,
"variants" : [
"5eb9300438d0b83a3088feed",
"5eb92a7909823240081cd763"
],
"attributes" : [
{
"_id" : ObjectId("5eb92a5409823240081cd760"),
"is_active" : true,
"name" : "Size",
"slug" : {
"is_active" : true,
"_id" : ObjectId("5eb9300438d0b83a3088feed"),
"name" : "XXL",
"updatedAt" : ISODate("2020-06-01T13:46:36.417Z"),
"createdAt" : ISODate("2020-05-11T10:59:16.731Z")
},
"createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
"updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
"__v" : 0
},
{
"_id" : ObjectId("5eb92a4d09823240081cd75f"),
"is_active" : true,
"name" : "Color",
"slug" : {
"is_active" : true,
"_id" : ObjectId("5eb92a7909823240081cd763"),
"name" : "Green",
"updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
"createdAt" : ISODate("2020-05-11T10:35:37.357Z")
},
"createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
"updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
"__v" : 0
}
]
"seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
"name" : "LAVA X11",
"createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
"updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
}
// ------------ Here variants array values are attribute's slug ID -----------------
Please Help Me
Please let me know how?
That's not very effective data design: one of your collections uses ids of parts of documents in other collection. First of all you need to consider having slug._id: 1 index for attributes. Then to fetch products with included attributes which have filtered slugs use something like
db.product.find(...).map(function(prod){
prod.attributes = [];
for(sid in prod.variants){
var attr = db.attributes.find({'slug._id': sid});
attr.slug = attr.slug.find(function(slug){return slug._id == sid});
prod.atributes.push(attr);
}
return prod;
})

How to get part of Object from Array of Objects using typescript

I have an array of Objects and I want to filter and ftech only part of object which I will be sending as a body parameter to an API.
Below is my code,however it returns me an array and inside that I have my object.I don't want it to be an array but just an Object.
How can I achieve this.
Original Array of Objects:
[ {
"flowId" : 11,
"flowName" : "jobtest003",
"version" : 1,
"ingestionFlowId" : "",
"jobCreatedDate" : "25-05-2020",
"jobUpdateDate" : "28-06-2020",
"jobLastRunDate" : "29-06-2020",
"active" : false,
"properties" : [ {
"id" : 12,
"key" : "sourceTable",
"value" : "job002",
"category" : "General Settings"
}, {
"id" : 13,
"key" : "Source",
"value" : "api",
"category" : "Source Properties"
}, {
"id" : 147,
"key" : "Target Path",
"value" : "/raw/au/jackson",
"category" : "Destination Properties"
} ]
}, {
"flowId" : 21,
"flowName" : "jobtest004",
"version" : 1,
"ingestionFlowId" : null,
"jobCreatedDate" : "25-05-2020",
"jobUpdateDate" : "28-06-2020",
"jobLastRunDate" : "29-06-2020",
"active" : false,
"properties" : [ {
"id" : 21,
"key" : "sourceTable",
"value" : "job003",
"category" : "General Settings"
}, {
"id" : 22,
"key" : "Source",
"value" : "api",
"category" : "Source Properties"
}, {
"id" : 23,
"key" : "Client ID",
"value" : "ebf73456-443e-4986-941b-057906d25e2f",
"category" : "Destination Properties"
}, {
"id" : 147,
"key" : "Target Path",
"value" : "/raw/au/jackson",
"category" : "Destination Properties"
} ]
}, {
"flowId" : 22,
"flowName" : "jobtest004",
"version" : 1,
"ingestionFlowId" : null,
"jobCreatedDate" : "25-05-2020",
"jobUpdateDate" : "28-06-2020",
"jobLastRunDate" : "29-06-2020",
"active" : false,
"properties" : [ {
"id" : 21,
"key" : "sourceTable",
"value" : "job003",
"category" : "General Settings"
}, {
"id" : 22,
"key" : "Source",
"value" : "api",
"category" : "Source Properties"
}, {
"id" : 23,
"key" : "Client ID",
"value" : "ebf73456-443e-4986-941b-057906d25e2f",
"category" : "Destination Properties"
}, {
"id" : 147,
"key" : "Target Path",
"value" : "/raw/au/jackson",
"category" : "Destination Properties"
} ]
} ]
The result I want :
{
"flowId" : 20,
"flowName" : "jobtest004",
"version" : 1,
"ingestionFlowId" : null,
"jobCreatedDate" : "25-05-2020",
"jobUpdateDate" : "28-06-2020",
"jobLastRunDate" : "29-06-2020",
"active" : false,
"properties" : [ {
"id" : 21,
"key" : "sourceTable",
"value" : "job003",
"category" : "General Settings"
}, {
"id" : 22,
"key" : "Source",
"value" : "api",
"category" : "Source Properties"
}, {
"id" : 23,
"key" : "Client ID",
"value" : "ebf73456-443e-4986-941b-057906d25e2f",
"category" : "Destination Properties"
}, {
"id" : 147,
"key" : "Target Path",
"value" : "/raw/au/jackson",
"category" : "Destination Properties"
} ]
}
The result I am getting:
[{
"flowId" : 20,
"flowName" : "jobtest004",
"version" : 1,
"ingestionFlowId" : null,
"jobCreatedDate" : "25-05-2020",
"jobUpdateDate" : "28-06-2020",
"jobLastRunDate" : "29-06-2020",
"active" : false,
"properties" : [ {
"id" : 21,
"key" : "sourceTable",
"value" : "job003",
"category" : "General Settings"
}, {
"id" : 22,
"key" : "Source",
"value" : "api",
"category" : "Source Properties"
}, {
"id" : 23,
"key" : "Client ID",
"value" : "ebf73456-443e-4986-941b-057906d25e2f",
"category" : "Destination Properties"
}, {
"id" : 147,
"key" : "Target Path",
"value" : "/raw/au/jackson",
"category" : "Destination Properties"
} ]
}]
I don't want my result to be an array instead I just want it to be an Object.
Use find method of array as follows.
var array = your data;
var object_needed = array.find(d=>d.flowId == 21)//use required id in place of 21 or use required property in place of .flowIf
//console.log(object_needed)
Hope this helps!!!
For that you can use find method on the Array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
With your explanation if I have understood right
Array.prototype.filter will always return an array only, you have to use array index to get the object out of array
Use reduce method to convert array into object.
const convertArrayToObject = (array, key) => {
const initialValue = {};
return array.reduce((obj, item) => {
return {
...obj,
[item[key]]: item,
};
}, initialValue);
};
let testData = [ { "flowId" : 11, "flowName" : "jobtest003", "version" : 1, "ingestionFlowId" : "", "jobCreatedDate" : "25-05-2020", "jobUpdateDate" : "28-06-2020", "jobLastRunDate" : "29-06-2020", "active" : false, "properties" : [ { "id" : 12, "key" : "sourceTable", "value" : "job002", "category" : "General Settings" }, { "id" : 13, "key" : "Source", "value" : "api", "category" : "Source Properties" }, { "id" : 147, "key" : "Target Path", "value" : "/raw/au/jackson", "category" : "Destination Properties" } ] }, { "flowId" : 21, "flowName" : "jobtest004", "version" : 1, "ingestionFlowId" : null, "jobCreatedDate" : "25-05-2020", "jobUpdateDate" : "28-06-2020", "jobLastRunDate" : "29-06-2020", "active" : false, "properties" : [ { "id" : 21, "key" : "sourceTable", "value" : "job003", "category" : "General Settings" }, { "id" : 22, "key" : "Source", "value" : "api", "category" : "Source Properties" }, { "id" : 23, "key" : "Client ID", "value" : "ebf73456-443e-4986-941b-057906d25e2f", "category" : "Destination Properties" }, { "id" : 147, "key" : "Target Path", "value" : "/raw/au/jackson", "category" : "Destination Properties" } ] }, { "flowId" : 22, "flowName" : "jobtest004", "version" : 1, "ingestionFlowId" : null, "jobCreatedDate" : "25-05-2020", "jobUpdateDate" : "28-06-2020", "jobLastRunDate" : "29-06-2020", "active" : false, "properties" : [ { "id" : 21, "key" : "sourceTable", "value" : "job003", "category" : "General Settings" }, { "id" : 22, "key" : "Source", "value" : "api", "category" : "Source Properties" }, { "id" : 23, "key" : "Client ID", "value" : "ebf73456-443e-4986-941b-057906d25e2f", "category" : "Destination Properties" }, { "id" : 147, "key" : "Target Path", "value" : "/raw/au/jackson", "category" : "Destination Properties" } ] } ];
console.log(
convertArrayToObject(testData,'flowId')
);
click here for detailed explanation
You can use find method as below which will return only object
arrayOfData.find(function(obj){
return obj.Id == 11
})
or
arrayOfData.find(obj => obj.Id == 11)

Project Specific Fields from a Foreign Collection $lookup result

I'm writing aggregation to get foreign collection data with local collection.
db.getCollection('orders').aggregate([
{
$match: {
status: "UNASSIGNED",
serviceLocationId: "83177"
}
}, {
$lookup: {
from: "servicelocations",
localField: "serviceLocationId",
foreignField: "serviceLocationId",
as: "locations"
}
}, {
$unwind: "$locations"
}])
I'm getting:
{
"_id" : ObjectId("59d32b5c360198e441b67545"),
"accountId" : 1.0,
"orderId" : "AQ137O1701240",
"serviceLocationId" : "83177",
"orderDate" : "2017-09-18T18:29:00.000Z",
"description" : "AQ137O1701240",
"serviceType" : "Delivery",
"orderSource" : "Import",
"takenBy" : "KARIM",
"plannedDeliveryDate" : ISODate("2017-10-09T00:00:00.000Z"),
"plannedDeliveryTime" : "",
"actualDeliveryDate" : "",
"actualDeliveryTime" : "",
"deliveredBy" : "",
"size1" : 25.0,
"size2" : 464.0,
"size3" : 46.0,
"jobPriority" : 1.0,
"cancelReason" : "",
"cancelDate" : "",
"cancelBy" : "",
"reasonCode" : "",
"reasonText" : "",
"status" : "UNASSIGNED",
"lineItems" : [
{
"ItemId" : "MMGW001",
"size1" : 25.0,
"size2" : 464.38,
"size3" : 46.875
}
],
"locations" : {
"_id" : ObjectId("59ce18e172dbf6926093e189"),
"accountId" : 1.0,
"serviceLocationId" : "83177",
"regionId" : "1",
"zoneId" : "DXBZONE1",
"description" : "EXPRESS BLUE MART SUPERMARKET",
"locationPriority" : 1.0,
"accountTypeId" : 1.0,
"locationType" : "SERVICELOCATION",
"location" : {
"makani" : "",
"lng" : 55.179042,
"lat" : 25.098741
},
"deliveryDays" : "MTWRFSU",
"serviceTimeTypeId" : "1",
"timeWindow" : {
"timeWindowTypeId" : "1"
},
"address1" : "",
"address2" : "",
"phone" : "",
"city" : "",
"county" : "",
"state" : "",
"country" : "",
"zipcode" : "",
"imageUrl" : "",
"contact" : {
"name" : "",
"email" : ""
},
"status" : "ACTIVE",
"createdBy" : "",
"updatedBy" : "",
"updateDate" : ""
}
}
but i need:
{
"_id" : ObjectId("59d32b5c360198e441b67545"),
"accountId" : 1.0,
"orderId" : "AQ137O1701240",
"serviceLocationId" : "83177",
"orderDate" : "2017-09-18T18:29:00.000Z",
"description" : "AQ137O1701240",
"serviceType" : "Delivery",
"orderSource" : "Import",
"takenBy" : "KARIM",
"plannedDeliveryDate" : ISODate("2017-10-09T00:00:00.000Z"),
"plannedDeliveryTime" : "",
"actualDeliveryDate" : "",
"actualDeliveryTime" : "",
"deliveredBy" : "",
"size1" : 25.0,
"size2" : 464.0,
"size3" : 46.0,
"jobPriority" : 1.0,
"cancelReason" : "",
"cancelDate" : "",
"cancelBy" : "",
"reasonCode" : "",
"reasonText" : "",
"status" : "UNASSIGNED",
"lineItems" : [
{
"ItemId" : "MMGW001",
"size1" : 25.0,
"size2" : 464.38,
"size3" : 46.875
}
],
"locations" : {
"lng" : 55.179042,
"lat" : 25.098741
}
}
MongoDB less than 3.4.4
Basically, use $project as a final stage and select ALL the specific fields you want. Unfortunately $addFields is out because it will actually "merge" the sub-keys with the existing ones. So the seemingly simple:
{ "$addFields": {
"locations": {
"lng": "$locations.location.lng",
"lat": "$locations.location.lat"
}
}}
Just gives you all the existing content under "locations" as well as the those newly defined keys. Unless of course you don't $unwind directly after the $lookup, which you can do if this would not cause the BSON limit to be exceeded. ( this is called $lookup + $unwind coalescence )
Then we can use $addFields with $map, because we can simply "re-map" the array:
{ "$addFields": {
"locations": {
"$map": {
"input": "$locations",
"as": "l",
"in": {
"lng": "$$l.location.lng",
"lat": "$$l.location.lat"
}
}
}
}},
{ "$unwind": "$locations" }
And then $unwind if you still need to after that re-mapping.
So with $project it is:
{ "$project": {
"accountId" : 1,
"orderId" : 1,
"serviceLocationId" : 1,
"orderDate" : 1,
"description" : 1,
"serviceType" : 1,
"orderSource" : 1,
"takenBy" : 1,
"plannedDeliveryDate" : 1,
"plannedDeliveryTime" : 1,
"actualDeliveryDate" : 1,
"actualDeliveryTime" : 1,
"deliveredBy" : 1,
"size1" : 1,
"size2" : 1,
"size3" : 1,
"jobPriority" : 1,
"cancelReason" : 1,
"cancelDate" : 1,
"cancelBy" : 1,
"reasonCode" : 1,
"reasonText" : 1,
"status" : 1,
"lineItems" : 1,
"locations" : {
"lng": "$locations.location.lng",
"lat": "$locations.location.lat"
}
}}
Simple but long winded.
MongoDB 3.4.4 Or greater
If you have MongoDB 3.4.4 or greater with $objectToArray and $arrayToObject, then you can be a bit more fancy about it:
{ "$replaceRoot": {
"newRoot": {
"$arrayToObject": {
"$concatArrays": [
{ "$filter": {
"input": { "$objectToArray": "$$ROOT" },
"cond": { "$ne": [ "$$this.k", "locations" ] }
}},
{ "$objectToArray": {
"locations": {
"lng": "$locations.location.lng",
"lat": "$locations.location.lat"
}
}}
]
}
}
}}
Which basically takes all the fields presently in the whole document from $$ROOT, turns it into an array format. We then $filter the "location" field by the "key name" and $concatArrays it with the new "location" key and sub-keys again transformed into an array.
Finally of course $arrayToObject takes that and transforms back into an object which is supplied to newRoot of $replaceRoot as the final output.
So using either of those except $addFields after $unwind of course gives you the correct result:
/* 1 */
{
"_id" : ObjectId("59d32b5c360198e441b67545"),
"accountId" : 1.0,
"orderId" : "AQ137O1701240",
"serviceLocationId" : "83177",
"orderDate" : "2017-09-18T18:29:00.000Z",
"description" : "AQ137O1701240",
"serviceType" : "Delivery",
"orderSource" : "Import",
"takenBy" : "KARIM",
"plannedDeliveryDate" : ISODate("2017-10-09T00:00:00.000Z"),
"plannedDeliveryTime" : "",
"actualDeliveryDate" : "",
"actualDeliveryTime" : "",
"deliveredBy" : "",
"size1" : 25.0,
"size2" : 464.0,
"size3" : 46.0,
"jobPriority" : 1.0,
"cancelReason" : "",
"cancelDate" : "",
"cancelBy" : "",
"reasonCode" : "",
"reasonText" : "",
"status" : "UNASSIGNED",
"lineItems" : [
{
"ItemId" : "MMGW001",
"size1" : 25.0,
"size2" : 464.38,
"size3" : 46.875
}
],
"locations" : {
"lng" : 55.179042,
"lat" : 25.098741
}
}
MongoDB 3.6 and greater
As a bit of a preview, $lookup gets a more expressive overhaul with MongoDB 3.6. So you can actually specifically state the fields to return that way:
{ "$lookup": {
"from": "servicelocations",
"let": { "serviceLocationId": "$serviceLocationId" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "serviceLocationId", "$$serviceLocationId" ] } }},
{ "$project": {
"_id": 0,
"lng": "$location.lng",
"lat": "$location.lat"
}}
],
"as": "locations"
}}
Little bit more handy when that is actually released. This actually uses $expr instead of the localField and foreignField to define the "join" condition in a $match stage of the sub-pipeline. Then you can simply $project the fields to return, which then go into the array targeted by $lookup.
Going forward, that's the general approach you would want to take as it limits what actually gets returned.

RedQueryBuilder validations

I am using the RedQueryBuilder version 0.5.0
http://0-5-0.redquerybuilder.appspot.com/ for building an Expression validator.
I want to Know, how to validate the entered expressions?
The metadata of the RedQuery Builder 0.5.0 supports css and class additions for Jquery support.
Please, suggest how to add the css metadata to the existing metadata.
The Sample MetaData is given below:
meta : {
tables : [ {
"name" : "PERSON",
"label" : "Person",
"columns" : [ {
"name" : "NAME",
"label" : "Name",
"type" : "STRING",
"size" : 10
}, {
"name" : "DOB",
"label" : "Date of birth",
"type" : "DATE"
}, {
"name" : "SEX",
"label" : "Sex",
"type" : "STRING",
"editor" : "SELECT"
}, {
"name" : "CATEGORY",
"label" : "Category",
"type" : "REF",
} ],
fks : []
} ],
types : [ {
"name" : "STRING",
"editor" : "TEXT",
"operators" : [ {
"name" : "=",
"label" : "is",
"cardinality" : "ONE"
}, {
"name" : "<>",
"label" : "is not",
"cardinality" : "ONE"
}, {
"name" : "LIKE",
"label" : "like",
"cardinality" : "ONE"
}, {
"name" : "<",
"label" : "less than",
"cardinality" : "ONE"
}, {
"name" : ">",
"label" : "greater than",
"cardinality" : "ONE"
} ]
}, {
"name" : "DATE",
"editor" : "DATE",
"operators" : [ {
"name" : "=",
"label" : "is",
"cardinality" : "ONE"
}, {
"name" : "<>",
"label" : "is not",
"cardinality" : "ONE"
}, {
"name" : "<",
"label" : "before",
"cardinality" : "ONE"
}, {
"name" : ">",
"label" : "after",
"cardinality" : "ONE"
} ]
}, {
"name" : "REF",
"editor" : "SELECT",
"operators" : [ {
"name" : "IN",
"label" : "any of",
"cardinality" : "MULTI"
}]
} ]
},

Categories

Resources