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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:32:29+00:00 2026-05-23T04:32:29+00:00

In MongoDB, the update modifier unset works as follows: Consider a Mongo DB Database

  • 0

In MongoDB, the update modifier unset works as follows:

Consider a Mongo DB Database db with a collection users. Users contain a Document, of the following format:

//Document for a user with username: joe
{
    "_id" : ObjectId("4df5b9cf9f9a92b1584fff16"),
    "relationships" : {
            "enemies" : 2,
            "friends" : 33,
            "terminated" : "many"
    },
    "username" : "joe"
}

If I want to remove the terminated key, I have to specify the $unset update modifier as follows:

>db.users.update({"username":"joe"},{"$unset":{"relationships.terminated": "many"}});

My Question is, why do I have to specify the ENTIRE KEY VALUE PAIR for the $unset to work, instead of simply specifying:

>db.users.update({"username":"joe"},{"$unset":{"relationships.terminated"}});

Mon Jun 13 13:25:57 SyntaxError: missing : after property id (shell):1

Why not?

EDIT:

If the way to $unset is to specify the entire key value pair, in accordance with JSON specifications, or to add “1” as the value to the statement, why can’t the Shell do the “1” substitution itself? Why isn’t such a feature provided? Are there any pitfalls of providing such support?

  • 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-05-23T04:32:30+00:00Added an answer on May 23, 2026 at 4:32 am

    The short answer is because {"relationships.terminated"} is not a valid json/bson object. A JSON object is composed of a key and a value, and {"relationships.terminated"} only has a key (or value, depends on how you look it).

    Affortunately to unset a field in Mongo you do not need to set the actual value of the field you want to remove. You can use any value (1 is commonly used in Mongo docs) no matter the actual value of relationships.terminated:

    db.users.update({"username":"joe"},{"$unset":{"relationships.terminated" : 1}});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to update the following document in Mongodb. doc = { id
From the MongoDB command line I can do db.user.update({userid: {$in: [435707147,88513850,466518582]}},{$unset: {f1 : 1}})
As per How do I update Array Elements matching criteria in a MongoDB document?
How do I update a mongodb collection from JS? db.collection('fruits', function (err, collection) {
I lovely update a document with values(Maps coord) from clienside (under privileges). MongoDB use
I'm trying to select a document by id I've tried: collection.update({ _id: { $oid:
In MongoDB, I have a document with a field called ClockInTime that was imported
Using mongodb I'm trying to remove a property (_id) from a child collection (ListingFeatures)
This page shows a update reaching into a previously retrieved (find) document and querying
I have a MongoDB collection and need to find the max() value of a

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.