簡體   English   中英

如何在貓鼬的填充字段中使用“ find()”?

[英]How to using “find()” with populate field in mongoose?

我在同一數據庫中有2個集合。 我需要使用find()僅選擇具有“ team”等於“ team1”的“事件”

非常感謝你。

我嘗試按人口領域查找,但是沒有用。

  This is my Event schema const Event = mongoose.model('Event', new mongoose.Schema({ title: { type: String, required: true, min: 3 }, eventType: { type: String, lowercase: true }, date: { type: Date, default: Date.now() }, venue: String, country: String, callLog: { type: String, max: 1000 }, eventImgUrl: [String], editedBy : { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, timestamp: { type: String, default: Date.now() } })); 

 This is my User schema const User = mongoose.model('User', new mongoose.Schema({ username: { type: String, required: true, min: 3, lowercase: true }, name: String, email: String, team: String, role: { type: String, enum: ['admin', 'creator', 'viewer'] } })); 

 This is my Controller const events = await Event.find() .populate({ path: 'editedBy', select: ['name', 'team']}); res.send(events); 

find所有的Events有一個team"team1"

const team1Events = await Event.find({ team: "team1" });

select僅指定返回的字段。 您需要使用填充匹配

await Event.find()
    .populate({ path: 'editedBy', match: { yourField: 'yourmatch' }});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM