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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:37:20+00:00 2026-06-12T20:37:20+00:00

Is there a method for inserting a new document which does not contain certain

  • 0

Is there a method for inserting a new document which does not contain certain properties in MongoDB?

For example, say I want to insert a document which looks like:

{
    'f_name': 'Jim',
    'l_name': 'Jones',
    'age': 21,
    'country': 'UK',
    ...etc...
}

But I wanted to insert this document only if a document containing

{
    'f_name': 'Jim',
    'l_name': 'Jones'
}

did not already exist. Is there a way of doing this in a single operation (e.g. like an upsert, but without modifying any existing matching documents if one exists), or does this have to be done in two seperate operations (a lookup to see if one exists, then an insert)?

It’s not majorly important, but it would be quite useful to know about and could save a lot of 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-12T20:37:22+00:00Added an answer on June 12, 2026 at 8:37 pm

    You can use one of two options:

    • A compound unique index
    • An upsert like so:

      db.col.update({f_name: ‘Jim’, l_name: ‘Jones’}, {//your doc here}, {upsert: true})

    This will basically search for a document where these fields already have these values and updates (you can just update the fields with their same value) or inserts if the find criteria does not return results.

    A last ditch try it is to use two queries, however the upsert above should work quite nicely.

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

Sidebar

Related Questions

is there a method to verify if a NSString haven't characters? example of string
Is there a method which allows to know parent item of leaf tapped item?
I'm inserting a bunch of new rows into a table which is defined as
I'm new to jasmine, and I want to write some tests for mongodb operations.
I'm currently inserting/updating fields like this (if there's a better way, please say so
Is there some method that accepts inserting custom html without having to actually add
The new thumbnail feature works great when inserting a new file. Is there a
Is there a method to return the position of the character in the JTextField.
is there a method to link specific files so that I can reference them
Is there any method I can get the full JSON data from jqGrid when

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.