【问题标题】:Delete document with OR pattern - MongoDb and mongo-migrate删除带有 OR 模式的文档 - MongoDb 和 mongo-migrate
【发布时间】: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


【解决方案1】:

这不是你称呼remove的方式。

该链接指向 MongoDB 通用文档。比其他地方好一点。

您使用data 作为传入的参数,但您真正想要的是query。因此,争论的不是要删除哪些文档,而是要找到它们的查询。

【讨论】:

  • 我尝试按照文档进行操作,但我得到了这个:Error: Cannot use a writeConcern without a provided callback。另外,我希望有一条消息来详细说明每个已删除的数据。
  • 我找到了这个链接,我在Collection.remove 方法中看到了三个参数。不仅仅是上一个链接中的两个。我真的不明白! mongodb.github.io/node-mongodb-native/api-generated/…
  • 我刚刚做了一个console.log(collection.remove.toString()) 看看有什么功能,有3个args,而不仅仅是两个! function remove(selector, options, callback)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2015-04-04
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多