Javascript mongoDB node.js查找并修改问题
我正在做一个关于MongoDB的在线课程,不幸的是它有点过时了。看起来有些功能已经改变了(当然是使用1.4版,而我使用3.0版。) 以下是我遇到问题的代码,我尝试将其更新到当前版本的MongoDB: app.js 如果我通过Mongo shell运行相同的findAndModify,我会得到预期的结果(增加计数器并显示新文档),但是当我使用节点运行此操作时,它对数据库没有影响,并抛出此错误:Javascript mongoDB node.js查找并修改问题,javascript,json,node.js,mongodb,Javascript,Json,Node.js,Mongodb,我正在做一个关于MongoDB的在线课程,不幸的是它有点过时了。看起来有些功能已经改变了(当然是使用1.4版,而我使用3.0版。) 以下是我遇到问题的代码,我尝试将其更新到当前版本的MongoDB: app.js 如果我通过Mongo shell运行相同的findAndModify,我会得到预期的结果(增加计数器并显示新文档),但是当我使用节点运行此操作时,它对数据库没有影响,并抛出此错误: TypeError: Cannot call method 'findAndModify' of und
TypeError: Cannot call method 'findAndModify' of undefined
有什么建议吗?请尝试:
db.counters('counters').findAndModify
而不是:
db.collection['counters'].findAndModify
请尝试:
db.counters('counters').findAndModify
而不是:
db.collection['counters'].findAndModify
请尝试:
db.counters('counters').findAndModify
而不是:
db.collection['counters'].findAndModify
请尝试:
db.counters('counters').findAndModify
而不是:
db.collection['counters'].findAndModify
从mongodb文档:
可以使用集合打开现有集合
db.collection([[name[, options]], callback);
如果“严格模式”处于禁用状态,则会创建一个新集合(如果尚未创建)
现在
因此,您需要这样做:
db.collection('counters', function(err, collection){
collection.findAndModify({
query: {
name: 'comments'
},
update: {
$inc: {
counter: 1
}
},
new: true
}, function(err, doc) {
if (err) throw err;
if (!doc) {
console.dir('No counter found for comments.');
} else {
console.dir('Number of comments: ' + doc.counter);
}
});
});
从mongodb文档:
可以使用集合打开现有集合
db.collection([[name[, options]], callback);
如果“严格模式”处于禁用状态,则会创建一个新集合(如果尚未创建)
现在
因此,您需要这样做:
db.collection('counters', function(err, collection){
collection.findAndModify({
query: {
name: 'comments'
},
update: {
$inc: {
counter: 1
}
},
new: true
}, function(err, doc) {
if (err) throw err;
if (!doc) {
console.dir('No counter found for comments.');
} else {
console.dir('Number of comments: ' + doc.counter);
}
});
});
从mongodb文档:
可以使用集合打开现有集合
db.collection([[name[, options]], callback);
如果“严格模式”处于禁用状态,则会创建一个新集合(如果尚未创建)
现在
因此,您需要这样做:
db.collection('counters', function(err, collection){
collection.findAndModify({
query: {
name: 'comments'
},
update: {
$inc: {
counter: 1
}
},
new: true
}, function(err, doc) {
if (err) throw err;
if (!doc) {
console.dir('No counter found for comments.');
} else {
console.dir('Number of comments: ' + doc.counter);
}
});
});
从mongodb文档:
可以使用集合打开现有集合
db.collection([[name[, options]], callback);
如果“严格模式”处于禁用状态,则会创建一个新集合(如果尚未创建)
现在
因此,您需要这样做:
db.collection('counters', function(err, collection){
collection.findAndModify({
query: {
name: 'comments'
},
update: {
$inc: {
counter: 1
}
},
new: true
}, function(err, doc) {
if (err) throw err;
if (!doc) {
console.dir('No counter found for comments.');
} else {
console.dir('Number of comments: ' + doc.counter);
}
});
});
哎呀,我刚用错括号了。本应:
db.collection('counters')
而不是
db.collection['counters']
就像图格说的那样。哎呀,我只是用错了括号。本应:
db.collection('counters')
而不是
db.collection['counters']
就像图格说的那样。哎呀,我只是用错了括号。本应:
db.collection('counters')
而不是
db.collection['counters']
就像图格说的那样。哎呀,我只是用错了括号。本应:
db.collection('counters')
而不是
db.collection['counters']
就像图格说的那样。现在就用这个:
db.collection('counters').findOneAndUpdate(
{name: 'comments'}, //query
{$inc: {counter: 1}}, //update
{ //options
upsert: true, // create the doc when it's not there
returnOriginal:false // return the modified doc *(new is not supported here!)
},
function(err, r){ //callback
if(err) throw err;
console.log('counter: '+r.value.counter);
}
);
现在就用这个:
db.collection('counters').findOneAndUpdate(
{name: 'comments'}, //query
{$inc: {counter: 1}}, //update
{ //options
upsert: true, // create the doc when it's not there
returnOriginal:false // return the modified doc *(new is not supported here!)
},
function(err, r){ //callback
if(err) throw err;
console.log('counter: '+r.value.counter);
}
);
现在就用这个:
db.collection('counters').findOneAndUpdate(
{name: 'comments'}, //query
{$inc: {counter: 1}}, //update
{ //options
upsert: true, // create the doc when it's not there
returnOriginal:false // return the modified doc *(new is not supported here!)
},
function(err, r){ //callback
if(err) throw err;
console.log('counter: '+r.value.counter);
}
);
现在就用这个:
db.collection('counters').findOneAndUpdate(
{name: 'comments'}, //query
{$inc: {counter: 1}}, //update
{ //options
upsert: true, // create the doc when it's not there
returnOriginal:false // return the modified doc *(new is not supported here!)
},
function(err, r){ //callback
if(err) throw err;
console.log('counter: '+r.value.counter);
}
);