I have been asked in an interview to mutate the array of objects in such a way that the data appears in the following manner
[
{
"companyName": "ABC",
"members": [
{
"id": 13121212,
"firstName": "Ray",
"lastName": "Fernandis",
"points": 1800,
"position": 1
},
{
"id": 13131313,
"firstName": "Carrie",
"lastName": "Yoda",
"points": 1200,
"position": 2
}
]
}]
and the sample data was given below.
[
{
"communityName": "ABC",
"lastUpdateTimestamp": {
"date": {
"year": 2020,
"month": 10,
"day": 7
},
"time": {
"hour": 18,
"minute": 6,
"second": 5,
"nano": 536529000
}
},
"data": {
"listChannelsData": [
{
"channelId": 1234,
"channelName": "BCD",
"members": [
{
"id": 13121212,
"firstName": "Ray",
"lastName": "Fernandis",
"points": 1800,
"position": 1
}
]
}
]
}
},
{
"communityName": "DEF",
"lastUpdateTimestamp": {
"date": {
"year": 2020,
"month": 10,
"day": 7
},
"time": {
"hour": 18,
"minute": 6,
"second": 21,
"nano": 47894000
}
},
"data": {
"listChannelsData": [
{
"channelId": 3421,
"channelName": "GHI",
"members": [
{
"id": 13121212,
"firstName": "Nicholas",
"lastName": "Xin",
"points": 800,
"position": 2
},
{
"id": 13131313,
"firstName": "Carrie",
"lastName": "Yoda",
"points": 1000,
"position": 1
}
]
}
]
}
}
]
The agenda for me was to print the derived json from sample json and I could only figure out this much code which was a courtesy of stackoverflow
function dictionary(data) {
var map = {};
data.forEach(item => {
if (!Array.isArray(map[item.companyName])) {
map[item.companyName] = [item.data.listChannelsData];
} else {
map[item.communityName].push(item.data.listChannelsData);
}
});
return map;
}
console.log(dictionary(data));
But now when I try to pick up the data for the member using another foreach loop, I'm not able to access the data for members. Can anyone help me with the part where I can successfully access the member array and print the company name along side it
Try this code
var newArray = []
data.map((item, index) => {
newArray[index] = {companyName : item.communityName}
newArray[index].members = item.data.listChannelsData[0].members
})
data is the given data.
Related
I want to update flag in array of objects which are 3 levels deep nested.
I have this array of ages:
[20, 25]
I want to set flag: 1 for those users which has type: students and ages: 20 or 25
Here is my Users collection:
{
"_id": "wef324DGSshf",
"userTypes": [
{
"type": "students",
"users": [
{
"name": "John",
"age": 20,
"flag": 0
},
{
"name": "Mike",
"age": 25,
"flag": 0
}
]
},
{
"type": "students",
"users": [
{
"name": "Henry",
"age": 40,
"flag": 0
},
{
"name": "Nick",
"age": 25,
"flag": 0
}
]
},
{
"type": "teachers",
"users": [
{
"name": "Kim",
"age": 20,
"flag": 0
},
{
"name": "Charles",
"age": 25,
"flag": 0
}
]
}
]
}
After update I expect this response:
{
"_id": "wef324DGSshf",
"userTypes": [
{
"type": "students",
"users": [
{
"name": "John",
"age": 20,
"flag": 1
},
{
"name": "Mike",
"age": 25,
"flag": 1
}
]
},
{
"type": "students",
"users": [
{
"name": "Henry",
"age": 40,
"flag": 0
},
{
"name": "Nick",
"age": 25,
"flag": 1
}
]
},
{
"type": "teachers",
"users": [
{
"name": "Kim",
"age": 20,
"flag": 0
},
{
"name": "Charles",
"age": 25,
"flag": 0
}
]
}
]
}
What I have tried so far is this query:
Users.update(
{ "userTypes.users.age": { $in: [20,25] } },
{ "$set": { "userTypes.users.$.flag": "1" } }
);
My solution is not updating the desired records, can anyone help me with the right query for this case?
My documents look like this
{
"_id": {
"$oid": "62825f71005ce00c5f0235c1"
},
"user": "jon",
"roles": {
"User": 2001,
},
"STOCK ": [
{
"sku": "BLACK-M",
"productname": "BLACK",
"sendout": 0,
"recived": 1,
"totalinstock": 40,
"location": "B32",
"_id": {
"$oid": "62826016005ce00c5f0235c8"
}
},
{
"sku": "CJNS",
"productname": "89796363",
"sendout": 0,
"recived": 45,
"totalinstock": 0,
"location": "B232",
"_id": {
"$oid": "62836f2d56b4f1ac79c99b8d"
}
}
],
"ORDERS": [
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "745607",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "8678678678",
"Name": "Holly ",
"Trackingnumber": 40,
"ZipCode": 00000,
"Province": "New ",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a7"
}
},
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "748840",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "354353454",
"Name": "Michael",
"Trackingnumber": 0,
"ZipCode": 00000,
"Province": "North",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a9"
}
}
]
}
I successful to return all the objects in STOCK or all the objects in ORDERS
Through this query
const foundUser= await User.find({"user":req.body.user},("Orders") ).exec()
Now I want to filter the response to include only items where "Trackingnumber" is different from 0
For the sample data I expect to receive only
{
"date": {
"$date": "2022-06-02T15:23:58Z"
},
"OrderNumber": "748840",
"City": "xxxxx",
"Address": "yyyyyy",
"Phone": "354353454",
"Name": "Michael",
"Trackingnumber": 0,
"ZipCode": 00000,
"Province": "North",
"Quantity": [
1
],
"Product_Name": [
" pants pants"
],
"SKU": [
"CJNS"
],
"_id": {
"$oid": "6298d61ba6eeec72b78332a9"
}
}
You can use an aggregation pipeline with a $filter for this:
db.collection.aggregate([
{
$match: {
"user": "jon"
}
},
{
$project: {
ORDERS: {
$filter: {
input: "$ORDERS",
as: "item",
cond: {$ne: ["$$item.Trackingnumber", 0]}
}
}
}
}
])
Playground example
User.find({"Orders" : {"Trackingnumber": 0} })
I am trying to join two collections in GraphQL query.
My data is as follows:
Cruises:
[
{
"cruiseID" : "00001",
"title" : "title 1",
"description" : "desc 1",
"startDate" : 20150820,
"endDate" : 20150827,
"numDays" : 8,
"startPort" : "Juneau, Alaska",
"roomTypes" : [
{"roomID" : "IPD"},
{"roomID" : "SDS"}
]
}, {
"cruiseID" : "00002",
"title" : "title 1",
"description" : "desc 2",
"startDate" : 20150710,
"endDate" : 20150724,
"numDays" : 14,
"startPort" : "San Diego",
"roomTypes" : [
{"roomID" : "IPD"},
{"roomID" : "SJS"},
{"roomID" : "SDS"}
]
}
]
Rooms:
[
{
"roomID": "IPD",
"roomDetails": {
"roomType": "IPD aaaaa",
"title": "aaaa",
"description": "ddddd",
"maxOccupants": 2
},
"capacity": [
{"cruiseID": "00001", "total": 21, "available": 20},
{"cruiseID": "00002", "total": 31, "available": 30}
]
},
{
"roomID": "SJS",
"roomDetails": {
"roomType": "SJS aaaa",
"title": "aaaaa",
"description": "aaaaa",
"maxOccupants": 4
},
"capacity": [
{"cruiseID": "00001", "available": 27},
{"cruiseID": "00002", "available": 27}
]
},
{
"roomID": "SDS",
"roomDetails": {
"roomType": "SDS aaa",
"title": "sssss",
"description": "sssssss",
"maxOccupants": 4
},
"capacity": [
{"cruiseID": "00001", "available": 20},
{"cruiseID": "00002", "available": 20}
]
}
]
My GraphQL schema:
type Query {
Cruises: [Cruise]
}
type RoomDetails {
roomType: String
title: String
description: String
maxOccupants: Int
}
type Capacity {
cruiseID: String
total: Int
available: Int
}
type Room {
roomID: String
roomDetails: RoomDetails
capacity: [Capacity]
}
type Cruise {
_id: ID
cruiseID: String
title: String
description: String
startDate: Int
endDate: Int
numDays: Int
startPort: String
roomTypes: [Room]
}
My GraphQL resolver where I try to print details for roomTypes by reading them from rooms collection:
const Query = {
Cruises: async () => {
data = await db.collection('cruises').find().toArray().then(res => { return res });
return data
}
}
const Cruise = {
roomTypes: async (root) => {
data = await db.collection('rooms').find({'roomID': 'SDS'}).toArray().then(res => { return res });
// prints OK
logger.debug(`root.roomTypes.roomID': ${JSON.stringify(root.roomTypes[1].roomID)}`);
// undefined
logger.debug(`root.roomTypes.roomID': ${JSON.stringify(root.roomTypes.roomID)}`);
// prints OK
logger.debug(`root.startPort': ${root.startPort}`);
return data
}
}
module.exports = {
Query,
Cruise
}
I can't find a proper join between Cruises and Rooms.
Hardcoded {'roomID': 'SDS'} works OK i.e. shows proper output.
However, I can't find a way to properly pass roomID from Cruises to the find filter. Any hints?
UPDATE:
When I implement the resolver life this:
const Cruise = {
roomTypes: async (root) => {
data = await db.collection('rooms').find({'capacity.cruiseID': root.cruiseID}).toArray().then(res => { return res })
//data = await db.collection('rooms').find().toArray().then(res => { return res })
return data
}
}
I get the data back (roomTypes fully resolved) but, it doesn't do any filtering/joining actually.
Here's what I get (edited with some rubbish data so it's shorter):
"data": {
"Cruises": [
{
"cruiseID": "00001",
"title": "and",
"startDate": 20150820,
"endDate": 20150827,
"numDays": 8,
"startPort": "Juneau, Alaska",
"roomTypes": [
{
"roomID": "IPD",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "asdr",
"maxOccupants": 2
},
"capacity": [
{
"cruiseID": "00001",
"total": 21,
"available": 20
},
{
"cruiseID": "00002",
"total": 31,
"available": 30
}
]
},
{
"roomID": "OPD",
"roomDetails": {
"roomType": "ad",
"title": "and",
"description": "asdr",
"maxOccupants": 2
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 30
},
{
"cruiseID": "00002",
"total": null,
"available": 30
}
]
},
{
"roomID": "SDD",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "and",
"maxOccupants": 2
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 25
},
{
"cruiseID": "00002",
"total": null,
"available": 25
}
]
},
{
"roomID": "SD2",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "Fast",
"maxOccupants": 4
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 22
},
{
"cruiseID": "00002",
"total": null,
"available": 22
}
]
},
{
"roomID": "SJS",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "Tasdr",
"maxOccupants": 4
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 27
},
{
"cruiseID": "00002",
"total": null,
"available": 27
}
]
},
{
"roomID": "SDS",
"roomDetails": {
"roomType": "asd",
"title": "and",
"description": "Tase",
"maxOccupants": 4
},
"capacity": [
{
"cruiseID": "00001",
"total": null,
"available": 20
},
{
"cruiseID": "00002",
"total": null,
"available": 20
}
]
}
]
},
instead of only having 2 room types (IPD, SDS) listed for cruiseID 00001 as per the entries in Cruises collection.
This is how I finally did it. It's not the cleanest code/solution but it does the job.
const Cruise = {
roomTypes: async (root) => {
let rooms = root.roomTypes;
allRooms = await db.collection('rooms').find().toArray().then(res => { return res });
rooms.forEach(room => {
let currentRoomID = room.roomID;
currentRoom = allRooms.filter(function(r) {
return r.roomID === currentRoomID;
});
currentRoomCapacity = currentRoom[0].capacity.filter(function(c) {
return c.cruiseID === root.cruiseID;
});
room.roomDetails = currentRoom[0].roomDetails
room.capacity = currentRoomCapacity
});
return rooms;
}
}
I have a database of exercises in a workout tracker, and when I do a find(), the result is this:
[
{
"_id": "5e9dacbb6512969974bd5b2d",
"day": "2020-04-10T14:07:55.905Z",
"exercises": [
{
"type": "resistance",
"name": "Bicep Curl",
"duration": 20,
"weight": 100,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b2e",
"day": "2020-04-11T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Lateral Pull",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b2f",
"day": "2020-04-12T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Push Press",
"duration": 25,
"weight": 185,
"reps": 8,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b30",
"day": "2020-04-13T14:07:55.916Z",
"exercises": [
{
"type": "cardio",
"name": "Running",
"duration": 25,
"distance": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b31",
"day": "2020-04-14T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 285,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b32",
"day": "2020-04-15T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b33",
"day": "2020-04-16T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Quad Press",
"duration": 30,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b34",
"day": "2020-04-17T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b35",
"day": "2020-04-18T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Military Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b36",
"day": "2020-04-19T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench",
"duration": 30,
"distance": 2
}
]
}
]
Then I need to get total sums of statistics from each exercise, so I used mongoose aggregate to give me this data:
[
{
"_id": null,
"totalDuration": 230,
"totalWeight": 2070,
"totalSets": 32,
"totalReps": 78,
"totalDistance": 6
}
]
I want to combine these two results in one GET request, ideally doing something similar to a push where I just push the totals at the end of the first JSON object. How do I achieve this?
Something like this:
function mergeResults(resultFromFindQuery, totalSums){
var allData = {};
allData['mongoFindresult'] = resultFromFindQuery;
allData['totalSums'] = totalSums;
return allData;
}
Then use the returned value to what you need to do. Now you have both of them in the same variable.
This question already has answers here:
$lookup multiple levels without $unwind?
(1 answer)
Mongoose: deep population (populate a populated field)
(10 answers)
Closed 3 years ago.
I have a database structured like this:
I want to get all listItems, of all lists from a user with a x ID. What's the correct way of doing that?
I'm using node with mongoose, and I tried the following:
await User.findById(user._id).populate('list');
But realized that I can't populate all ListItem's from that. Meaning, I can't do this:
await User.findById(user._id).populate('list').populate('listItem');
How can I get all listItems, of all lists from a user with a x ID?
Assuming User, List, and ListItem are Collections, you should be able to accomplish this using $lookup..
Here is a live demo of the following query..
Query:
db.users.aggregate([
{
$match: {
uniqueId: 1
}
},
{
$lookup: {
from: "lists",
localField: "uniqueId",
foreignField: "userId",
as: "lists"
}
},
{
$lookup: {
from: "listItems",
localField: "uniqueId",
foreignField: "userId",
as: "listItems"
}
}
])
Dataset:
db={ // Simulates a DB ********
"users": [ // Simulates a Collection ********
{
"firstname": "John",
"lastname": "Smith",
"email": "jsmith#gmail.com",
"password": "password123",
"uniqueId": 1
},
{
"firstname": "Jane",
"lastname": "Doe",
"email": "doe#yahoo.com",
"password": "123password",
"uniqueId": 2
}
],
"lists": [ // Simulates a Collection ********
{
"userId": 1,
"name": "Johns List 1",
"items": [
11,
12,
13
]
},
{
"userId": 2,
"name": "Groceries",
"items": [
21,
22,
23
]
}
],
"listItems": [ // Simulates a Collection ********
{
"userId": 2,
"itemId": 21,
"title": "Apple",
"notes": []
},
{
"userId": 2,
"itemId": 22,
"title": "Banana",
"notes": []
},
{
"userId": 2,
"itemId": 23,
"title": "Strawberry",
"notes": []
},
{
"userId": 1,
"itemId": 11,
"title": "Oil Change",
"notes": []
},
{
"userId": 1,
"itemId": 12,
"title": "Pick Up Grandma",
"notes": []
},
{
"userId": 1,
"itemId": 13,
"title": "Go For Run",
"notes": []
}
]
}
Result:
[
{
"_id": ObjectId("5a934e000102030405000008"),
"email": "jsmith#gmail.com",
"firstname": "John",
"lastname": "Smith",
"listItems": [
{
"_id": ObjectId("5a934e000102030405000003"),
"itemId": 11,
"notes": [],
"title": "Oil Change",
"userId": 1
},
{
"_id": ObjectId("5a934e000102030405000004"),
"itemId": 12,
"notes": [],
"title": "Pick Up Grandma",
"userId": 1
},
{
"_id": ObjectId("5a934e000102030405000005"),
"itemId": 13,
"notes": [],
"title": "Go For Run",
"userId": 1
}
],
"lists": [
{
"_id": ObjectId("5a934e000102030405000006"),
"items": [
11,
12,
13
],
"name": "Johns List 1",
"userId": 1
}
],
"password": "password123",
"uniqueId": 1
}
]