【问题标题】:Node : Mongoose Updating User Schema with arrays节点:Mongoose 使用数组更新用户模式
【发布时间】:2017-03-12 00:54:58
【问题描述】:

这是我的用户架构

{  
   "user_collection":[  
      {  
         "_id":"xxx",
         "name":"NAME",
         "prescription":  
            {  
               "doctor_id":  
                  [{  
                     "_id":"xxx",
                     "medicine_id":"MEDICINE_ID",
                  }]
            },
         "meal":{  
               "meal_name":{
               "start_time":"START_TIME",
               "end_time":"END_TIME"
               }  
         },
         "created_at":"CREATED_AT",
         "updted_at":"UPDATED_AT"
      }
   ]
}

注意:_id 仅供理解

我需要在处方数组中插入文档。这是条件

如果给出了新的医生id,它应该像这样添加到处方数组中

{
    "_id" : ObjectId("5813288eaa0f1231de477d92"),
    "name" : "andrew",
    "prescription" : [ 
        {
            "prescription" : [ 
                {
                    "_id" : ObjectId("58143d484e26a229873b0528"),
                    "medicine_id" : "10011241343"
                }
            ],
            "_id" : ObjectId("58143d484e26a229873b0527"),
            "doctor_id" : "5813221ace684e2b3f5f0a6d"
        }
    ]
}

如果我给出了已经存在的医生 ID,它应该添加类似

{
    "_id" : ObjectId("5813288eaa0f1231de477d92"),
    "name" : "andrew",
    "prescription" : [ 
        {
            "prescription" : [ 
                {
                    "_id" : ObjectId("58143d484e26a229873b0528"),
                    "medicine_id" : "10011241343"
                }
            ],
            "prescription" : [ 
                {
                    "_id" : ObjectId("58143d484e26a229873b0529"),
                    "medicine_id" : "10011241349"
                }
            ],
            "_id" : ObjectId("58143d484e26a229873b0527"),
            "doctor_id" : "5813221ace684e2b3f5f0a6d"
        }
    ]
}

我试过的是

dbModel.user.update({
                    _id: req.body.user_id
                }, {
                    $set: {
                        prescription:   [ { "doctor_id" : req.body.doctor_id, "prescription" : [
                        {
                            "medicine_id" : req.body.medicine_id

                        }]} ],
                    }
                }, {
                    upsert: true
                }, function(err) {

                    if (err) {
                        res.status(202).json({
                            "success": "0",
                            "message": err
                        })
                    } else {
                        res.status(200).json({
                            "success": "1",
                            "message": "Prescription given successfully"
                        });
                    }
})

我不知道如何检查医生id是否已经存在,如果不存在则添加一个新数组,如果存在则添加到现有数组中

【问题讨论】:

标签: node.js mongodb express mongoose mean-stack


【解决方案1】:

看看这个answer

但基本上你可以使用$ 运算符来标识数组中的元素。

可以看到here一些mongodb数组操作符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2021-03-23
    • 2019-12-15
    • 2020-02-24
    • 2018-07-04
    相关资源
    最近更新 更多