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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:59:48+00:00 2026-06-17T22:59:48+00:00

I have a document: { ‘profile_set’ : [ { ‘name’ : ‘nick’, ‘options’ :

  • 0

I have a document:

{ 'profile_set' :
  [
    { 'name' : 'nick', 'options' : 0 },
    { 'name' : 'joe',  'options' : 2 },
    { 'name' : 'burt', 'options' : 1 }
  ] 
}

and would like to add a new document to the profile_set set if the name doesn’t already exist (regardless of the option).

So in this example if I tried to add:

{'name' : 'matt', 'options' : 0}

it should add it, but adding

{'name' : 'nick', 'options' : 2}

should do nothing because a document already exists with name nick even though the option is different.

Mongo seems to match against the whole element and I end up with to check if it’s the same and I end up with

profile_set containing [{'name' : 'nick', 'options' : 0}, {'name' : 'nick', 'options' : 2}]

Is there a way to do this with $addToSet or do I have to push another command?

  • 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-17T22:59:49+00:00Added an answer on June 17, 2026 at 10:59 pm

    You can qualify your update with a query object that prevents the update if the name is already present in profile_set. In the shell:

    db.coll.update(
        {_id: id, 'profile_set.name': {$ne: 'nick'}}, 
        {$push: {profile_set: {'name': 'nick', 'options': 2}}})
    

    So this will only perform the $push for a doc with a matching _id and where there isn’t a profile_set element where name is 'nick'.

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

Sidebar

Related Questions

I have a document in HTML and CSS. I would like to have headers
i have a document like this: English English English English 中文中文中文中文中文 English English English
I have pdf document, for example 25 pages. How to add one blank page
I have a document library in SharePoint. When a new file is uploaded to
I have a document, something like this: <root> <A node=1/> <B node=2/> <A node=3/>
I have Document with EmbeddedDocument like this in mongoengine . how can create ReferenceField
I have Document class: public class Document { public int FromCompanyId { get; set;
I have a Document Library for which I need to add a user (SPRoleAssignment
I have document like { id : 100, heros:[ { nickname : test, spells
I have a document that looks similar to this: { 'user_id':'{1231mjnD-32JIjn-3213}', 'name':'John', 'campaigns': [

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.