0

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": "[email protected]",
    "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": "[email protected]",
        "receiver": "[email protected]",
        "sendAt": "2023-01-22T14:45:20.147Z",
        "status": "NA",
        "__v": 0
      }
    ]
  },
  {
    "_id": "6304e43b31ef2e7a4dec9251",
    "email": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
        "me": "[email protected]",
        "acceptAt": "2023-01-22T15:02:23.379Z",
        "__v": 0
      }
    ],
    "request": []
  },
  {
    "_id": "630b5eea134f2e8b9d7f56d0",
    "email": "[email protected]",
    "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": "[email protected]",
        "receiver": "[email protected]",
        "sendAt": "2023-01-22T14:45:20.147Z",
        "status": "NA",
        "__v": 0
      }
    ]
  },
  {
    "_id": "630b5f1a134f2e8b9d7f56d1",
    "email": "[email protected]",
    "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": "[email protected]",
        "receiver": "[email protected]",
        "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": "[email protected]",
    "friendList": [],
    "request": []
  },
  {
    "_id": "631452d0e1c2acf0be28ce43",
    "email": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
        "receiver": "[email protected]",
        "sendAt": "2023-01-22T14:45:20.147Z",
        "status": "NA",
        "__v": 0
      }
    ]
  },
  {
    "_id": "6317fa0c17ab46516a37a2af",
    "email": "[email protected]",
    "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": "[email protected]",
        "receiver": "[email protected]",
        "sendAt": "2023-01-22T14:45:20.147Z",
        "status": "NA",
        "__v": 0
      }
    ]
  },
  {
    "_id": "6317fa4117ab46516a37a2b7",
    "email": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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 = '[email protected]';
  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": "[email protected]",
    "name": "Devraj Shrestha",
    "profile": "https://i.etsystatic.com/5713076/r/il/db3028/660348889/il_1588xN.660348889_byu0.jpg",
    "_id": "6304e43b31ef2e7a4dec9251"
  },
  {
    "email": "[email protected]",
    "name": "Radha Shrestha",
    "profile": "https://www.theskinnybeep.com/wp-content/uploads/2019/01/Versace-Man-2019.jpg",
    "_id": "6304e44431ef2e7a4dec9254"
  },
  {
    "email": "[email protected]",
    "name": "Rajib Shrestha",
    "profile": "https://i.pinimg.com/originals/13/98/08/139808514282e8f491afee6b3768d8ce.jpg",
    "_id": "630b5eea134f2e8b9d7f56d0"
  },
  {
    "email": "[email protected]",
    "name": "Rajib Shrestha",
    "profile": "https://i.pinimg.com/originals/13/98/08/139808514282e8f491afee6b3768d8ce.jpg",
    "_id": "630b5eea134f2e8b9d7f56d0"
  },
  {
    "email": "[email protected]",
    "name": "Amit Shrestha",
    "profile": "https://66.media.tumblr.com/50e2ebcf640e4aa38911078873baba79/tumblr_oc274g0Zi41va1j7ko2_1280.jpg",
    "_id": "630b5f1a134f2e8b9d7f56d1"
  },
...
]

new_array has following array of object

[
  {
    "email": "[email protected]",
    "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": "[email protected]",
    "name": "Radha Shrestha",
    "profile": "https://www.theskinnybeep.com/wp-content/uploads/2019/01/Versace-Man-2019.jpg",
    "_id": "6304e44431ef2e7a4dec9254"
  },
  {
    "email": "[email protected]",
    "name": "Rajib Shrestha",
    "profile": "https://i.pinimg.com/originals/13/98/08/139808514282e8f491afee6b3768d8ce.jpg",
    "_id": "630b5eea134f2e8b9d7f56d0"
  },
  {
    "email": "[email protected]",
    "name": "Amit Shrestha",
    "profile": "https://66.media.tumblr.com/50e2ebcf640e4aa38911078873baba79/tumblr_oc274g0Zi41va1j7ko2_1280.jpg",
    "_id": "630b5f1a134f2e8b9d7f56d1"
  },
  {
    "email": "[email protected]",
    "name": "Manju Shrestha",
    "profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
    "_id": "6317f88817ab46516a37a2a5"
  },
  {
    "email": "[email protected]",
    "name": "Prakash Shrestha",
    "profile": "https://i.pinimg.com/736x/7a/31/ab/7a31ab5b22ff3144e66ab42c3ba46040--short-hairstyles-for-men-long-haircuts.jpg",
    "_id": "6317fa0c17ab46516a37a2af"
  }
]
Debaraj Stha
  • 1
  • 1
  • 5

1 Answers1

0

I can see from your question that you've done the lookup aggregations. After that you should be able to simply filter the result so that you are left only with users with empty friend and request lists.

It should look something like this:

const users = await userModel.aggregate([
      {
        $lookup: {
          from: "friend_lists",
          localField: "email",
          foreignField: "friend",
          as: "friends",
          
        },
      },
      {
        $lookup: {
          from: "frieend_requests",
          localField: "email",
          foreignField: "receiver",
          as: "requests",
          
        },
      },          
    ])

Now we can filter the users:

const finalUsers = users.filter((user) => user.friends.length === 0 && user.requests.length === 0);