简体   繁体   English

如何修改我的mongodb / mongoose查询,使其返回更详细的json?

[英]how can I modify my mongodb/mongoose query so that it returns more detailed json?

In my app I have a model for storing users. 在我的应用程序中,我有一个用于存储用户的模型。 Each user can block other users, so I created an entry in this model that holds ids of blocked users: 每个用户都可以阻止其他用户,因此我在此模型中创建了一个条目,其中包含被阻止用户的ID:

...
blocked_user_ids: {type: [String]},
...

I also created an endpoint that allows me to get list of blocked users for a given user_id . 我还创建了一个端点,该端点使我可以获取给定user_id的阻止用户列表。 Since I wanted to have each user's details on that list I had to do the following queries: 由于我想在该列表中包含每个用户的详细信息,因此必须执行以下查询:

usersRoutes.get('/:id/blockedUsers/', functions.validateRequestsGET, function(req, res){
    var userId = req.params.id;

    async.auto({
        one: function(callback){
            findBlockedUsersIdsOfGivenUser(userId, callback);
        },
        two: ['one', function(callback, results){
            getBlockedUsersDetails(results.one, callback);
        }],
        final: ['one', 'two', function(callback, results) {
            res.status(200).send(results.two);
        }],
    }, function(err) {
        if (err) {
            sendError(res, err);
            return;
        }
    });
});

function findBlockedUsersIdsOfGivenUser(userId, callback) {
    var query = User.findOne({_id: userId}).select("blocked_user_ids");
    query.exec(function(err, blockedUsersIds){
        if(err) {
            return callback(err);
        }

        return callback(null, blockedUsersIds.blocked_user_ids);
    });
}

function getBlockedUsersDetails(blockedUsersIds, callback) {
    var query = User.find({_id: {$in: blockedUsersIds}});
    query.exec(function(err, blockedUsersDetails) {
        if(err) {
            return callback(err);
        }

        return callback(null, blockedUsersDetails);
    });
}

That worked well and returned me a json where each json node held information about specific user. 效果很好,并给我返回了一个json,其中每个json节点都保存有关特定用户的信息。

However, now my model changed, currently I had to change the string array into a structure: 但是,现在我的模型发生了变化,目前我不得不将字符串数组更改为一个结构:

...
blocked_user_ids: [{
    user_id: {type: String, required: true},
    is_anonymous: {type: Boolean, required: true}
}],
...

and now, besides getting user information in my json, I need to modify my queries so that the output json contains information about the flag is_anonymous - I want to add the value of that flag to each json node that represents each user. 现在,除了在json中获取用户信息外,我还需要修改查询,以便输出json包含有关标志is_anonymous -我想将该标志的值添加到代表每个用户的每个json节点上。 Can you help me with that? 你能帮我吗?

So you can get the ids using Array.map and after you get the result add the is_anonymous property to each user. 因此,您可以使用Array.map获取ID,并在获取结果后向每个用户添加is_anonymous属性。

function getBlockedUsersDetails(blockedUsersIds, callback) {
    // get all ids and also create users object for easier assigning of `is_anonymous` property
    var users = {};
    var ids = blockedUsersIds.map(function(a){
        users[a.user_id] = a.is_anonymous;
        return a.user_id;
    });
    // ids is same array you previously had as blockedUsersIds
    var query = User.find({_id: {$in: ids}});
    query.exec(function(err, blockedUsersDetails) {
        if(err) {
            return callback(err);
        }

        //blockedUsersDetails.forEach(function(user, index){            
            //user.is_anonymous = users[user.user_id];
        //});

        blockedUsersDetails.forEach(function(user, index){            
            blockedUsersDetails[index].is_anonymous = users[user.user_id];
        });

        return callback(null, blockedUsersDetails);
    });
}

I haven't tried this code so expect some bugs or typos :) 我没有尝试过这段代码,所以期待一些错误或错别字:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修改JSON结果? - how can I modify my JSON result? 如何使用 Mongoose/MongoDB 直接插入 JSON? - How can I directly insert JSON using Mongoose/MongoDB? 如何在Mongoose / Mongodb中创建查询以获取此json? - How to create a query in Mongoose/Mongodb to obtain this json? 如何重写我的SQL查询,以便它以特定格式(使用mysql或php)返回值? - How can I rewrite my sql query so that it returns the values in a specific format (using mysql or php)? 如何使用PHP在JSON中修改此mySQL查询的显示? - How can I modify the display of this mySQL query in JSON with PHP? 如何在不影响实际文档的情况下修改 ZCCADCDEDB567ABAE643E15DCF0974E503Z 中的查询结果? - How can I modify the resulting query in Mongoose without affecting the actual document? 如何修改我的代码,以便我可以在post请求中添加body参数 - How can I modify my code so I can add a body parameter to the post request 我应该如何修改 JSON 元数据以便我可以在 jupyter notebook 中打开 ipynb 文件? - How should I modify the JSON metadata so that I can open a ipynb file in a jupyter notebook? 如果 Postgres sql 查询返回 json,那么如何在 spring 引导中访问它? - If Postgres sql query returns json, so how can access it in spring boot? 我如何将这个按标题分组的 json 展平,以便我可以以更易于理解的方式呈现这些数据? - How do I flatten this json grouped by headers so I can present this data in a more understandable way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM