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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:30:11+00:00 2026-06-13T06:30:11+00:00

When I do this: client_id = req.param(client_id) ? null client = name: req.param clientName

  • 0

When I do this:

client_id = req.param("client_id") ? null
client =
  name: req.param "clientName"
  status: 'active'

Client.update {_id: client_id}, client, {upsert: true}, (err, updRes) ->
  if err
    res.json
      error: "Couldn't create client"
  else
    res.json client

It will create a new client record, except with a null _id field. I assume that’s because the insert part of the upsert looks to the query to create the document. How can I do it so that if no doc is found, then insert a new ObjectId?

  • 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-13T06:30:12+00:00Added an answer on June 13, 2026 at 6:30 am

    Not sure if you have figured this one out yet, but in case you don’t want to run into trouble with unique key constraints like Mustafa mentioned above, the way I’ve done it is by using mongoose’s findByIdAndUpdate:

    // require mongoose
    
    client_id = req.param("client_id") ? new mongoose.Types.ObjectId
    client =
      name: req.param "clientName"
      status: 'active'
    
    Client.findByIdAndUpdate client_id, client, {upsert: true}, (err, updRes) ->
      if err
        res.json
          error: "Couldn't create client"
      else
        res.json client
    

    I’ve never written CoffeeScript, so forgive me if some of that syntax is wrong, but I think it should be fairly obvious what I’m trying to do.

    One thing to notice is that you need to make sure client_id is neither an empty string (as that would cause an ‘Invalid ObjectID’ error) nor null (because mongoose seems infer the new document’s id from the one you’re querying for). In case it is, I create a new ObjectId, which will cause the query to miss and therefore create a new document with that ID since I pass {upsert: true}.

    This seems to have solved the problem for me.

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

Sidebar

Related Questions

Campaign.find {client_id:req.param('client_id')}, (error, campaigns) -> if error response = error: error.message else for campaign
I have a relationships table, the table looks something like this ------------------------ | client_id
I get this error in my app { error: { message: Missing client_id parameter.,
I have a proposals table like this - ID (auto_increment) - proposal_id - client_id
I have this php code $jsonArray = array(); $sql = SELECT ID,CLIENT FROM PLD_SERVERS;
I want to capture the access_token returned by this url(below) https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_REDIRECT_URI&client_secret=YOUR_APP_SECRET&code=CODE_GENERATED_BY_FACEBOOK But if I
Just see this: SELECT clientid,clientname,startdate,enddate,age FROM clients WHERE clientid IN (1,2,3,4,5) AND CASE WHEN
I have this SQL code here.. SELECT cl.clientid, cl.clientname, cl.billingdate, cp.startdate, cp.expiration, (SELECT COUNT(*)
My XML format is this : <Main> <IsD>false</IsD> <IsN>true</IsN> <ID>826e03b3-191c-40c9-8a3d-f6607842fa5f</ID> <ClientId>c6a32c38-0398-4806-b82f-e924f96217fa</ClientId> <General> <IsD>false</IsD> <IsN>true</IsN>
I have a client table (with fields id, name) and a project table (with

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.