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 8360681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:31:32+00:00 2026-06-09T11:31:32+00:00

I am using MongoDB and trying to remove array elements (themselves embedded documents) from

  • 0

I am using MongoDB and trying to remove array elements (themselves embedded documents) from documents in a DB matching a criteria. FOr this I am trying to use the $pull operator in the update command. But I am unable to make this work in some cases (See description below). What am I missing?

Thanks in advance.

-Sachin

> use test
switched to db test

//First, insert a record with an array of addresses, with array elements being embedded objects with exactly 1 element (email)
> db.users.insert({
    name: 'smith',
    addresses:[{email:'a@b'},{email:'c@d'}]
    });... ... ...


//Result of the insertion
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "a@b" }, { "email" : "c@d" } ] }


//From records with name= Smith, try to $pull any array elements with email a@b

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}});> 

//After successful $pull

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }


//Now insert  a record with an array of addresses, with array elements being embedded objects with exactly 2 elements (email, phone)

> db.users.insert({
    name: 'smith',
    addresses:[{email:'a@b', phone: '12345'},{email:'c@d',phone :'54321'}]
    });... ... ... 


//Result of the insertion

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }

//From records with name= Smith, again try to $pull any array elements with email a@b

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}})


// - Unsuccessful $pull (Why? How to fix this)
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }


//Meanwhile, the single element pull still works as before -

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'c@d'}}})


> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }
> 

Thanks for the resposen, although that didn’t work. Here is the transcript of the Mongo shell.

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {                               
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }
> db.users.update({name:'smith'}, {$pull:{"addresses.email": 'a@b'}})
Modifier spec implies existence of an encapsulating object with a name that already represents a non-object, or is referenced in another $set clause
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }
> 

…so basically the dot notation didnt work out.

  • 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-09T11:31:34+00:00Added an answer on June 9, 2026 at 11:31 am

    Ok, so I found the answer.

    First thing, I was using MongoDB 1.2.2, and this version didnt support the update $pull operation as described above.

    Next, I upgraded to MongoDB 2.06 (latest stable). Then when I use the old database created in 1.2.2, the same result.

    Next, I created a new DB in 2.06 and then tried the suggestion by @sergio-tulentsev, i.e.
    db.users.update({name:’smith’}, {$pull:{“addresses.email”: ‘a@b’}})

    Unfortunately, this didnt work either.

    Last, I tried the initial command I had not been able to execute

    db.users.update({name:’smith’}, {$pull:{addresses:{email:’a@b’}}})

    And it worked!!!

    So takeaway:
    1. Update MongoDB server
    2. Old version of Database file wont work, only works with new database file. Now I need to somehow migrate my data to the newer version.

    UPDATE:…this migration was as simple as issuing the mongod –upgrade command.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using mongodb I'm trying to remove a property (_id) from a child collection (ListingFeatures)
I am trying to retrieve five recent documents from Deal collection in a MongoDB
I am trying to insert nested documents in to a MongoDB using C#. I
I am trying to connect to my MongoDB using authentication. I did this on
I'm trying to Or some conditions in MongoDB (using the Java driver). This is
I'm trying to retrieve only a subset of fields from mongodb using Java driver.
I am trying to compute an average value from a collection using the mongodb
Using MongoDB I'm trying to copy a database from one server to another. My
I'm trying to use MongoDB with my POCOs. Using mongodb-csharp library ( http://github.com/samus/mongodb-csharp ),
I was trying to use the count() Mongodb feature (db.collection_name.count({data:value}) using the Ruby Driver.

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.