MongoDB – 插入两个文档,分别在不同的集合中但共享相同的ObjectId。

huangapple go评论81阅读模式
英文:

MongoDB - Insert 2 documents, each in another collection but share ObjectId

问题

我有这个问题:
我想要将两个文档插入到两个集合中。

一个是用户集合,一个是公司集合。

这两个插入操作都是通过API请求的。

在创建(插入)公司时,我想要知道是哪个用户[创建/插入]了这个公司。而在用户文档中,我想要包含他插入的公司的 _id。

用户
{
_id: "userGeneratedId",
companyId: 公司._id
}

公司
{
_id: "companyGeneratedId",
registeredByID: 用户._id
}

这该如何实现?

谢谢,
Dufino

英文:

I have this question:
I want to insert 2 documents into 2 collections.

One is for user and one is for company.

Both inserts are requested via api.

In created(Inserted) company I want to know, which user[Created/inserted] create this company. And in user i want to have _id of company that he inserted.

User
{
_id: "userGeneratedId",
companyId : Company._id
}

Company
{
_id: "companyGeneratedId",
registeredByID : user._id
}

How can this be done?

Thank you,
Dufino

答案1

得分: 1

第一种简单的方法

将新字段添加到您的用户和公司模型中。可以称之为userSaveId或您选择的任何名称。现在,您将在这些新字段中插入相同的唯一标识,这样当您检索公司时,可以根据该ID来检索它。

第二种方法是执行4个操作。2个插入操作和两个更新操作。请注意,这将略微增加操作的响应时间。

假设您已经插入了一个user和一个company,并获取了user文档company文档ID,如下所示:

const user = User.save(yourData);
const company = Company.save(yourCompanyData);

之后,获取这些ID并用它们来更新已存储的文档,如下所示:

const userId = user._id;
const companyId = company._id;

User.updateOne({_id: userId}, {$set: {companyId: companyId}});
Company.updateOne({_id: companyId}, {$set: {registeredByID: userId}});

因此,完整的代码将如下所示:

const user = User.save(yourData);
const company = Company.save(yourCompanyData);
const userId = user._id;
const companyId = company._id;

User.updateOne({_id: userId}, {$set: {companyId: companyId}});
Company.updateOne({_id: companyId}, {$set: {registeredByID: userId}});
英文:

There are two ways to go about this

The first and easy way

Add new fields to your user and company model. maybe call it userSaveId or whatever you choose. Now you will insert same unique id to these new fields fields, so that when you are retrieving a company, you can just retrieve it based on that ID.


The second way this could be done is by performing 4 operations. 2 insert operations and two update operations. Note that this would slightly increase the response time of your operations.

Suppose you have inserted a user and company, get the IDs of both the user document and company document as such:

const user = User.save(yourData);
const company = Company.save(yourCompanyData);

Afterwards get the ids and use it to update the documents that are already stored as such:

const userId = user._id;
const companyId = company._id;

User.updateOne({_id: userId}, {$set: {companyId: companyId}});
Company.updateOne({_id: companyId}, {$set: {registeredByID: userId}});

So the complete code would be this:

const user = User.save(yourData);
const company = Company.save(yourCompanyData);
const userId = user._id;
const companyId = company._id;

User.updateOne({_id: userId}, {$set: {companyId: companyId}});
Company.updateOne({_id: companyId}, {$set: {registeredByID: userId}});

huangapple
  • 本文由 发表于 2023年1月11日 05:09:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75075894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定