在MongoDB中输入用户名数组,并返回相同大小的id数组,对于不存在的用户名为null或false

Input array of usernames into MongoDB and get back array of ids that is the same size, with null or false for non-existing usernames

本文关键字:用户 数组 id 不存在 false null 输入 MongoDB 返回      更新时间:2023-09-26

我正在使用Meteor JS。我想在MongoDB中输入一个用户名数组,并返回一个大小相同的id数组,其中与不存在的用户名对应的元素为null或false。下面的代码能完成任务吗?

var userIds = Meteor.users.find({ username: { $in: usernameArray } }).map(function(item){ return item._id; });

如果是的话,我还能通过之类的东西找到不存在的用户名的索引吗

var badUsernames = userIds.indexOf(null);

只是澄清一下:我想知道Meteor.users.find是否无法通过usernameArray输入找到一些用户。userIds数组甚至会包含与无效用户id相对应的元素吗?

假设你得到了用户列表,你可以filter it:

var goodUserIds = userIds.filter(function(id){
    return !!id;
});

反转条件以获取无效的用户ID。

http://jsfiddle.net/7jrhzwa6/1/