我想更新数据对象中的 posts 数组。我的架构在这里不起作用:
const mongoose = require('mongoose');
const Post = require('./post');
const Review = require('./comment')
const User = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
username: {
type: String,
required: true
}
// necessary details needed for the user to initially working with
data: {
posts: [
Post
],
following: [String]
}
}
});
我已经尝试过:
User.update({_id: user}, {data:{$push: {posts: post
}}}).then(data=>{
console.log(data);
})
但它不起作用。谢谢。
试试这个代码:
User.update(
{ "_id": user },
{
"$push":
{
"data.posts": post
}
}
).then(data => {
console.log(data);
})
Read more about $push here.
希望这能解决您的疑问!
我是一名优秀的程序员,十分优秀!