更新嵌套数组猫鼬 [英] Updating Nested Array Mongoose

查看:98
本文介绍了更新嵌套数组猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个快速js应用程序,需要更新嵌套数组. 1)架构:

I am working on an express js application where I need to update a nested array. 1) Schema :

//Creating a mongoose schema
var userSchema = mongoose.Schema({
_id: {type: String, required:true},
name: String,
sensors: [{
    sensor_name: {type: String, required:true},
    measurements: [{time: String}]
}] });

2) 这是代码段,解释如下:

2) Here is the code snippet and explanation is below:

router.route('/sensors_update/:_id/:sensor_name/')
.post(function (req, res) {
User.findOneAndUpdate({_id:req.body._id}, {$push: {"sensors" : 
{"sensor_name" : req.body.sensor_name , "measurements.0.time": req.body.time } } },
{new:true},function(err, newSensor) {
if (err)
res.send(err);
res.send(newSensor)
}); });

我能够使用带推技术的findOneAndUpdate成功地将值更新到测量数组,但是当我尝试向传感器数组添加多个测量值时我失败了.

I am able to successfully update a value to the measurements array using the findOneAndUpdate with push technique but I'm failing when I try to add multiple measurements to the sensors array.

这是我将第二个测量值发布到传感器数组时得到的当前json:

Here is current json I get if I get when I post a second measurement to the sensors array :

{
"_id": "Manasa",
"name": "Manasa Sub",
"__v": 0,
"sensors": [
{
  "sensor_name": "ras",
  "_id": "57da0a4bf3884d1fb2234c74",
  "measurements": [
    {
      "time": "8:00"
    }
  ]
},
{
  "sensor_name": "ras",
  "_id": "57da0a68f3884d1fb2234c75",
  "measurements": [
    {
      "time": "9:00"
    }
  ]
  }]} 

但是我想要的正确格式是使用这样的传感器阵列发布多个测量值:

But the right format I want is posting multiple measurements with the sensors array like this :

正确的JSON格式为:

Right JSON format would be :

 {
"_id" : "Manasa",
"name" : "Manasa Sub",
"sensors" : [ 
    {
        "sensor_name" : "ras",
        "_id" : ObjectId("57da0a4bf3884d1fb2234c74"),
        "measurements" : [ 
            {
                "time" : "8:00"
            }
        ],
        "measurements" : [ 
            {
                "time" : "9:00"
            }
        ]
    }],
"__v" : 0 }

请对此提出一些建议.预先感谢.

Please suggest some ideas regarding this. Thanks in advance.

推荐答案

您可能需要重新考虑数据模型.就目前而言,您无法完成所需的工作.传感器字段是指数组.在您提供的理想文档格式中,该数组中只有一个对象.然后在该对象内部,您将拥有两个具有完全相同的键的字段.在这种情况下,在JSON对象或mongo文档中,同一对象内不能有重复的键.

You might want to rethink your data model. As it is currently, you cannot accomplish what you want. The sensors field refers to an array. In the ideal document format that you have provided, you have a single object inside that array. Then inside that object, you have two fields with the exact same key. In a JSON object, or mongo document in this context, you can't have duplicate keys within the same object.

目前尚不清楚您在这里寻找什么,但是也许最好是选择以下内容:

It's not clear exactly what you're looking for here, but perhaps it would be best to go for something like this:

{
"_id" : "Manasa",
"name" : "Manasa Sub",
"sensors" : [ 
    {
    "sensor_name" : "ras",
    "_id" : ObjectId("57da0a4bf3884d1fb2234c74"),
    "measurements" : [ 
        {
            "time" : "8:00"
        },
        {
            "time" : "9:00"
        }
    ]
},
{
    // next sensor in the sensors array with similar format
    "_id": "",
    "name": "",
    "measurements": []
}],
}

如果这是您想要的,则可以尝试以下操作:

If this is what you want, then you can try this:

User.findOneAndUpdate(
    {  _id:req.body._id "sensors.sensor_name": req.body.sensor_name },
    { $push: { "sensors.0.measurements": { "time": req.body.time } } }
);

另外,如果只想在Measurements数组的每个对象中存储一个字符串,则可能只想存储实际值而不是整个对象{ time: "value" }.您可能会发现以这种方式处理数据更容易.

And as a side note, if you're only ever going to store a single string in each object in the measurements array, you might want to just store the actual values instead of the whole object { time: "value" }. You might find the data easier to handle this way.

希望这会有所帮助.

这篇关于更新嵌套数组猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆