Node.js 在架构中保存ObjectId的数组

Node.js 在架构中保存ObjectId的数组,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个名为Shop的模型,其模式如下所示: 'use strict'; var mongoose = require('mongoose'), Schema = mongoose.Schema; var ShopSchema = new Schema({ name: { type: String, required: true }, address: { type: String, required: true }, description: String, sto

我有一个名为
Shop
的模型,其模式如下所示:

'use strict';

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ShopSchema = new Schema({
  name: { type: String, required: true },
  address: { type: String, required: true },
  description: String,
  stock: { type: Number, default: 100 },
  latitude: { type: Number, required: true },
  longitude: { type: Number, required: true },
  image: String,
  link: String,
  tags: [{ type: Schema.ObjectId, ref: 'Tag' }],
  createdAt: { type: Date, default: Date.now },
  updatedAt: { type: Date, default: Date.now }
});

module.exports = mongoose.model('Shop', ShopSchema);
显然,我想使用数组
标记
通过
ObjectId
引用另一个模型。当我通过
db.shops.update({…},{$set:{tags:…}})
将ID添加到属性中并且正确设置ID时,此设置工作正常。但是,当我尝试通过分配给模型的Express.js控制器执行此操作时,没有任何更新,甚至没有错误消息。以下是控制器中的更新功能:

// Updates an existing shop in the DB.
exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }
  Shop.findById(req.params.id, function (err, shop) {
    if (err) { return handleError(res, err); }
    if(!shop) { return res.send(404); }
    var updated = _.merge(shop, req.body);
    shop.updatedAt = new Date();
    updated.save(function (err) {
      if (err) { return handleError(res, err); }
      return res.json(200, shop);
    });
  });
};
这适用于
Shop
模型的任何其他属性,但不适用于标签。我还尝试将标记的类型设置为string,但这没有帮助


我想我在Mongoose中保存数组时遗漏了一些东西?

问题似乎是
\uu。merge()
无法正确处理合并数组,这就是您案例中的
标记
数组。一种解决方法是在合并后添加
标记的显式赋值
数组,前提是可以覆盖现有标记

var updated = _.merge(shop, req.body);
if (req.body.tags) {
  updated.tags = req.body.tags;
} 

希望这有帮助。。如果解决方案不足,您可以访问
lodash
论坛

保存
之前,您能给我们一个
更新的
对象的例子吗?当然,给您:
{id:5483977030adcc600450db0f,姓名:“Kunstspätkauf”,地址:“柏林10997 Schleische街19号”,纬度:52.49861,经度:13.44614999999,图像:'http://www.oushop.com/warp_sites/oushop.g6/files/Shop2.jpg更新日期:2014年12月6日星期六23:58:13 GMT+0000(GMT标准时间),创建日期:2013年10月22日星期二10:34:23 GMT+0100(GMT夏令时),标签:[5483977030adcc600450db13,5483977030adcc600450db14],股票:2}