如何通过猫鼬将多个数组元素推入mongod

时间:2020-04-20 10:44:18

标签: javascript arrays mongodb typescript mongoose

有一个mongodb模式,其中包含这些字段,其类型为数组

......
orderlist: [
    {
      id: String,
      price: Number,
      photo: String,
      name: String,
      num: Number
    }
  ]
......

前端将诸如此类的数据发布给我,这个数组有很多数组元素

goodslist:[
  {
    goodsid: '10001',
    goodsprice: 20,
    goodsphoto: '/goodsimg/upload_1843.jpg',
    goodsname: 'goods1',
    goodsnum: 2
  },
  {
    goodsid: '10002',
    goodsprice: 30,
    goodsphoto: '/goodsimg/upload_1845.jpg',
    goodsname: 'goods2',
    goodsnum: 4
  },
........(etc)
]

我该怎么做,用猫鼬在不更改mongodb字段的情况下将“好清单”数据推送到“订单列表”字段中,谢谢

2 个答案:

答案 0 :(得分:0)

您必须使用mongoose virtuals来解决此问题。

您的架构必须这样:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const collectionName = 'orderlist';

const OrderSchema = new Schema({
  id: String,
  price: Number,
  photo: String,
  name: String,
  num: Number
}, { minimize: false });

const OrderlistSchema = new Schema({
  orderList: [OrderSchema]
}, { minimize: false, toJSON: { virtuals: true } });

OrderlistSchema.virtual('goodslist').
  get(function () {
    return this.orderList.map(order => ({
      goodsid: order.id,
      goodsprice: order.price,
      goodsphoto: order.photo,
      goodsname: order.name,
      goodsnum: order.num
    }))
  }).
  set(function (v) {
    this.set({
      orderList: v.map(good => ({
        id: good.goodsid,
        price: good.goodsprice,
        photo: good.goodsphoto,
        name: good.goodsname,
        num: good.goodsnum
      }))
    });
  });

module.exports = mongoose.model('Orderlist', OrderlistSchema, collectionName);

goodslist是此处的虚拟字段。 使用此架构,您可以使用格式设置订单字段,而无需更改mongodb中的任何内容。 示例过帐文档:

{
  "goodslist": [
    {
      "goodsid": 2,
      "goodsprice": 200,
      "goodsphoto": "photo2",
      "goodsname": "name2",
      "goodsnum": 1234
    }
  ]
}

您还可以获取商品清单格式的订单数据

{
    "_id": "5e9d8c0e27c7a813840c9ff0",
    "orderList": [
        {
            "_id": "5e9d8c0e27c7a813840c9ff1",
            "id": "2",
            "price": 200,
            "photo": "photo2",
            "name": "name2",
            "num": 1234
        }
    ],
    "__v": 0,
    "goodslist": [
        {
            "goodsid": "2",
            "goodsprice": 200,
            "goodsphoto": "photo2",
            "goodsname": "name2",
            "goodsnum": 1234
        }
    ],
    "id": "5e9d8c0e27c7a813840c9ff0"
}

答案 1 :(得分:0)

它将类似于以下内容:

//Update order | create if does not exist
                        orderDB.updateOne({ _id: 'xxxx' }, {

                            //Push the list into order array
                            $push: {
                                orderlist: [{
                                    id: goodlist[0][0],
                                    price: goodlist[0][1],
                                    photo: goodlist[0][2],
                                    name: goodlist[0][3],
                                    num: goodlist[0][4],
                                }]
                            }

                            //Upsert => update / create
                        }, { upsert: true })

但是,您可能需要遍历商品清单。