Mongoose更新查询不适用于嵌套对象

时间:2016-04-21 18:20:32

标签: node.js mongoose

我有一个嵌套对象的模型,我想更新该文档的单个字段但无法执行此操作。我无法理解为什么它不起作用。

这是我的model

var sampleItemSchema = new Schema({
    id: {
        type: String,
        required: true
    },
    content: {
        type: Object,
        required: true
    },
    location: Object,
    createdAt: {
        type: Date,
        default: Date.now
    },
    sample: {type: mongoose.Schema.Types.ObjectId, ref: 'Sample'}
});

这是我的文件之一。

{ createdAt: Thu Apr 21 2016 19:46:17 GMT+0200 (CEST),
  __v: 0,
   sample: 571911df9a97810c3f35d83d,
  location: { city: 'Kildare' },
 content: 
  { images: [],
  price: { currency: 'EUR', amount: 2000 },
  createdAt: '2016-04-21T17:46:17.349Z',
  category: { id: '2012', name: 'Animals | Ponies' },
  body: 'Beaulieu Ginger Pop (Ben) is a 14 year old 13.2hh grey roan  New Forest pony. He has a full green passport, is microchipped and is   fully up...',
 title: '13.2hh All rounder Gelding' },
id: '12123191',
_id: 571911e99a97810c3f35d845 }

这是我尝试过的。

我只是提供部分代码

 models.SampleItem.find({
  sample: sample
}, function(err, sampeItemList) {
  console.log('Total sample: ', sampeItemList.length);
  async.eachSeries(sampeItemList, function(item, next) {
            item.content.body = "want to update this field";
            item.save(function(err, updatedItem) {
              console.log('Updated description...', index);
  })
})
})

请看一下。也许我做错了什么。

谢谢

2 个答案:

答案 0 :(得分:0)

Mongoose不允许您将join用作架构类型。而是将SELECT patient.firstname, patient.surname, practice.name, practice.address FROM patient JOIN appointment ON patient.patientid = appointment.patientid JOIN practice ON appointment.practiceid = practice.practiceid WHERE appointment.practiceid IN (1) ORDER BY firstname; 设置为Object。这将为您提供一个对象,其中包含您要设置的任何属性。

您可以看到所有有效架构类型in the Mongoose Schema Types documentation的列表。

(您可能还希望将type字段更改为Schema.Types.Mixed。)

答案 1 :(得分:0)

猫鼬不允许'Object'作为类型。将“ Schema.Types.Mixed”用于具有灵活/未知值的属性。

相关问题