node.js - why mongoose doesn't change mongodb -
my mongdb started by: sudo mongod --port 27111
but mongoose code() doesnt work on mongodb, nothing in mongodb changed mongo shell point of view.
$mongo 127.0.0.1:27111/foo -- insert -- mongodb shell version: 2.4.9 connecting to: 127.0.0.1:27111/foo
show dbs local 0.078125gb show dbs local 0.078125gb
var mongoose = require('mongoose'); var db = mongoose.createconnection('mongodb://127.0.0.1:27111/foo'); var schema = mongoose.schema; var tasks = new schema({ project: string, description: string, }); mongoose.model('task', tasks); var task =mongoose.model('task'); var task = new task(); task.project = 'bikeshed'; task.description = 'paint bideshed red.'; task.save(function(err){ //save if(err) throw err; console.log('task saved'); }); //搜索文档 task.find({"project": 'bikeshed'}, function(err, tasks) { (var = 0 ; < task.length ; i++) { console.log('--------------id:' + tasks[i]._id); } }); mongoose.disconnect();
instead of mongoose.createconnection
, use mongoose.connect
var db = mongoose.connect('mongodb://127.0.0.1:27111/foo');
Comments
Post a Comment