0

Guys I want to $pull an element from array. Here:

User's Follow:

{ follow:
   [ 5edfe8f3bfc9d677005d55ca,
     5edfe92fbfc9d677005d55cc,
     5ee2326cc7351c5bb0b75f1a ],

follow id:(Which we will pull)

5edfe92fbfc9d677005d55cc

The process:

    .
    .
    
    User.update(user,{$pull: { follow: friend}}, function(err){
          if(err){
          console.log(err);
        }else{
          res.send("Success")
        } 
        });
    .
    .

Also I have tried with $elemMatch: Mongodb Unfollow With $pull

Question: How to $pull specific element from an array?

But it doesnt work. Can someone help me to solve this problem ?

Note: Guys the id's are given as an example to understand well. The follow is the friends of current user. And if user wants to unfollow then it will $pull the friend(friend: the user on the profile page) Think like facebook's follow-unfollow request. So that if you want to answer please do not give example with id's above because follow id's always change user to user.

So how to pull specific element from an array?

Full Code:

app.post('/cikar/:id', function(req, res){

  User.find(req.user
  
  ).exec(function(err, user){
  
  if(err){
    console.log(err);
  }
    
    User.find({"username":req.params.id}, function(err, friend){
      
      if(err){
        console.log(err);
      }
    User.update(user,{$pull: { follow: friend}}, function(err){
      if(err){
      console.log(err);
    }else{
      res.send("Success")
    } 
    });
  });
  });
  });
17
  • This may be what you need Commented Jul 4, 2020 at 14:28
  • I have eddited the question. In my project there is no certain ids because friends(follow) changes user to user Commented Jul 4, 2020 at 14:32
  • Can you show your schema please? Your query looks okay to me. Also, is friend an object or friend's ID? Commented Jul 4, 2020 at 16:03
  • friend is the user on the profile. Like owner of the profile Commented Jul 4, 2020 at 17:33
  • About schema: Follower is an object id Commented Jul 4, 2020 at 17:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.