繁体   English   中英

如何将新项目添加到 json 并插入到 mongodb

[英]How to add new items to json and insert to mongodb

can someone help me add new items to a json object and the insert the new result to mongoDB, here is my json object

{
  image: "https://cdn-prd-02.com/sys-master/images/h1c/h3c/9351894368286/silo-6003901000319-front-335548_140Wx140H"
  name: "Boss Mature Captains Chicken 775g x 6"
  new_price: "R10794"
  old_price: "R13699"
},
{
  image: "https://cdn-prd-02.com/sys-master/images/h27/h17/9623589617694/silo-product-image-v2-01Nov2019-180040-6001889008532-front-382785-2_140Wx140H"
  name: "Ideas Double Hot Plate"
  new_price: "R11900"
  old_price: "R22900"
},
{
  image: "https://cdn-prd-02.com/sys-master/images/h94/h83/9529131663390/silo-product-image-v2-02Jul2019-180102-6002322013991-front-360000-24_140Wx140H"
  name: "Russell Hobbs Supremeglide Steam Iron"
  new_price: "R24900"
  old_price: "R25900"
},
{
  image: "https://cdn-prd-02.com/sys-master/images/h99/hd6/9258690871326/silo-6009523601514-front-312470_140Wx140H"
  name: "Purity Aqueous Cream Goodnight 325ml"
  new_price: "R2199"
  old_price: "R3299"
},
{
  image: "https://cdn-prd-02.com/sys-master/images/he4/hf2/9823222464542/silo-product-image-v2-26May2020-180139-6009523601460-up-426623-1762_140Wx140H"
  name: "Purity Baby Aqueous Cream 325ml"
  new_price: "R2199"
  old_price: "R3299"
},
{
  image: "https://cdn-prd-02.com/sys-master/images/h94/hc2/9732176904222/silo-product-image-v2-21Mar2020-180150-6009708050465-up-416097-1344_140Wx140H"
  name: "SCORE ENERGY DRINK ORIGINAL 500ML"
  new_price: "R999"
  old_price: "R1099"
},
{
  image: "https://cdn-prd-02.com/sys-master/images/h45/hbb/9195891654686/silo-4971850090250-front-280121_140Wx140H"
  name: "Casio Advanced Scientific Calculator"
  new_price: "R38400"
  old_price: "R54900"
}

所以我基本上想在 old_price 之后添加一个名为 sale_type:“reduced”的新项目,然后插入 mongodb。 我目前正在使用

var user = new Parse.User();
user.set("name", $scope.Fullname);

为数据库设置另一个信息。

将新项目 sale_type 添加到 json object 您可以使用

for(i=0;i<user.length;i++)
{
user[i].sale_type="reduced"
}

插入 mongodb 使用

var client = require('mongodb').MongoClient;
var url = "";

client.connect(url, function(err, db) {
  if (err) throw err;
  var db = db.db("mydb");
  col = db.collection('myCol');
for(i=0;i<user.length;i++)
{
  col.insertOne(user[i], function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");

  });
}
  db.close();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM