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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:01:46+00:00 2026-06-04T16:01:46+00:00

I have a fairly straight forward database structure I would like to create with

  • 0

I have a fairly straight forward database structure I would like to create with MongoDB. My structure looks like this:

    {url: value,
     users: {user_id: value}}

An example document might looks like this:

{'url': 'http://stackoverflow.com/', users: {'billy': 12, 'tommy': 2}}

I’m using PyMongo to update my database and add documents as show above. I can successfully add data to my database, but I cannot seem to duplicate the above data structure.

    for d in data:
        foo.update(
            {'id': d.get('url')}, 
            {'$addToSet' :{'users': {d.get('user'): 'NaN'}}},
            upsert=True)

The above code inserts but the ‘user’ structure is not what I intended. I would like:

users: {'billy': 'NaN', 'tommy': 'NaN'}

and I get:

users : [ { 'billy' : "NaN" }, { 'tommy' : "NaN" } ]

Can anyone help? Thanks for your time.

  • 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-04T16:01:48+00:00Added an answer on June 4, 2026 at 4:01 pm

    Per my comment above I would suggest not embedding a document but instead using an array. The following would work well:

    {'url': 'http://stackoverflow.com/', 
     'users': [{'user': 'billy', 'value': 12},
               {'user': 'tommy', 'value': 2}}
    

    This way you can leverage $push, $pull etc. as well as $addToSet. For the latter you could add a new user like this:

    foo.update({'url': d.get('url')}, {'$addToSet': {'users': {'user': 'john', 'value': 0}}})
    

    Now lets say you just wanted to increment a value for a given user, you could easily do that too:

    foo.update({'url': d.get('url'), 'users.user': 'tommy'}, {'$inc': {'users.$.value': 1}})
    

    To see more things that you could do with the schema I proposed I would read this

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

Sidebar

Related Questions

I have a fairly straight-forward navigation based app. It is like a wizard in
I have a question which i suspect is fairly straight forward. I have the
I currently have a fairly straight forward app, my first attempt at MVVM using
I have a fairly straight-forward scenario that I am trying to solve but I'm
This is driving me a little crazy because it seems like a fairly straight
I have a fairly straight forward question. I have a commandline application I'm writing
My schema is a fairly straight-forward membership and roles schema: Users table: UserId (PK)
My question is fairly straight forward and hopefully simple. I've got a database that
There probably is a fairly simple and straight-forward answer for this, but for some
I have a fairly straight-forward sequential approval workflow that has an EnableModificationActivity that is

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.