mongo-dart / mongo_dart

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

While connecting I get null and when I try to insert gives error {ok: 0, errmsg: getLastError not supported for new-style commands in atlas free tier., code: 8000, codeName: AtlasError} #238

Closed balasubramanian1612s closed 3 years ago

balasubramanian1612s commented 3 years ago
openConnection() async {
    var db = await mongo.Db.create(
        "mongodb+srv://<username>:<password>@cluster0.evuzl.mongodb.net/addres?retryWrites=true&w=majority");
    await db.open().then((value) {
      print(value);
    });
    var coll = db.collection('clients');
    await coll.insert({
      "_id": {"\$oid": "55cba2476c522cafdb053ade"},
      "location": {
        "coordinates": [-73.961704, 40.662942],
        "type": "Point"
      },
      "name": "Wendy'S"
    }).then((value) {
      print(value);
    });
  }

Same URL works fine in my NodeJS app.

Thanks for reply.

balasubramanian1612s commented 3 years ago

I/flutter (29224): null I/flutter (29224): {ok: 0, errmsg: getLastError not supported for new-style commands in atlas free tier., code: 8000, codeName: AtlasError}

Two print statements results like this.

balasubramanian1612s commented 3 years ago

Just now tried creating one new mongo account. Still, I get the same ERROR. Pls, help.

giorgiofran commented 3 years ago

The first null you receive is because db.open does not return any value. The error seems related to the fact that Atlas requires the OP_MSG methods. Try usinginsertOne instead of insert. Please, note that eJson is not supported in mongo_dart (use the ObjectId class instead of {"\$oid": "55cba2476c522cafdb053ade"})

balasubramanian1612s commented 3 years ago

Thank you, it worked.