gpt4 book ai didi

mongodb - 蒙哥 : How do I insert another subdocument to an existing document

转载 作者:IT老高 更新时间:2023-10-28 13:20:15 26 4
gpt4 key购买 nike

我已经开始学习Mongo了。给定以下集合,例如所谓的帖子,我将如何将新评论插入到现有文档中?我在 mongo 网站上看到的示例是针对“简单”集合的。感谢您的帮助。

{ "_id" : ObjectId( "510a3c5382d395b70b000034" ),

"authorId" : ObjectId( "..." ),
"comments" : [
{ "_id" : ObjectId( "..." ),
"authorId" : ObjectId( "..." ),
"content" : "",
"createdAt" : Date(...) } ],
"content" : "Some" }

最佳答案

你可以试试这样的:

    db.posts.update({ _id: ObjectId( "510a3c5382d395b70b000034" ) },
{
$push: { comments: { "_id" : ObjectId( "..." ),
"authorId" : ObjectId( "..." ),
"content" : "",
"createdAt" : Date(...) } }
})

关于mongodb - 蒙哥 : How do I insert another subdocument to an existing document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19372093/

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