gpt4 book ai didi

node.js - 使用 Mongoose 将项目推送到对象中的数组

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:37 26 4
gpt4 key购买 nike

我想更新数据对象中的 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.

希望这能解决您的疑问!

关于node.js - 使用 Mongoose 将项目推送到对象中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51596048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com