如何将数据插入mongodb正确方法

我试图将数据插入mongodb,但我不知道如何插入数据。我已经尝试过下面的代码,但是它不起作用。将数据插入mongodb的正确方法是什么? 这是我的数据:

db.trade.insert([
   {
      "p_id":"ot1",
      "product_name":"Mixed",
      "product_weight":"1kg",
      "product_price":550,
      "product_image":"rose.jpg"
   },
   {
      "p_id":"ot2",
      "product_name":"Mixed",
      "product_weight":"1kg",
      "product_price":550,
      "product_image":"rose.jpg"
   },
   {
      "p_id":"ot3",
      "product_name":"Mixed",
      "product_weight":"1kg",
      "product_price":550,
      "product_image":"rose.jpg"
   }
]);
评论
  • 顏小朵o
    顏小朵o 回复

    the insert() function takes a single document, while insertMany() takes an array of documents.

    In your code, you're attempting to pass an array (multiple documents) into insert() (which expects a single document).

    You could either make multiple calls to insert() or a single call to insertMany().