[英]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.