【发布时间】:2021-03-27 02:52:53
【问题描述】:
我有一个集合 (users),其中包含一些文档,如下所示:
{
_id: ObjectId("56d45406be05db4022be51f9"),
morecontent : ""
},
{
_id: ObjectId("56d45406be05db3021be32e3"),
morecontent : ""
}
我想为user 集合中的每个条目创建一个新文档。
文档将从这样的通知对象创建:
{
type: 'alert',
msg: 'This is important'
}
集合notifications 应如下所示:
{
_id: ObjectId("56d45406be05db3021bf20a1"),
someoldcontent: "This was here before the request"
},
{
_id : ObjectId("56d45406be05db4022be20b1"),
user: ObjectId("56d45406be05db4022be51f9"),
type: 'alert',
msg: 'This is important'
},
{
_id : ObjectId("56d45406be05db3021be32e3"),
user: ObjectId("56d45406be05db3021be32e3"),
type: 'alert',
msg: 'This is important'
}
有没有办法在 mongodb 请求中做到这一点?
【问题讨论】:
-
您将使用什么作为数据库客户端? (node, java, c#) 或者你只想有一个查询来执行?
-
@profesor79 我会使用 nodejs
-
我不确定我的问题是否正确 1. 用户执行操作 2. 应用程序使用用户 _id 插入通知集合
-
@profesor79 我想为用户集合中的每个用户文档在通知集合中插入一个对象。除了应该有不同的 user_id 之外,每个用户的通知都是相同的。通知应该在通知集合中。
标签: mongodb collections mongodb-query document mongodb-update