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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:27:35+00:00 2026-06-12T04:27:35+00:00

As part of a for-in loop in python using pymongo I want to add

  • 0

As part of a for-in loop in python using pymongo I want to add some nested documents/objects inside a linktype field which is to be within a links field:
neither the links field or linktype field exists before the first such entries are to be added.

What is the commands to do this ?

Here is an item before adding links:

item = {
    "_id" : ObjectId("5067c26b9d595266e25e825a"),
    "name": "a Name"
}

And after adding one link of type typeA:

toType = "typeA"
to_link = {"_id" : ObjectId("5067c26b9d595266e25e825b"), "property":"value"}

{
    "_id" : ObjectId("5067c26b9d595266e25e825a"),
    "name": "a Name",
    "links" : {
        "typeA":{
            {"_id" : ObjectId("5067c26b9d595266e25e825b"), "property":"value"}       
        }
    }
}

I have tried:

db.collection.update({"name":"a Name"},{{"links":{"$addToSet":{toType:to_link}}})

which doesnt work.
If I just use:

db.collection.update({"name":"a Name"},{ {"$addToSet":{toType:to_link}} )

that works but that is not what i want.

  • 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-12T04:27:36+00:00Added an answer on June 12, 2026 at 4:27 am

    $addToSet is for adding to an array. To add a new property to an existing embedded object you need to use the $set operator and dot notation as:

    db.collection.update({name: 'a name'}, {$set: {'links.' + toType: to_link}})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a python script that runs a loop in which it calls
I am using some python to do some variable name generation. For some reason
In some part of my python program , I need to do this -
Python: 2.7.3 Flask: 0.9 Hi, I want to make car simulator using Apscheduler .
I want to run a part of my loop for a certain time: 60
as part of a script i am working on i have the following: loop
Part of the GUI I'm building using tkinter has a pop-up window that says
Edit: Just for clarification I am using python, and would like to do this
I am using a module that is part of a commercial software API. The
I'm learning Python, and I have a situation where I want to consume items

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.