如何更新mongoose?

我是新的在Mongoose / nodejs和我挣扎着一个数组中的数组的简单更新。

这是架构:

var County = new Schema({ _id : Schema.ObjectId, name : String, biggestCity : String }); var Country = new Schema({ _id : Schema.ObjectId, name : String, counties : {type: [County], ref: "County"} }); var Continent = new Schema({ _id : Schema.ObjectId, countries : {type: [Country], ref: "Country"}, }); 

以下是我一直在尝试的更新代码:

 var continents = mongoose.model("Continent"); var update = { "countries.counties.name": newName, "countries.counties.biggestCity": newBiggestCity }; var conditions = { "_id": countryId, "countries.name": countryName, "countries.counties.name": countyName }; var options = { multi: false }; wagers.update(conditions, update, options, function(err, numAffected) { //callback code... }); 

当这样做时,err中的错误显示“不能使用string字段名”县“添加到数组”。 这是什么意思? 我究竟做错了什么?

您应该将子对象定义为另一个Schema,而不仅仅是一些匿名对象的列表。 ( 参考 )

尝试将Country定义为单独的架构,嵌套在Continent ,然后进行更新。