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関係は、あまり理解できていない印象があります。 また判ったら更新したいと思います。