簡體   English   中英

我如何通過ID從另一個收集字段獲取數據

[英]How can i get data from another collection field by the id

情況就是這樣。 我有兩個模型/模式。 我想從出價模型中獲取我的工作模型中的bid字段

這是我使用郵遞員“過帳”出價模型的示例:

{
  "price": "8.770.000",
  "negotiation": "this is just example",
  "job" :"5c4192f5da1c3619f4089f5e" //this is job_id
}

當我嘗試“獲取”工作模型時,結果如下:

{
  "id": "5c4192f5da1c3619f4089f5e",
  "owner": {
    "id": "5be541622a228a231c6769c2",
    "username": "ceka",
    "rating": 0,
  },
  "title": "create website",
  "description": "this is just description",
  "category": "programming",
  "budget": "2.000.000",
  "remote": true,
  "bid": []
}

出價為空

出價模式:

const bidderSchema = new Schema({
  user: {
   type: Schema.ObjectId,
   ref: 'User',
   required: true
  },
  job: {
   type: Schema.ObjectId,
   ref: "Job",
   required: true
  },
  price: {
   type: String
  },
  negotiation: {
   type: String
  }
}

和工作模式:

const jobSchema = new Schema({
 owner: {
  type: Schema.ObjectId,
  ref: 'User',
  required: true
 },
 title: {
  type: String
 },
 description: {
  type: String
 },
 bid: [
  {
   type: Schema.ObjectId,
   ref: 'Bidder'
  }
 ]
}

路由器:

export const index = ({ querymen: { query, select, cursor } }, res, next) =>
Job.find(query, select, cursor)
 .populate('owner')
 .then((jobs) => jobs.map((job) => job.view()))
 .then(success(res))
 .catch(next)

因此,關鍵是我想通過“ job_id”將出價從出價模型輸入到工作中,該怎么辦? 可能嗎?

由於您使用的是貓鼬,因此只需填充bid行就可以了,就像這樣:

export const index = ({ querymen: { query, select, cursor } }, res, next) =>
Job.find(query, select, cursor)
 .populate('owner')
 .populate('bid')
 .then((jobs) => jobs.map((job) => job.view()))
 .then(success(res))
 .catch(next)

暫無
暫無

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

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