Error when inserting a document into MongoDB via Node.js

前端 未结 1 605
误落风尘
误落风尘 2020-12-17 15:16

I\'m trying to insert a document into MongoDB in Node.js. I have read from the DB successfully, and added documents via the command-line interface. However, I can\'t insert

相关标签:
1条回答
  • 2020-12-17 15:51

    You need to provide a callback function to your insert call so that the method can communicate any errors that occur during the insert back to you.

    db.collection("test").insert({ str: "foobar" }, function (err, inserted) {
        // check err...
    });
    

    Or, if you truly don't care whether the insert succeeds or not, pass a write-concern value of 0 in the options parameter:

    db.collection("test").insert({ str: "foobar" }, { w: 0 });
    
    0 讨论(0)
提交回复
热议问题