LoginSignup
14
12

More than 5 years have passed since last update.

mongooseでupsert(レコードあればupdate、なければinsert)

Posted at

最初に触れたのがmonkだったのでmonk使ってましたがupsertが分からずmongooseデビューしました。。

〜スキーマ定義などなど〜

article = mongoose.model('article', schema);
mongoose.connect('mongodb://localhost:27017/hogetable', function(err) {});

article.update({id: XXX}, item, {upsert: true}, function(err) {});

※引数1:updateするレコードの条件
※引数2:更新or挿入するレコード
※引数3:{upsert: true}の指定をする!
※引数4:コールバック

mongooseならこれでいけます。

14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12