搜尋

首頁  >  問答  >  主體

node.js - mongoose中数据不存在则插入怎么写

添加一组数据有个user字段不允许重复,要检查没此字段时插入,有的话就跳过。
这段要怎么写呢?
翻find()用法好像没看到,能在没找到自动执行的方式。

阿神阿神2873 天前362

全部回覆(2)我來回復

  • ringa_lee

    ringa_lee2017-04-17 15:40:36

    使用findOneAndUpdate方法,第三個參數裡面的upsert設為true。

    UserModel.findOneAndUpdate({_id: "asdasdadasd"}, req.newData, {upsert:true}, function(err, doc){});

    http://stackoverflow.com/ques...
    http://mongoosejs.com/docs/ap...

    回覆
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:40:36

    雷雷

    回覆
    0
  • 取消回覆