我有一个数组,我试图用 Node.JS 和 Mongoose 插入一个集合类型。DB Schema 定义如下:
//LIBRARY
var LibrarySchema = new mongoose.Schema({
name: {type:String, required: true},
keyw: {type:String, required: true},
data: {type:String, required: true},
ref: {url: String}
},
{collection: 'library'}
)
我已经能够成功插入数组(显示为逗号分隔的列表),但似乎无法掌握我认为简单的东西。我已经阅读了各种文档,这应该有效:
// Dummy Data
var b.topic = "This is my name"
var b.keywords = "keyword1,keyword2,keyword3"
var data = "This is some data, just another string"
var arrRef = []
arrRef[0] = "http://www.website1.com"
arrRef[1] = "http://www.website2.com"
//Create Schema & Save
var lib = new db.Library({
name: b.topic,
keyw: b.keywords,
data: b.data,
ref: {$addToSet: {url: arrRef}}
})
lib.save(function(err,docs) {
console.log("Error:" + err)
}
我试图在数据中拥有的是:
name: "This is my name"
keyw: "keyword1,keyword2,keyword3"
data: "This is some data, just another string"
ref:
url: "http://www.website1.com"
url: "http://www.website2.com"
关于我做错了什么的任何想法?上面的代码不会填充“ref”字段。