1

我四处寻找解决方案,但我似乎找不到

我正在尝试查看我的 nedb 数据库中是否存在某行,如果它不存在则插入一些东西,但如果它确实存在,那么就在这里移动是我尝试过的

function newAgent(pcName){
    socket.broadcast.emit('newAgent', pcName)
    agentList.find({agentName: { $nin: pcName}}, function(err, docs) {  

    agentList.insert({agentName: pcName}, function (err) {});
});

}

现在我可能正在做一些愚蠢的事情,但我是 nedb 的新手,所以我不知道该使用什么

4

1 回答 1

0

试试这样的东西怎么样

function newAgent(pcName){
    socket.broadcast.emit('newAgent', pcName)
    agentList.find({agentName: { $in: pcName}}, function(err, docs) { 
    if(null === docs){
       agentList.insert({agentName: pcName}, function (err) {});
        } else {
//since it exists you might want update
        agentList.update({
            pcName: pcName
          }, {
        $set: {
          //call fields to be updated
        }
      }, {}, callback);
    }

});

}
于 2018-01-10T09:16:11.460 回答