MongoDB for VS code - error message = "command insert is unsupported"

Hi I am new in using MongoDB and I have a big problem:

I don’t know if I am the only one in this case but whenever I try to insert some datas in my database they always send me back this message : “command insert is unsupported”

I tried on MongoShell, or on the playground, it respond me always with this message…
I am sure I am Connected to my DB because when I make requests it respond me back…

I tried to use Insert(), InsertOne() or InsertMany().

I am using a free M0 cluster to learn, is it the problem ?

No doubt your syntax is incorrect.
Perform a sample mongosh session and cut and paste it into this topic, please.
Obscure any personal info or passwords.
Enter it between a line of ``` triple-backticks before and afterwards so it will format nicely.
Show the error as well as the commands you entered.

Thanks for your response Jack_Woehr to help a Newbie like me !

My URI is correct , because with the same connection, I can find all my datas in my collection “first”.
the problem appears when I try to insert.
I was wondering if my code was wrong, that’s why I did the “Create my Playground” on MongoDB for VS code.
I took their example they gave to me but I got the same problem

const { MongoClient } = require('mongodb');

// Set up Express middleware

router.use(express.urlencoded({ extended: true }));
router.use(express.json());

const uri = 'mongodb://....'

// Create a new MongoClient

const client = new MongoClient(uri); 

 async function addData() {
    await client.connect()
    try {
      const db = client.db('data'); // Replace with your database name
      const collection = db.collection('first'); // Replace with your collection name
      console.log(req.body)
      let task = { title: req.body.task, completed: false }
      console.log(task)
      // Insert a single document, wait for promise so we can read it back
      const push = await collection.insertOne(task)
      // Find one document
      const myDoc = await collection.findOne();
      // Print to the console
      console.log(myDoc);
      res.sendStatus(201);
    }
    catch (err) {
      console.log(err.stack);
    }

    finally {
      await client.close();
    }
  }

  addData()

Did you substitute valid names here when you used the example?

Yes , it is correct.
I can read my datas from the “first” collection in the data “data” names.

Can you provide a very simple example that fails in mongosh that I can try?

Sure, to connect to my MongoShell I type this command : “mongosh $MDB_CONNECTION_STRING;”

In my data, I just have one data name “data” with only one collection named “first”
So I Type to connect to this DB : “use data”

In this collection (“first”), I have only 2 objects, to see it I use the command : “db.first.find()”
It respond me this:

[
  {
    _id: ObjectId("aksnldnkandlsandl"),
    title: 'eat',
    completed: false
  },
  {
    _id: ObjectId("sakndklandkland"),
    title: 'sleep',
    completed: false
  }
]

So now I want to add new object in this DB I use the command : “db.first.insertOne({title:“work”, completed: false})”

The shell respond me with this error: "MongoServerError: command insert is unsupported, correlationID = "

I tried to use other methods to add new object, as “bulkWrite()”, or “insertMany()” with an array as argument, or insert, I got the same message.

and this error message I Got it from the “Create a new playground” with “MongoDB with VS code”

Do you think I have the wrong version of MongoDB?
I installed with npm install Mongodb , so the version I am using is 5.6.0 for node version.

Okay, here’s what I did.
I am not having any trouble inserting.
Am I misunderstanding your problem?

Atlas Cluster0-shard-0 [primary] test> show collections
/* no collections yet present */
Atlas Cluster0-shard-0 [primary] test> db.first.insertOne({
...     _id: ObjectId("aksnldnkandlsandl"),
...     title: 'eat',
...     completed: false
...   })
BSONError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
/* In other words, your string for an ObjectId is not valid */
/* So I'll let MongoDB choose an object id for me instead of assigning one */
Atlas Cluster0-shard-0 [primary] test> db.first.insertOne({title: 'eat', completed: false })
{
  acknowledged: true,
  insertedId: ObjectId("64a4a913f8a38313d2807502")
}
Atlas Cluster0-shard-0 [primary] test> db.first.insertOne({title: 'sleep', completed: false})
{
  acknowledged: true,
  insertedId: ObjectId("64a4a951f8a38313d2807503")
}
Atlas Cluster0-shard-0 [primary] test> db.first.find()
[
  {
    _id: ObjectId("64a4a913f8a38313d2807502"),
    title: 'eat',
    completed: false
  },
  {
    _id: ObjectId("64a4a951f8a38313d2807503"),
    title: 'sleep',
    completed: false
  }
]
Atlas Cluster0-shard-0 [primary] test> 
2 Likes

BTW in this line from your post above, the double quote marks in that string are “smart quotes” not standard quote marks. When I pasted in that line just now, I had to change them to standard quote marks.

I don’t think you miss something. You understand my problems.

For the _id I changed it on purpose to hide it.
When I do the InsertOne, let MongoDB to chose the Object ID.

In your case it looks like it works very well.
I tried to change the double to single quote it but still not working…

The only thing I see different is your MongoShell is on “Atlas Cluster0-shard-0 [primary]”
and mine is “AtlasDataFederation”, is there any impact ?

I checked in my Data API I am able to Read and Write, also I checked my User account if I can change it.

I sincerely don’t know why I cannot do the insert… Why this method is Unsupported?

All I can guess is it’s some sort of authority / role problem.

I checked. For me I have all authority on this data but still not working.
If I remembered correctly the error message would be different if I have some authorities restrictions. It just like my Mongosh doesn’t support the method but I don’t know why…

I checked the version I am using on MongoShell, it is only mongosh : 1.10.1.
Maybe this is too old ?

1.10.1 is the same version I am using.

Hi All,

The error message MongoServerError: command insert is unsupported appears to be originating from the MongoDB Data Federation instance because the operation was unsupported. The list of supported operations on a Federated Instance are available at the documentation page Query and Write operation commands.

Try creating and connecting to a free tier cluster to attempt the insert commands (instead of connecting directly to a data federation instance).

For reference, please see the following example from my test environment in which the insertOne() command being attempted on a Data Federation Instance generates the same error:

AtlasDataFederation test> use db
switched to db db
AtlasDataFederation db> db.collection.insertOne({title:"test"})
MongoServerError: command insert is unsupported, correlationID = 176edbca56c01e28efce9c8e
AtlasDataFederation db>

I suspect you’ll have similar output to Jack’s example provided earlier if attempting on an M0 tier cluster.

Hope this helps.

Regards,
Jason

1 Like

Hi Jason,

You are right in your arguments , I changed my DB connection and it hit to my M0-cluster.
After then it works well!
So my problem was my connection string who hit the wrong path.

I am very grateful to Jack Woehr and Jason Tran for your quick responses.
Best regards,

J N.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.