我有一个引用了Category模型的Product模型,下面的代码运行得很好,但我想知道这是不是最好的方法。
server.get "/:id/edit", (req, res) ->
Category.find({})
.sort("position", 1)
.exec (err, categories) ->
if not err
Product.findOne({_id: req.params.id })
.populate("categories")
.exec (err, data) ->
if err
res.json err
else
res.render "#{view_path}/products/edit",
title: "Edit Product"
user: req.session.user
product: data
categories: categories发布于 2012-07-06 02:59:47
使用populate的用处在很大程度上取决于您的Category模型的复杂程度以及您在Product模型相关页面上使用它的用途。
一般来说,类别是嵌入到您的产品中的一个很好的例子,因为它们不会频繁更改,并且会一直随产品一起拉。
您可能会发现this question很有趣。关于引用或嵌入的用例,总是有很多持续不断的讨论。
https://stackoverflow.com/questions/11346779
复制相似问题