mongo-dart / mongo_dart

Mongo_dart: MongoDB driver for Dart programming language
https://pub.dev/packages/mongo_dart
MIT License
446 stars 98 forks source link

type 'ObjectId' is not a subtype of type 'String' of 'value' #289

Closed Zopenzop closed 1 year ago

Zopenzop commented 2 years ago

I am trying to insert a document into my Mongo Database using this package.

Here's my code -

  var db = await Db.create(MONGO_CONN_STRING);
  await db.open();
  var coll = db.collection('reports');
  await coll.insertOne({
    "username": "Tom",
    "action": "test",
    "dateTime": "today",
  });

I get a runtime error on line 4

Unhandled Exception: type 'ObjectId' is not a subtype of type 'String' of 'value'

Is it an issue with the package or is something wrong with my code?

giorgiofran commented 2 years ago

The code seems to be correct, I tried in my environment and it works. From the message error it seems that you are supplying an ObjectId value when the system is expecting a String. As you have no ObjectIds in your document, I guess it could only be the "_id" field. If you not specify it, the driver assumes an ObjectId value by default. MongoDb allows to use "_id" fields of different types in the same document, so my suspect is that you have in place a schema that is restricting the "_id" field to be a String. Let me know if you got the problem.

giorgiofran commented 1 year ago

I close this issue, feel free to reopen it if needed.

Zopenzop commented 1 year ago

Tested the same piece of code after a long time and surprisingly, it's working just fine now! Without a single change!