node.js - 如何修改我的 mongodb/mongoose 查询,以便它采用对象数组而不是字符串数组?

标签 node.js mongodb mongoose

在我的应用程序中,我有一个评论架构:

var CommentsSchema = new Schema({
    user_id: {type: String, required: true, ref: 'users'},
    text_content: {type: String},
    is_anonymous: {type: Boolean, default: false}
});

现在我正在构建一个 mongoose 查询来下载所有评论并将其显示给用户。

由于我不想下载被最终用户阻止的用户的帖子,因此我引入了排除被阻止作者的帖子的可能性:

var blockedUsers = req.body.blockedUsers;

function withBlockedUsers(query, blockedUsers) {
    if(blockedUsers != undefined){
        query.$and.push({ 'user_id' : { $nin: blockedUsers } });
    }

    return query;
}

var query = {};
query.$and = [];

query = withBlockedUsers(query, blockedUsers)
...
query = Comment.find(query);

query.exec(function(err, comments){
        if(err) {
            callback(err);
            return;
        }
    return callback(null, comments);
    });

该代码有效,当我调用端点时,我需要向那里发送一个包含被阻止用户 ID 的 string 数组,并且他们的帖子将被排除。

现在我正在更改我的功能,而不是传递被阻止用户的 string 数组,而是传递对象数组:

{ 
 user_id: '586af425378c19fc044aa85f'
 is_anonymous: '0' 
},

当满足这两个条件时,我不想下载这些用户的帖子。

例如,当我的应用程序中有两个帖子时:

{
    user_id: '586af425378c19fc044aa85f',
    text_content: 'text1',
    is_anonymous: true
},
{
    user_id: '586af425378c19fc044aa85f', //same as above
    text_content: 'text2',
    is_anonymous: false
}

我传递了blockedUsers对象:

{ 
 user_id: '586af425378c19fc044aa85f'
 is_anonymous: '0' 
},

作为返回,我只需要显示:

{
    user_id: '586af425378c19fc044aa85f',
    text_content: 'text1',
    is_anonymous: true
},

应阻止另一个帖子,因为 user_id 已被识别,并且 is_anonymousfalse

使用我当前的代码,我收到错误:

message: 'Cast to string failed for value "[object Object]" at path "user_id"', name: 'CastError', kind: 'string', value: { user_id: '586af425378c19fc044aa85f', is_anonymous: '0' }, path: 'user_id', reason: undefined } Cast to string failed for value "[object Object]" at path "user_id"

最佳答案

您需要一种方法来构建使用 $nor 的查询。 运算符,对由一个或多个查询表达式组成的数组执行逻辑 NOR 运算,并返回数组中所有查询表达式均失败的文档。

例如,当您传递像

这样的对象数组时
var blockedUsers = [
    {
        user_id: '586af425378c19fc044aa85f'
        is_anonymous: '0' 
    },
    {
        user_id: '585808969e39db5196444c06'
        is_anonymous: '0' 
    },
    {
        user_id: '585808969e39db5196444c07'
        is_anonymous: '0' 
    }
]

您的查询基本上应该结束为

Comment.find({
    "$nor": [
        {
            user_id: '586af425378c19fc044aa85f'
            is_anonymous: false 
        },
        {
            user_id: '585808969e39db5196444c06'
            is_anonymous: false
        },
        {
            user_id: '585808969e39db5196444c07'
            is_anonymous: false 
        }
    ]
}).exec(function(err, comments){
    if(err) {
        callback(err);
        return;
    }
    return callback(null, comments);
});

所以任务是转换类似的东西

var blockedUsers = [
    {
        user_id: '586af425378c19fc044aa85f'
        is_anonymous: '0' 
    },
    {
        user_id: '585808969e39db5196444c06'
        is_anonymous: '0' 
    },
    {
        user_id: '585808969e39db5196444c07'
        is_anonymous: '0' 
    }
]

var query = {
    "$nor": [
        {
            user_id: '586af425378c19fc044aa85f'
            is_anonymous: false 
        },
        {
            user_id: '585808969e39db5196444c06'
            is_anonymous: false
        },
        {
            user_id: '585808969e39db5196444c07'
            is_anonymous: false 
        }
    ]
}

由于您传递的是对象数组,因此可以使用 map() 方法将 is_anonymous 值转换为 bool 值,因此您的重构函数应如下所示:

function withBlockedUsers(query, blockedUsers) {
    var norOperator = [];
    if(blockedUsers != undefined){
        norOperator = blockedUsers.map(function(user){
            return {
                "user_id": user.user_id,
                "is_anonymous": (user.is_anonymous === '1')
            }
        });
        query["$nor"] = norOperator;
    }
    return query;
}

关于node.js - 如何修改我的 mongodb/mongoose 查询,以便它采用对象数组而不是字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41441637/

相关文章:

javascript - 如何删除 Mongoose 中带有引用集合的集合?

node.js - 尝试在 DocumentDB 中插入 200 个文档时出现 RequestRateTooLargeException

node.js - 如何设置VS Code集成终端的node版本?

javascript - Webpack HMR 不重新加载 HTML 文件

javascript - 更好地从 Node 或每个请求的连接到 memcached/mongo 的单一全局连接?

Mongoose 将默认设置为空对象

node.js - Heroku piggyback SSL with node and express - 需要任何配置吗?

node.js - Mongoose:过滤查询结果

mongodb - mongodb 中的异构批量更新

node.js - 如何进行更新突变 - GraphQL(加上 mongoDB)