这里有两个模式,用户模式和连接模式。用户架构具有电子邮件、名称和密码。连接模式是指UserID等于用户的_id,hasRequested --该数组由请求连接&isConnectionE29的用户的_id组成--一个数组具有用户的_idE 110连接到用户E 211。假设有一个具有_id A的用户,并且有两个连接(与_id B和C)。假设B有连接A,X,Y,假设C有连接A,M,N。我应该写什么MongoDB查询,这样我就可以得到A的连接,以及B和C的连接,或者简单地说连接。
模式->>
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true
}
}, { timestamps: true });const ConnectionSchema = new mongoose.Schema({
UserID: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true
},
hasRequested: [{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}],
isConnection: [{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}]
})发布于 2022-06-03 13:18:45
不幸的是,无法在这些文档之间嵌套填充。为了达到这个目标,你应该提出分开的请求。但是,如果您将它们组合在一起,如:
const UserSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
hasRequested: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
],
isConnection: [{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}]
},
{ timestamps: true }
);然后,您可以使用这个查询来获取您想要的请求:
await User.findOne({ _id: userID }).populate(path = "hasRequested", populate = "hasRequested");https://stackoverflow.com/questions/72489009
复制相似问题