Mongodb 通过 ids 数组按顺序查找
我正在制作一个约会应用程序,其中算法通过排序返回匹配用户 uuid 的数组。 所以我想按该 uuid 数组按顺序查找所有用户。
我尝试这样做
//init algorithm
const algo = new Match(currentUser, users);
//calculate score
algo.calcScore();
//sort it
algo.sort();
//get array of uids of matching users
const matches = algo.matching_users;
//find users by that array of uid
const matching_users = await User
.find({
uid: {
$in: matches
}
})
.skip(total_items - items)
.limit(items)
.lean()
.exec()
这会返回用户,但不按顺序返回。 例如,让 uids 数组为:["c", "d", "a", "b"]
上面的查询返回以下位置的用户对象:[{...uid: "a"},{...uid: "b"},{...uid: "c"},{...uid : "d"}, ]
预期输出:[{...uid: "c"},{...uid: "d"},{...uid: "a"} ,{...uid: "b"}, ]
I am making a dating app where an algorithm returns an array of matching user's uuid by sorting it.
So I want to find all the users by that array of uuid in order.
I tried doing this
//init algorithm
const algo = new Match(currentUser, users);
//calculate score
algo.calcScore();
//sort it
algo.sort();
//get array of uids of matching users
const matches = algo.matching_users;
//find users by that array of uid
const matching_users = await User
.find({
uid: {
$in: matches
}
})
.skip(total_items - items)
.limit(items)
.lean()
.exec()
This returns the users but not in order.
For example let array of uids be: ["c", "d", "a", "b"]
the above query returns user objects in: [{...uid: "a"},{...uid: "b"},{...uid: "c"},{...uid: "d"}, ]
Expected output: [{...uid: "c"},{...uid: "d"},{...uid: "a"},{...uid: "b"}, ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论