我想更新数据对象中的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);
})
希望这能解决您的查询!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句