简体   繁体   English

node.js-在Mongoose中填充嵌入式文档

[英]node.js - populate embedded document in Mongoose

Given: 鉴于:

var productSchema = new Schema({
  name: String,
  details : [{ name: String,  description: String }]
})


var listSchema = new Schema({
  name: String,
  products: [{
    product_id: { type: Schema.Types.ObjectId, ref: 'Product' },
    product_vid: { type: Schema.Types.ObjectId, ref: 'Product.details' }
    }]
})

How can I do the query for the List with only corresponding product_id with one details matching product_vid in it? 如何查询仅包含与product_vid匹配的一个详细信息的相应product_id的列表?

List.findById(list_id)
    .populate({
       path: 'products.product_id',
       populate: {
         path: 'products.product_vid'
       }
     })
     .exec(function(err, doc){
       ......
}

There is no need for 不需要

product_vid: {
type: Schema.Types.ObjectId, ref: 'Product.details'}

in listSchema. 在listSchema中。

var productSchema = new Schema({
name: String,
details : [{ name: String,  description: String }]
     })


var listSchema = new Schema({
   name: String,
   products: [{ type: Schema.Types.ObjectId, ref: 'Product' }])

      List.findById(list_id)
        .populate({
              path: 'products',
              match: { details: "your matching value"})
           .exec(function(err, doc){
                  ......
      }

This is wrong 这是错的

product_vid: { type: Schema.Types.ObjectId, ref: 'Product.details' }

detail is field of a model. 细节是模型的领域。 and not model itself. 而不是自己建模。

it should be something like.. 它应该像..

 var listSchema = new Schema({
   name: String,
   products: [{ type: Schema.Types.ObjectId, ref: 'Product' }],
})

and then 接着

 populate('products')

or probably 或可能

 populate('products products.details')

Just try and let me know. 请尝试让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM