updateOne with App Services API question

Have been using HTTPS endpoints to connect to a serverless instance. Read, write and delete work great but having trouble with updates. The following is my function for updateOne:

exports = function({ query, headers, body}, response) {
    const result = context.services
    .get('mongodb-atlas')
    .db('gymlog')
    .collection('exercises')
    .updateOne({});
  

    return  "Updated!";
     };

then used post to send the assigned post url with the following body from Postman:

  { 
    "query": {"id":"65a93d47a040369f2c8de4f5"},
     "update": {"exname": "Test update"}
    }

get this res error back:

  "error": "uncaught promise rejection: update: 2 args (query and update modifier) 
      required",
    "error_code": "UncaughtPromiseRejection"

does anyone have this working? I have tried many body arrays from the Atlas documents but can’t get this to work. the above was my last attempt prior to seeking help.

Hi @Louis_Chiodo and welcome to the Community Forums.

I believe the solution is in the update syntax. You must use an operator such as $set. Updating your code would look something like this:

{ 
    "query": {"id":"65a93d47a040369f2c8de4f5"},
    "update": {
       $set:{
         "exname": "Test update"
       }
    }
}

Let me know if it worked or if I can help with anything else!

Best!

Thanks for the suggestion. I tried the following, with and without " " around $set and still got the error.

{ 
    "query": {"id":"65a93d47a040369f2c8de4f5"},
     "update": {
        $set:{
            "exname": "Test update"
            }
        }
}

“error”: “uncaught promise rejection: update: 2 args (query and update modifier) required”,

Lou

Hi @Louis_Chiodo, sorry for the “trial and error”, but I don’t have any environment set up where I can quickly test this.

Here is a link to the documentation (I believe you may have already seen it). https://www.mongodb.com/docs/atlas/app-services/functions/mongodb/api/#collection.updateone--

However, based on it, I believe the syntax would be something like:

exports = function({ query, headers, body}, response) {
    const query = { "id":"65a93d47a040369f2c8de4f5" };
    const update = { 
        "$set": {
            "exname": "Test update"
        }
    }
    const result = context.services
    .get('mongodb-atlas')
    .db('gymlog')
    .collection('exercises')
    .updateOne({query, update});
  

    return  "Updated!";
};

Again, let me know if it works.

Best!

Thanks for your time still struggling with this but wanted to circle back . Using the VS code mongodb playground the following update a document works:

db.exercises.updateOne( {"_id": ObjectId("65aea14fd9b9d336f59f0e6b")},{"$set": {"exname": "Lat Pulldown - narrow grip"}})

Using Postman to call my HTTPS endpoint for updateOne is still failing. My endpoint query is:

exports = function({ query, headers, body}, response) {
    const result = context.services
    .get('mongodb-atlas')
    .db('gymlog')
    .collection('exercises')
    .updateOne();
  

    return  "Updated!";
};

The body in Postman is:

{"_id": ObjectId("65aea14fd9b9d336f59f0e6b")},{"$set": {"exname": "Lat Pulldown - narrow grip"}}

But I still get the following error response:

  "error": "uncaught promise rejection: update: 2 args (query and update modifier) required",
    "error_code": "UncaughtPromiseRejection",

Very frustrating.

I do feel the pain.

Most likely the issue is with:

ObjectId is a function available in the node driver. Give it a try with

{ "_id" : { "$oid" : "65aea14fd9b9d336f59f0e6b" } }

rather than

I suspect that

did not work is either because the field is _id rather than id and that it is a string while your field is an object id. Type and value must match.

1 Like

Well observed @steevej … I hope @Louis_Chiodo can test again this way!

Thank you!