简体   繁体   中英

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:

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

I also created an endpoint that allows me to get list of blocked users for a given 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.

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. 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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM