I am making a simple API in which i want to select user which is not in array of object. I want to get user which is not in request and friendList array of object.I have tried with mapping but it repeatedly push the object into array.
API
[
{
"_id": "6304e42231ef2e7a4dec924d",
"email": "devrajstha@gmail.com",
"name": "Deepa Shrestha",
"phone": 9819339011,
"profile": "https://eduzim.co.zw/news/wp-content/uploads/2022/09/6486/pictures-former-rhythm-city-actress-pearl-petronella-tshuma-set-to-star-in-good-men-meet-the-whole-cast.jpg",
"dob": "2001-03-26T08:00:00.000Z",
"join_at": "2022-08-23T14:27:43.249Z",
"password": "Devraj 123@",
"__v": 0,
"friendList": [],
"request": [
{
"_id": "63cd4c1a675ce5566d3ee6df",
"sender": "dstha221@gmail.com",
"receiver": "devrajstha@gmail.com",
"sendAt": "2023-01-22T14:45:20.147Z",
"status": "NA",
"__v": 0
}
]
},
{
"_id": "6304e43b31ef2e7a4dec9251",
"email": "devrajstha88@gmail.com",
"name": "Devraj Shrestha",
"phone": 9819339011,
"profile": "https://i.etsystatic.com/5713076/r/il/db3028/660348889/il_1588xN.660348889_byu0.jpg",
"dob": "2001-03-26T08:00:00.000Z",
"join_at": "2022-08-23T14:27:43.249Z",
"password": "Devraj 123@",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6304e44431ef2e7a4dec9254",
"email": "dstha221@gmail.com",
"name": "Radha Shrestha",
"phone": 9819339011,
"profile": "https://www.theskinnybeep.com/wp-content/uploads/2019/01/Versace-Man-2019.jpg",
"dob": "2001-03-26T08:00:00.000Z",
"join_at": "2022-08-23T14:27:43.249Z",
"password": "Devraj 123@",
"__v": 0,
"friendList": [
{
"_id": "63cd500ed514da391523b7f5",
"friend": "dstha221@gmail.com",
"me": "devrajstha@gmail.com",
"acceptAt": "2023-01-22T15:02:23.379Z",
"__v": 0
}
],
"request": []
},
{
"_id": "630b5eea134f2e8b9d7f56d0",
"email": "jeevan@gmail.com",
"dob": "2002-12-13T00:00:00.000Z",
"name": "Rajib Shrestha",
"password": "Rajib 123@",
"profile": "https://i.pinimg.com/originals/13/98/08/139808514282e8f491afee6b3768d8ce.jpg",
"join_at": "2001-12-13T00:00:00.000Z",
"friendList": [],
"request": [
{
"_id": "63cd4c1b675ce5566d3ee6e1",
"sender": "dstha221@gmail.com",
"receiver": "jeevan@gmail.com",
"sendAt": "2023-01-22T14:45:20.147Z",
"status": "NA",
"__v": 0
}
]
},
{
"_id": "630b5f1a134f2e8b9d7f56d1",
"email": "jeevanstha12@gmail.com",
"dob": "2001-03-05T00:00:00.000Z",
"join_at": "2003-05-07T00:00:00.000Z",
"name": "Amit Shrestha",
"password": "Amit 123@",
"profile": "https://66.media.tumblr.com/50e2ebcf640e4aa38911078873baba79/tumblr_oc274g0Zi41va1j7ko2_1280.jpg",
"friendList": [],
"request": [
{
"_id": "63cd4c1c675ce5566d3ee6e3",
"sender": "dstha221@gmail.com",
"receiver": "jeevanstha12@gmail.com",
"sendAt": "2023-01-22T14:45:20.147Z",
"status": "NA",
"__v": 0
}
]
},
{
"_id": "6314510e50d002c0e5aa074e",
"profile": "https://i.pinimg.com/736x/7b/ce/11/7bce11dd52632801c864573812bf832f--trendy-mens-haircuts-men-haircuts-.jpg",
"name": "Rabina Tandan",
"password": "Rabina",
"dob": "2001-09-08T18:15:00.000Z",
"email": "rabina@gmail.com",
"friendList": [],
"request": []
},
{
"_id": "631452d0e1c2acf0be28ce43",
"email": "mahima@gmail.com",
"name": "Rajan Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-04T07:07:33.652Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "63145359e1c2acf0be28ce4c",
"email": "manisha@gmail.com",
"name": "Manisha Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-04T07:07:33.652Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "63145395e1c2acf0be28ce51",
"email": "nabaraj@gmail.com",
"name": "Prakash Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-04T07:07:33.652Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6317f3db17ab46516a37a291",
"email": "nabin@gmail.com",
"name": "Prativa Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:23:02.787Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6317f82b17ab46516a37a29c",
"email": "nabinrai@gmail.com",
"name": "Nabin Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:23:02.787Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6317f85317ab46516a37a2a1",
"email": "nabinrajrai@gmail.com",
"name": "Himesh Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:23:02.787Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6317f88817ab46516a37a2a5",
"email": "mamata@gmail.com",
"name": "Manju Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:23:02.787Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": [
{
"_id": "63cd4c1f675ce5566d3ee6e5",
"sender": "dstha221@gmail.com",
"receiver": "mamata@gmail.com",
"sendAt": "2023-01-22T14:45:20.147Z",
"status": "NA",
"__v": 0
}
]
},
{
"_id": "6317fa0c17ab46516a37a2af",
"email": "mamatarai@gmail.com",
"name": "Prakash Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:23:02.787Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": [
{
"_id": "63cd4c20675ce5566d3ee6e7",
"sender": "dstha221@gmail.com",
"receiver": "mamatarai@gmail.com",
"sendAt": "2023-01-22T14:45:20.147Z",
"status": "NA",
"__v": 0
}
]
},
{
"_id": "6317fa4117ab46516a37a2b7",
"email": "mamatarajrai@gmail.com",
"name": "BAbin Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:23:02.787Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6317fb622a4a331214a86a3c",
"email": "mamatarajrai12@gmail.com",
"name": "Maniat Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:59:43.137Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6317fb992a4a331214a86a40",
"email": "mamatarajrai123@gmail.com",
"name": "Mamata Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:59:43.137Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6317fe7b2a4a331214a86a4f",
"email": "anish@gmail.com",
"name": "MAnish Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:59:43.137Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
},
{
"_id": "6317fea22a4a331214a86a57",
"email": "anish12@gmail.com",
"name": "Umesh Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"dob": "2001-02-09T00:00:00.000Z",
"join_at": "2022-09-07T01:59:43.137Z",
"password": "Rajan",
"__v": 0,
"friendList": [],
"request": []
}
]
Js
router.get("/people_y_m_n", (req, res) => {
let users = [];
let new_array=[]
const myEmail = 'dstha22123@gmail.com';
userModel
.aggregate([
{
$lookup: {
from: "friend_lists",
localField: "email",
foreignField: "friend",
as: "friendList",
},
},
{
$lookup: {
from: "frieend_requests",
localField: "email",
foreignField: "receiver",
as: "request",
},
},
])
.exec((err, docs) => {
console.log(docs.length)
docs.map((doc) => {
console.log(myEmail==doc.request.sender)
doc.request.map((val)=>{
if(doc.email!=val.sender||doc.email!=val.receiver && myEmail!=val.receiver || myEmail!=val.sender ){
new_array.push({
email:doc.email,
name:doc.name,
profile:doc.profile,
_id:doc._id
})
}
})
doc.friendList.map((val)=>{
if(doc.email!=val.friend||doc.email!=val.me && myEmail!=val.friend || myEmail!=val.me ){
new_array.push({
email:doc.email,
name:doc.name,
profile:doc.profile,
_id:doc._id
})
}
})
new_array.map((val)=>{
if(val.email!=doc.email){
users.push({
email:doc.email,
name:doc.name,
profile:doc.profile,
_id:doc._id
})
}
})
});
res.send(users);
});
});
I get the many duplicate object and new_array push details of all user,I tried to solve but.How can I solve.Thanks in advane. In API call
[
{
"email": "devrajstha88@gmail.com",
"name": "Devraj Shrestha",
"profile": "https://i.etsystatic.com/5713076/r/il/db3028/660348889/il_1588xN.660348889_byu0.jpg",
"_id": "6304e43b31ef2e7a4dec9251"
},
{
"email": "dstha221@gmail.com",
"name": "Radha Shrestha",
"profile": "https://www.theskinnybeep.com/wp-content/uploads/2019/01/Versace-Man-2019.jpg",
"_id": "6304e44431ef2e7a4dec9254"
},
{
"email": "jeevan@gmail.com",
"name": "Rajib Shrestha",
"profile": "https://i.pinimg.com/originals/13/98/08/139808514282e8f491afee6b3768d8ce.jpg",
"_id": "630b5eea134f2e8b9d7f56d0"
},
{
"email": "jeevan@gmail.com",
"name": "Rajib Shrestha",
"profile": "https://i.pinimg.com/originals/13/98/08/139808514282e8f491afee6b3768d8ce.jpg",
"_id": "630b5eea134f2e8b9d7f56d0"
},
{
"email": "jeevanstha12@gmail.com",
"name": "Amit Shrestha",
"profile": "https://66.media.tumblr.com/50e2ebcf640e4aa38911078873baba79/tumblr_oc274g0Zi41va1j7ko2_1280.jpg",
"_id": "630b5f1a134f2e8b9d7f56d1"
},
...
]
new_array has following array of object
[
{
"email": "devrajstha@gmail.com",
"name": "Deepa Shrestha",
"profile": "https://eduzim.co.zw/news/wp-content/uploads/2022/09/6486/pictures-former-rhythm-city-actress-pearl-petronella-tshuma-set-to-star-in-good-men-meet-the-whole-cast.jpg",
"_id": "6304e42231ef2e7a4dec924d"
},
{
"email": "dstha221@gmail.com",
"name": "Radha Shrestha",
"profile": "https://www.theskinnybeep.com/wp-content/uploads/2019/01/Versace-Man-2019.jpg",
"_id": "6304e44431ef2e7a4dec9254"
},
{
"email": "jeevan@gmail.com",
"name": "Rajib Shrestha",
"profile": "https://i.pinimg.com/originals/13/98/08/139808514282e8f491afee6b3768d8ce.jpg",
"_id": "630b5eea134f2e8b9d7f56d0"
},
{
"email": "jeevanstha12@gmail.com",
"name": "Amit Shrestha",
"profile": "https://66.media.tumblr.com/50e2ebcf640e4aa38911078873baba79/tumblr_oc274g0Zi41va1j7ko2_1280.jpg",
"_id": "630b5f1a134f2e8b9d7f56d1"
},
{
"email": "mamata@gmail.com",
"name": "Manju Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"_id": "6317f88817ab46516a37a2a5"
},
{
"email": "mamatarai@gmail.com",
"name": "Prakash Shrestha",
"profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
"_id": "6317fa0c17ab46516a37a2af"
}
]