skip to Main Content

I am trying to insert a document into my Mongo Database using the package mongo_dart

  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",
  });

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?

3

Answers


  1. try this one

    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",
      }).toList();
    
    Login or Signup to reply.
  2. Are you using the latest version of the package?

    I think your problem is related to this issue in the GitHub Repo of the package but it is closed at Jul 25, 2015.

    Login or Signup to reply.
  3. The error is arising because you declared variables db and (collection name) outside the static connect() async function; declare them inside the function.

      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",
      });
    
    
    static connect() async {
    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",
      });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search