Node.js 在nodejs和mongodb中填充查询(使用mongoose)

Node.js 在nodejs和mongodb中填充查询(使用mongoose),node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有两个系列的产品和产品价格。在产品集合中有一个名为product\u id的字段,该字段位于字符串中,而在productprice集合中,该字段的名称与字符串相同 我如何确定这个模式,以及如何填充哪个产品附带了productprice字段 产品字段:\u id、产品id、名称 产品价格字段: _id,Product_id,price 两个集合的Product\u id中的值相同 const productpriceSchema = mongoose.Schema({ Product

我有两个系列的产品和产品价格。在产品集合中有一个名为
product\u id
的字段,该字段位于字符串中,而在
productprice
集合中,该字段的名称与字符串相同

我如何确定这个模式,以及如何填充哪个产品附带了
productprice
字段

产品字段:
\u id、产品id、名称

产品价格字段:

 _id,Product_id,price
两个集合的
Product\u id
中的值相同

const productpriceSchema = mongoose.Schema({
    Product_id: {
        type: mongoose.Schema.ObjectID,
        ref: 'Product'
    },
    price: String
});

const productSchema = mongoose.Schema({
    Product_Name: type: String,
    User_Object_ID  :type: String,
    cid :type: String
});

const Product = module.exports = mongoose.model('Product', productSchema);

const Productprice = module.exports = mongoose.model('Product_price', productpriceSchema);


module.exports.productwithprice = function(callback,limit){
 Productprice.find({}, callback).populate('Product_id')
}

有关详细信息,请检查的“填充虚拟现实”部分。

此查询提供了一个错误,即未指定默认引擎,也未提供扩展。此错误似乎是与mongoose无关的express渲染引擎出现了问题。请检查您的快递代码。此查询不起作用。在我的产品集合中,_id的形式为“_id”:ObjectId(“595f4cb77e713872c1297941”),我希望与productprice集合的产品_id:“595f4cb77e713872c1297941”匹配。请帮我做这件事。
//Product Schema
//you don't have to give field _id, mongodb will automatically generate it.
const productSchema = new Schema({
    Product_Id: String,
    name: String
});

const Product = mongoose.model('Product', productSchema);

//Product Price Schema

const productPriceSchema = new Schema({
    Product_Id: {
        type: Schema.ObjectId,
        ref: 'Product'
    },
    price: String
});

const ProductPrice = mongoose.model('ProductPrice', productPriceSchema)

ProductPrice.find({query})
.populate('Product_Id')
.exec((err, products) => {
    //Logic
});
var ProductSchema = new Schema({
  name: String,
  productId: String
});

var PriceSchema = new Schema({
  name: String,
  productId: String
});

ProductSchema.virtual('price', {
  ref: 'Price', // The model to use
  localField: 'productId', // Find price where `localField`
  foreignField: 'productId', // is equal to `foreignField`
  // If `justOne` is true, 'price' will be a single doc as opposed to
  // an array. `justOne` is false by default.
  justOne: true
});

var Product = mongoose.model('Product ', ProductSchema);
var Price = mongoose.model('Price ', PriceSchema);

Product.find({}).populate('price').exec(function(error, products) {
  /* `products.price` is available now */
});