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

  • Home
  • SEARCH
  • 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 8390859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:06:50+00:00 2026-06-09T19:06:50+00:00

Mongodb updates provide the $push modifier to append to an array. My problem is

  • 0

Mongodb updates provide the $push modifier to append to an array. My problem is that i want this to happen on a dict e.g

If my record looks like this initially:

{"collaborations":{'id1':{'role':'dev','scope':'dev'}}}

I want to add another item(“id2” below) to the “collaborations” field dict to look something like this:

{"collaborations":{'id1':{'role':'dev','scope':'dev'},'id2':{'role':'qa','scope':'qa'}}}

I am trying with $push:

my_record.update({match_criteria},{$push,{"collaborations":{'id2':{'role':'qa','scope':'qa'}}}})

and also with $addToSet:

my_record.update({match_criteria},{$,{"collaborations":{'id2':{'role':'qa','scope':'qa'}}}})

With both of these, mongodb throws as error “Cannot apply $addToSet($push) modifier to non-array”.

How can this be done for dict types? Any ideas?

  • 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-09T19:06:51+00:00Added an answer on June 9, 2026 at 7:06 pm

    The problem is that $addToSet and $push modifiers work with arrays.

    To update sub-document (that is what you need here) just use $set modifier with dot notation to access sub-document (field.subfield):

    my_record.update({
        match_criteria
    }, {
        '$set': {
            'collaborators.id2': {
                // new document fields here
            }
        }
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In MongoDB I have a query which looks like this to find out for
I'm trying to use MongoDB's Java driver to make two updates ($set and $push)
MongoDB has support for atomic updates. I.e. I can be sure that when a
i want to update all records in my database(mongodb),I tried to use command below
I have a simple structure in mongodb, with nested array. How can I update
Using MongoDB C# driver ( http://github.com/samus/mongodb-csharp ), seems that I'm unable to get the
As per How do I update Array Elements matching criteria in a MongoDB document?
In MongoDB, I have a document with a field called ClockInTime that was imported
In MongoDB, a field can have multiple values (an array of values). Each of
In MongoDB, the update modifier unset works as follows: Consider a Mongo DB Database

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.