【问题标题】:Search in a reference text field with Mongoose使用 Mongoose 在参考文本字段中搜索
【发布时间】:2017-09-04 04:41:55
【问题描述】:

我有这个架构

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%'

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

如何在猫鼬中处理?

【问题讨论】:

标签: node.js mongodb


【解决方案1】:

我在这里找到了答案http://mongoosejs.com/docs/populate.html 我使用 populatema​​tch

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 2013-04-06
    • 2023-03-04
    • 2015-11-10
    • 2015-11-25
    • 2021-03-09
    相关资源
    最近更新 更多