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()
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>
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 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 ?
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.
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.
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,