Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

mongoose是怎么给数组文档添加数据的

比如
var PersonSchame = new mongoose.Schema({

name: String,
books: [{
    type:String
}]

});

这时候怎么给books添加数据

有点基础的问题 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你的意思是往数据库中一条数据的 books字段添加元素吗?如果是的话,参考下面这个例子:

db.personModel.update({
    _id: xxx    //查询到特定的一条数据
}, {
    $push:{
        members: username
    }
}, (err) => {
    //update操作完之后的回调函数
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...