[英]How to query and get documents from two collections on mongoose
我需要在 mongoose 上同時查詢來自兩個 collections 的文檔。 我熟悉 SQL 查詢,但不熟悉 mongoDB。 我有兩個用戶模式,消息如下。
用戶
const UserSchema = new mongoose.Schema({
name: String,
email: {type: String, unique: true},
password: String,
avatar: {type: String, default: ""},
created_at: { type: Date, default: Date.now() }
});
module.exports = mongoose.model('User', UserSchema);
留言
const MessageSchema = new mongoose.Schema({
message: { type: String, default: "" },
from: { type: String, default: "" },
to: { type: String: default: "" },
is_read: { type: Boolean, default: false },
channel: { type: String, default: ''},
created_at: { type: Date, required: true, default: Date.now }
});
module.exports = mongoose.model('Message', MessageSchema);
我需要收到“is_read”為“false”的消息。 我想把“用戶名”和“頭像”放在一起。 message 的“from”值應與 User 的“_id”匹配。
我認為這篇文章總結得很好: Mongoose - query to get data from multiple collections
具體來說,第二個贊成的答案提到了 sql 和 mongodb 之間的相似之處,並繼續解釋如何在 Z758D31F351CE141BFC20 查詢中鏈接 collections。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.