Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9168525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:38:44+00:00 2026-06-17T15:38:44+00:00

I want to remove a specific element from array stored in mongodb document. I

  • 0

I want to remove a specific element from array stored in mongodb document.
I am using this:

 model.registerCompany.findOneAndUpdate({companyKey:"a key"},
    {$pop:{onlineEmployees:"John"}},
    function(err,doc){
    if(!err)
        console.log("Online list modified: ",doc);
    else
        console.log("Online list modified error :",err);
});

But I am not sure if the $pop removes the specific element “John” from array(onlineEmployees) or just pop out the last element from it.

Am i doing it right or there is another way to do it.?

I think i got the answer .. $pull is used for this purpose as explained here in the link:
http://docs.mongodb.org/manual/reference/operator/pull/#_S_pull

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:38:45+00:00Added an answer on June 17, 2026 at 3:38 pm

    The $pop operator will remove first or last element of the array, which may not necessarily be the right one.

    If you want a specific element, you can $pull the item with defined criteria:

       model.registerCompany.findOneAndUpdate({companyKey:"a key"},
        {$pull:{onlineEmployees:"John"}},
    

    You have to make sure the value in the array is unique, for $pull removes every element matching the name ‘John’.

    If identical values exist in the array, you need to use $unset and $ positional operator to set the target element value to null(unfortunately $unset won’t remove elements) and then use $pull to remove the element with null value. To do that, you have to make sure valid value can not be null. In that case, the code could be like:

    model.registerCompany.findOneAndUpdate({companyKey:"a key", onlineEmployees:"John"},{ $unset: { "onlineEmployees.$" : '' } } ) 
    model.registerCompany.findOneAndUpdate({companyKey:"a key"},{ $pull: { "onlineEmployees" : null } } ) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write something that removes a specific element from an array. I
I want to remove element from queue with specific value. How to do such
Possible Duplicate: Removing an element from an Array (Java) How to remove specific String
Possible Duplicate: Remove specific element from a javascript array? I am having an array,
i want to remove the link from my header using CSS so that only
Using the Word Interop API I have a document from which I remove text
I want to remove specific elements from my List. I don't want to do
I want to remove a specific object from my list wielrennerslist but some how
I want to remove specific elements from the page response, before it is handed
What if instead of removing duplicate elements from an array, I want to remove

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.