json - Mongoose:将 $in 运算符与 JSON 对象一起使用

标签 json node.js mongodb mongoose

我是 Nodejs 和 mongodb 的新手。

我试图获取以下场景的数据。考虑如下 3 种模式 个人资料架构

var ProfileSchema = new Schema({
username: {type: String, match: /^[a-zA-Z0-9_.-]+$/, unique: true},
name: String});

帖子架构

var PostsSchema = new Schema({
profileid: {type: ObjectId, ref: 'Profile'},
message: {type: String, match: /^.{1,160}$/} });

遵循架构

var FollowSchema = new Schema({
profileid: {type: ObjectId, ref: 'Profile'},
followingid: {type: ObjectId, ref: 'Profile'}});

这类似于 Twitter 层次结构。

为了获取我的关注者的所有帖子,我尝试了以下操作

Follows.find({'profileid' : '500d18823e792d8814000001'}).select('followingid -_id').limit(20).sort({addedon: 'desc'}).execFind(function (arr,followings) {
    Posts.find({profileid: {$in: followings}}).limit(20).execFind(function (arr,data) {
        console.log(data);
    });
});

但是它不起作用。 请指导获得此信息的最佳方法。

感谢您的支持

最佳答案

您没有从对 Follows 集合的初始查询中得到任何返回值吗?您正在传递一个字符串

'500d18823e792d8814000001'

作为profileId,但是MongoDB以不同的方式存储字符串和ObjectId。由于profileid字段是ObjectId,因此在执行查询之前必须将字符串转换为ObjectId。

尝试查询关注集合

'profileid':ObjectId('500d18823e792d8814000001')

关于json - Mongoose:将 $in 运算符与 JSON 对象一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093802/

相关文章:

jquery - 如何使用 jQuery 访问此 JSON 返回中的变量

javascript - 在 Mongoose 中使用子文档数组的正确方法

apache - AWS 和 Node.js,我需要 nginx 还是 apache?

node.js - 后续迁移

java - 存储为字符串的 Android SQL Lite int 值在检索时显示额外字符

mongodb - 如何将 MongoDB 作为 Windows 服务运行?

Java如何在for循环之外获取一行Json数据

json - 如何从Redis获取所有数据到JSON

MongoDB $lookup 不使用索引

javascript - Extjs 树面板未加载 json 数据