简体   繁体   English

Mongoose/MongoDb 在数组中查找具有反向引用的文档

[英]Mongoose/MongoDb find documents with reverse reference in an array

I have 2 schemas:我有 2 个模式:

Order
- orderId
- date
- items: OrderItem[] (Array of ObjectIds of OrderItem)

OrderItem
- name
- price
- discount

Now I have the list of order items and I want to associate them with order.现在我有了订单项目列表,我想将它们与订单相关联。 As I'm storing the references in the Order .因为我将引用存储在Order中。

But I need to list all the OrderItems in a separate page from where I can link the items to order via API.但是我需要在一个单独的页面中列出所有OrderItems ,我可以通过 API 将这些项目链接到订单。 So I need to see which items are already linked to an order and which are still pending.因此,我需要查看哪些商品已经与订单相关联,哪些商品仍处于待处理状态。 Is there any way to get the OrderId in the list of OrderItem with the use of mongoose?有什么方法可以使用 mongoose 获取OrderItem列表中的OrderId

I've tried to reverser reference as well in the OrderItem by including the orderId in the OrderItem schema.我还尝试通过在OrderItem架构中包含orderId来反向引用OrderItem But, it causes an issue when I try to link the order items which are already linked.但是,当我尝试链接已经链接的订单项目时,它会导致问题。

Say for example, I've 2 orders:例如,我有 2 个订单:

o1:
items: i1, i2

o2:
items: i3, i4, i5

i1:
orderId: o1

i2:
orderId: o1

i3:
orderId: o2

i4:
orderId: o2

i5:
orderId: o2

i6:
orderId: o2

So from the list of items, user selects i2, i3 to link to o3.因此,从项目列表中,用户选择 i2、i3 以链接到 o3。 Then the following updates has to be made:然后必须进行以下更新:

o3 -> items: i2, i3
i2 -> orderId: o3
i3 -> orderId: o3

But it leaves a situation where o1 still holds i2 and o2 still holds i3 .但它留下了o1仍然持有i2并且o2仍然持有i3的情况。 Those documents also needs to be updated.这些文件也需要更新。 These many operations leaves an issue for simple linking leads to an complex ambiguous situation.这些许多操作留下了一个问题,简单的链接会导致复杂的模棱两可的情况。

If there's a way I can get list of items and orderId it belongs to without storing orderId in the OrderItem and only storing it in the Order.items can solve this situation.如果有一种方法可以获取项目列表和它所属的orderId ,而无需将orderId存储在OrderItem中,而仅将其存储在Order.items中可以解决这种情况。

Is there any way I can achieve this with mongoose / mongodb?有什么方法可以用 mongoose / mongodb 来实现吗?

Storing orderItem without orderId lead you to the problems when retrieve order detail from orderItem.当从 orderItem 检索订单详细信息时,存储没有 orderId 的 orderItem 会导致您遇到问题。 Storing orderItemId in order collection is not useful as you think.如您所想,将 orderItemId 存储在订单集合中并没有用。 Because you can get them from orderItem collection.因为您可以从 orderItem 集合中获取它们。 Query nested data is not easy.查询嵌套数据并不容易。 Try to bring all of them into a line.尝试将它们全部排成一条线。 Just my opinion.只是我的观点。

Order
- salerId
- buyerId
- date

OrderItem
- orderId
- name
- price
- discount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mongodb/mongoose findMany - 查找 ID 列在数组中的所有文档 - mongodb/mongoose findMany - find all documents with IDs listed in array 在MongoDB / Mongoose中按array.prop1对嵌套文档进行排序,然后按array.0.prop2查找文档 - Sort nested documents by array.prop1 and then find documents by array.0.prop2 in MongoDB/Mongoose Mongoose,在文档数组中查找项目 - Mongoose, find item in a documents array 如何在猫鼬中查找数组中的文档 - How to find documents in array in mongoose Mongodb Mongoose - 查找与 EXACT 查询匹配的文档 - Mongodb Mongoose - Find documents that match EXACT query Mongoose 根据子文档引用值查找文档 - Mongoose find documents based on subdocument reference value Mongoose - 查找包含子文档引用的文档 - Mongoose - Find documents that contain a reference of sub-documents 查找在MongoDB / Mongoose中给定数组中包含子集合的所有元素的特定字段的所有文档 - Find all documents where a particular field of all elements of a subcollection are included inside a given array in MongoDB / Mongoose 如何通过检查数组的第一个元素在mongoDB或mongoose中查找文档? - How can I find documents in mongoDB or mongoose by checking the first element of an array? 如何在 Node.JS 中使用 Mongoose 在 MongoDB 中查找其中包含空数组的文档? - How to find the documents which have an empty array in them in MongoDB using Mongoose in Node.JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM