【发布时间】: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%'
我认为如果将以下数组嵌入到用户集合中,它会很容易制作。
如何在猫鼬中处理?
【问题讨论】:
-
带聚合。看mongoosejs.com/docs/api.html#aggregate_Aggregate-lookuphot 做一对多join。