繁体   English   中英

用猫鼬在参考文本字段中搜索

[英]Search in a reference text field with Mongoose

我有这个架构

var users = new Schema({
    name: {type: String, required: true},
    email: {type: String, required: true},
    password: {type: String, required: true},
    following: [{type: Schema.ObjectId, ref: "users"}]
});
users.index({name: 'text'});

我想使用Mongoose查找名称中带有“ john”的用户,并且它们存在于_id = x用户的以下数组中

换句话说,如果是SQL,查询将是(这只是说明关系的示例)

SELECT * FROM users where _id = x AND users.following.name LIKE '%john%'

我认为,如果将以下数组嵌入用户集合中,将很容易做到。

我怎么用猫鼬处理呢?

我在这里找到了答案http://mongoosejs.com/docs/populate.html我使用填充 匹配项

.findById("x", {following: 1}).populate({ path: 'following',match: {$text: {$search: "john"}}})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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