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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:12:17+00:00 2026-06-11T05:12:17+00:00

In my upsert request, I’d like to both: update the object in place if

  • 0

In my upsert request, I’d like to both:

  • update the object in place if it’s there, aka

    db.somecollection.update({x: 1}, {$inc: {x: 1}});
    
  • provide a new object if it’s not there, aka

    db.somecollection.update({x: 1}, {whole: 0, new: 'yata', object: 42});
    

Is there a way to do that ?

The problem is that upsert seems to work only if you already provide an object in the first place, not a modifier.

edit: I’ll be more specific.

I want to update objects of the form

{_id: 12, a: 123, b: 234, c: 345}

my update is just an increment of 1 on one of a, b or c. If the object is not in the base then I’d like to create an object of the form

{_id: 13, a: 1, b: 0, c: 0}

if a was the field I was trying to increment.

Now, if I read the manual correctly, I can write

db.somecollection.update({_id: 12}, {$inc: {a: 1}}, true);

which will work fine to update my a field but which will not create the fields b and c if the object doesn’t exist.

  • 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-11T05:12:18+00:00Added an answer on June 11, 2026 at 5:12 am

    Upserts can work with modifiers; the modifier gets applied to the selector/criteria object when a new document needs to be created. See here. But you do need to set the upsert parameter to true in your update call:

    db.somecollection.update({x: 1}, {$inc: {x: 1}}, true);
    

    UPDATE

    To get the behavior you’re looking for:

    db.somecollection.update({_id: 12}, {$inc: {a: 1, b: 0, c: 0}}, true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I update if exists, insert if not (aka upsert or
Possible Duplicate: How do I update if exists, insert if not (aka upsert or
I like to use Upsert stored procedures that update records if they exist or
I'm using \lithium\data\source\MongoDB::update() to do an upsert in safe mode. On some updates, there
http://en.wikipedia.org/wiki/Upsert Insert Update stored proc on SQL Server Is there some clever way to
Upsert (Replace) Update If Exists Insert If Not Exists (Using Primary Key as Pipelined)
Here is my upsert code: UPDATE LastTicket SET LastTicketNumber=LastTicketNumber+1 OUTPUT INSERTED.LastTicketNumber WHERE CategoryId='1'; IF
I am using a MERGE statement as an UPSERT to either add a new
I want to upsert a document with the mongo-ruby-driver using something like the following-
I'm relatively new to MongoDB and having trouble with a more advanced upsert. I've

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.