0

This might be a niche question since Stitch is rather new, but I'll try anyway!

I have a Stitch app, with the MongoDB Connection String option enabled on the underlying Atlas cluster.

When trying to run this line in my code

const r = await collection.findOneAndDelete(filter);

I get the following error:

MongoError: unknown command findAndModify
    at Connection.<anonymous> (node_modules/mongodb-core/lib/connection/pool.js:443:61)
    at processMessage (node_modules/mongodb-core/lib/connection/connection.js:364:10)
    at TLSSocket.<anonymous> (node_modules/mongodb-core/lib/connection/connection.js:533:15)

However, if I connect directly to the underlying Atlas cluster, this same code works fine.

I'm using MongoDB driver for NodeJS version 3.2.7

Did anyone encounter this, or can otherwise shed some light?

Juvaly
  • 252
  • 1
  • 17
  • If I'm remembering correctly, `findAndModify` is an older command that got replaced by `updateMany`. I believe `findOneAndDelete` got replaced with `removeOne`. – klhr Jul 15 '19 at 14:09
  • Using deleteOne works, but it doesn't return the deleted id, so not quite the same. Seems like something is off with the Stitch implementation of the MongoDB wire protocol, since findOne also returns an error message: error processing OP_MSG as CRUD request: invalid find command: "batchSize" field in find command unsupported. I'm not the one passing batchSize :) – Juvaly Jul 15 '19 at 15:33

1 Answers1

0

Stitch app, with the MongoDB Connection String option enabled

Currently the findAndModify operation from Stitch Connection String (Wire Protocol) is not exposed.

What happened in this case is MongoDB Node.JS driver v3.2 rewrites findOneAndXXX operations and use findAndModify command, however the command is not exposed/supported by the Stitch backend (yet).

In March 2019, MongoDB Stitch SDKs support db.collection.findAndXXX() function. There are three official Javascript/TypeScript SDKs for Stitch that you can use:

For example, you can utilise mongodb-stitch-server-sdk (via Node.JS) to call findOneAndDelete()

Alternatively, as you have discovered, you can connect to the Atlas cluster directly and utilise MongoDB Node.JS driver findOneAndDelete

Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • This answer ignores major details described in my question. I mentioned I am using the findOneAndDelete method. It is only the error that mentions findAndModify. Also I am using the native NodeJS MongoDB driver to connect, and not the Stitch SDK. By the way, this was eventually opened as a bug for the Atlas dev team, which will be solved sometime in the future (or not). – Juvaly Sep 04 '19 at 05:16
  • Hi @Juvaly apologies for misreading your question, I have updated the answer accordingly. – Wan B. Sep 04 '19 at 06:44