LoginSignup
2
0

More than 5 years have passed since last update.

初心者がmongoose公式 勉強するメモ Document 編

Posted at

mongoose Document

今回のお題はこちらです。

http://mongoosejs.com/docs/documents.html

  • 英語ですので、chromeの翻訳を使って下さい。
  • 検証結果は、掲載しませんので、実際確認してみてください。

  • document.js

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

const Schema = new mongoose.Schema({ name: 'String', size: 'String'});
const Tank = mongoose.model('Tank', Schema);

const id = "5a795cdc536527304721283a"

// 更新されず
// Tank.findById(id, function ( err, tank ) {
//   if (err) return console.log(err);
//   tank.size = 'small' ;
//   tank.save( function ( err, updatedTank ) {
//     if (err) return handleError(err);
//     // res.send(updatedTank);
//     console.log(updatedTank)
//   });
//   mongoose.disconnect();
// });

// 更新されず
// Tank.findById(id, function ( err, tank ) {
//   if (err) return handleError(err);
//   tank.set({ size: 'small' });
//   tank.save( function ( err, updatedTank ) { 
//     if (err) return handleError(err);
//     console.log(updatedTank);
//   });
//   mongoose.disconnect();
// });


// Tank.update({_id: id}, { $set: {size: 'small'}}, (err) => {
//   if (err) console.error(err);
//   console.log('update')
//   mongoose.disconnect();
// })


// Tank.findByIdAndUpdate(id, { $set: { size: 'small' }}, { new : true }, ( err, tank ) => {
//   if (err) return handleError(err);
//   // res.send(tank);
//   console.log(tank)
//   mongoose.disconnect();
// });

// 上書き未検証
// Tank.findById(id, function ( err, tank ) {
//   if (err) return handleError(err);
//   otherTank.set(tank);
// }); 

  • findById は、二個とも表示はされますが、updateがうまくいかないです。
    • なにか、抜けているかもしれないですが今のところ不明です。
    • Question 03
  • update OK
  • findByIdAndUpdate OK

  • 今回は、ここまです。

  • updage関係は、あまり理解できていない印象があります。 また判ったら更新したいと思います。

2
0
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
2
0