Last active
November 30, 2016 09:41
-
-
Save justin3737/2be9dd56eddf1690c6649deb204d99b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var monoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/member3'); | |
/* 新增Add */ | |
var Product = mongoose.model('Product', { | |
title: String, | |
price: Number | |
}); | |
var item = new Product({ | |
title: '電視機', | |
price: 10000 | |
}); | |
item.save(function(err){ | |
if (err) { | |
console.log('fail'); | |
} else { | |
console.log('success'); | |
} | |
}); | |
/* 查詢query */ | |
Product.find({title: '電視機'}, function(err, products){ | |
for (var index in products) { | |
var temp = products[index]; | |
console.log(temp); | |
} | |
}); | |
/* 查詢2 query: use regex */ | |
Product.find({title: /電視/i}, function(err, products){ | |
for (var index in products) { | |
var temp = products[index]; | |
console.log(temp); | |
} | |
}); | |
/* 查詢3 query: price gte than 6000 */ | |
Product.find({ | |
price: {$gte: 6000} | |
}, function(err, products){ | |
for (var index in products) { | |
var temp = products[index]; | |
console.log(temp); | |
} | |
}); | |
/* 移除 remove */ | |
Product.remove({title: '電視機'}, function(err){ | |
if (err) { | |
console.log('remove fail'); | |
} else { | |
console.log('remove success'); | |
} | |
}); | |
/* 更新 uodate */ | |
Product.update({title: '冰箱'}, | |
{'title':'大冰箱', 'price': 20000} | |
, function(err){ | |
if (err) { | |
console.log('update fail'); | |
} else { | |
console.log('update success'); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment