【发布时间】:2014-03-05 09:08:22
【问题描述】:
我使用 mongo-migrate 来配置我的数据库内容,创建集合和文档等等。
现在,我正在尝试在 down 方法中删除所有创建的文档,我对 mongodb、mongoose、mongo-migrate 还是个新手。我按照那里的说明进行操作:http://docs.mongodb.org/manual/tutorial/query-documents/(或部分)
源代码:
var mongodb = require('mongodb');
exports.up = function(db, next){
var documentName = 'category';
var collection = mongodb.Collection(db, documentName);
collection.insert({
code: 'a',
name: 'languageStatus'
}, function(error, data){
console.log(error ? error : documentName + ': ' + JSON.stringify(data));
});
collection.insert({
code: 'b',
name: 'accessName'
}, function(error, data){
console.log(error ? error : documentName + ': ' + JSON.stringify(data));
});
collection.insert({
code: 'c',
name: 'roleName'
}, function(error, data){
console.log(error ? error : documentName + ': ' + JSON.stringify(data));
});
collection.insert({
code: 'd',
name: 'translationStatus'
}, function(error, data){
console.log(error ? error : documentName + ': ' + JSON.stringify(data));
});
next();
};
exports.down = function(db, next){
var documentName = 'category';
var document = mongodb.Collection(db, documentName);
var query = {
$or: [
{name: 'languageStatus'},
{name: 'accessName'},
{name: 'roleName'},
{name: 'translationStatus'}
]
};
document.findAndModify(query, [], {}, { remove: true }, function(error, data){
console.log(error ? error : 'Deleted: ' + documentName + ': ' + JSON.stringify(data));
});
next();
};
在控制台中我得到:
向上:
category: [{"code":"a","name":"languageStatus","_id":"52f535d71a60eedc7c1685d4"}]
category: [{"code":"b","name":"accessName","_id":"52f535d71a60eedc7c1685d5"}]
category: [{"code":"c","name":"roleName","_id":"52f535d71a60eedc7c1685d6"}]
category: [{"code":"d","name":"translationStatus","_id":"52f535d71a60eedc7c1685d7"}]
向下:
Deleted: category: {"code":"a","name":"languageStatus","_id":"52f535d71a60eedc7c1685d4"}
我的down 查询出了什么问题?
PS:我只是想到了findAndModify...嗯,我猜它只返回第一个结果,有道理。但是我该怎么做才能删除这些文件?刚刚找到remove方法但是没有回调。
编辑:
当然是因为findAndModify。所以我更改了源代码,但我遇到了另一个我不明白的问题:
exports.down = function(db, next){
var documentName = 'category';
var document = mongodb.Collection(db, documentName);
var query = {
$or: [
{name: 'languageStatus'},
{name: 'accessName'},
{name: 'roleName'},
{name: 'translationStatus'}
]
};
document.find(query, function(error, data){
data.each(function(error, data){
console.log(data)
document.remove(data, function(error, number){
console.log(error ? error : documentName + ': (' + number + ') ' + JSON.stringify(data));
})
});
});
next();
};
控制台:
down : migrations/0010-init_category_table.js
{ code: 'a',
name: 'languageStatus',
_id: 52f53e2d3f852ed04d785e4c }
{ code: 'b', name: 'accessName', _id: 52f53e2d3f852ed04d785e4d }
{ code: 'c', name: 'roleName', _id: 52f53e2d3f852ed04d785e4e }
{ code: 'd',
name: 'translationStatus',
_id: 52f53e2d3f852ed04d785e4f }
null
category: (1) {"code":"b","name":"accessName","_id":"52f53e2d3f852ed04d785e4d"}
migration : complete
所以,在第一个控制台日志上,有 4 次迭代,没问题。但是在remove()方法的回调中,我只得到一个?!我不明白,我也应该在这里收到 4 条消息。 而且我不明白null,它来自第一个console.log。
奇怪的是,我收到一条关于 remove() 回调的消息,但所有文档都在数据库中正确删除。
【问题讨论】:
-
基本上,回调必须是
remove方法的第二个参数。但我只得到删除的行数。
标签: node.js mongodb migration database-migration