2016-08-04 102 views
0

如果字段存在,我想更新嵌入数组,否则使用指定的字段创建新文档。如果它存在,则更新文档中的嵌入数组,否则创建一个新文档MongoDB NodeJS

tldr:我有以下更新查询,只更新第一个文档中的嵌入数组。例如:如果第一个文档的嵌入式数组“设备”中不存在mac_address,则不会查看其他文档。

要设置/更新设备的状态,我有更新查询并插入新数据(如果它不存在;我正在通过更新函数中的if(!result.nModified)检查条件。

当我运行此代码时,它只会在mac_address的第一个文档中查找(如果存在),它会更新...是的,它可以工作!

如果它不存在 - >它会创建一个新文档。是的,它的作品。

然后,如果是检查新文档中最后插入的mac_address,它将重新插入它并形成重复项。

更新函数只适用于第一个文档,它会跳过其余部分。

任何帮助将非常感激。我一直在寻找最近一周左右的解决方案。尝试了所有可能的解决方案;但它并不像我希望的那样按照规定行事。

Mac和状态是此代码之前定义的变量:

db.devices.update({ 
     'devices.mac_address': mac 
    }, { 
     $set: { 
     'devices.$.state': state 
     } 
    }, 
    function(err, result) { 
     if (err) { 
     console.log('error: ' + err); 
     } 
     console.log('result-->' + JSON.stringify(result)); 

     if (!result.nModified) { 
     db.devices.insert({ 
      name: 'undefined', 
      'devices': [{ 
       '_id': new mongojs.ObjectID(), 
       'name': 'undefined', 
       'type': 'undefined', 
       'state': state, 
       'ip_address': ip_address, 
       'mac_address': mac, 
       'update_timestamp': new Date(), 
       'power': [{ 
       'value': '22', 
       'time': new Date() 
       }] 
      }] 
      }, 
      function(err, result) { 
      if (err) { 
       console.log('error: ' + err); 
      } 
      console.log('result-->' + result); 
      }); 
     } 
    }); 

这是我的JSON文件。

[ 
    { 
     "_id": "579a8116077f4a6fc78188c8", 
     "name": "Bedroom", 
     "devices": [ 
     { 
      "_id": "579a82ebe3648480708be146", 
      "name": "Smart Switch", 
      "type": "switch", 
      "state": "on", 
      "ip_address": "192.168.4.22", 
      "mac_address": "5c:cf:7f:03:35:ab", 
      "update_timestamp": "2016-07-28T22:10:51.957Z", 
      "power": [ 
       { 
        "value": "5000", 
        "time": "2016-07-28T22:10:51.957Z" 
       } 
      ] 
     }, 
    { 
     "_id": "57a2e0b1a6fdb70d35e95dfb", 
     "name": "Living Room", 
     "devices": [ 
     { 
      "_id": "57a2e0b1a6fdb70d35e95dfa", 
      "name": "Ceiling Fan", 
      "type": "switch", 
      "state": "disconnected", 
      "ip_address": "142.58.184.155", 
      "mac_address": "5c:cf:7f:03:35:aa", 
      "update_timestamp": "2016-08-04T06:29:05.247Z", 
      "power": [ 
       { 
        "value": "120", 
        "time": "2016-08-04T06:29:05.247Z" 
       } 
      ] 
     } 
     ] 
    } 
] 

回答

0

所以我最终最终解决了我的问题,并且我发布了我的解决方案给任何可能有类似或接近我所遇到的问题的人。

我使用的if条件都是错的。我正在用!检查'nModified'。我不得不用nModified === 0来替换它,因为当没有东西需要更新时它返回0,它告诉我们这是一个新记录。

下面是修改和更新工作代码:

db.test.update({ 
     'devices.mac_address': mac 
    }, { 
     $set: { 
     'devices.$.state': state, 
     'devices.$.update_timestamp': new Date(), 
     }, 
    $push: {'devices.$.power': {'value': power, 'time': new Date()}} 
    }, { 
     upsert: false 
    }, 
    function(err, result) { 
     if (err) { 
     console.log('error: ' + err); 
     } 

     console.log('Updated Existing Device: ' + JSON.stringify(result)); 
     if (result.nModified === 0) { 
     console.log('NEW DEVICE!!'); 

      db.test.insert({ 
       'room_name': 'Unassigned', 
       'devices': [{ 
       'name': 'Unassigned', 
       'type': 'Unassigned', 
       'state': state, 
       'ip_address': ip_address, 
       'mac_address': mac, 
       'update_timestamp': new Date(), 
       'power': [] 
       }] 

      }, 
      function(err, result) { 
       if (err) { 
       console.log('error: ' + err); 
       } 
       console.log('NEW DEVICE ADDED RESULTS: ' + JSON.stringify(result)); 
      }); 
     } 
    }); 
相关问题