简体   繁体   English

如何从 mongodb 的文档数组中提取嵌套文档?

[英]How to pull nested document from the array of documents in mongodb?

I am trying to remove a nested objects array in my document.我正在尝试删除文档中的嵌套对象数组。 The scenario is that i am searching for the days an event will be organised for, by using its eventid场景是我正在使用 eventid 搜索将组织活动的日期

const { eventid, typesOfTicketId } = req.params;
const eventDays = await EventDate.find({event: eventid});

Here eventid is passed from params as "5e9c0f0593ab3c058e282bfa".这里 eventid 从参数传递为“5e9c0f0593ab3c058e282bfa”。 I then want to remove a requested day from the nested objects array.然后我想从嵌套对象数组中删除请求的日期。 From the above query, I am receiving an array of dates and on each index of array the document is in this format:从上面的查询中,我收到了一个日期数组,并且在数组的每个索引上,文档都是这种格式:

[{
    "_id" : ObjectId("5ea7f54b8b22480431f1a455"),
    "day" : "1588186800",
    "typesOfTicket" : [ 
        {
            "_id" : ObjectId("5ea7f54b8b22480431f1a456"),
            "ticket" : "Adult Tickets",
            "noTickets" : 40,
            "price" : 50,
            "ticketsLeft" : 40
        }, 
        {
            "_id" : ObjectId("5ea7f54b8b22480431f1a457"),
            "ticket" : "Children Tickets",
            "noTickets" : 50,
            "price" : 30,
            "ticketsLeft" : 50
        }
    ],
    "event" : ObjectId("5e9c0f0593ab3c058e282bfa"),
    "__v" : 0
},
{
    "_id" : ObjectId("5ea7f5678b22480431f1a45f"),
    "day" : "1588273200",
    "typesOfTicket" : [ 
        {
            "_id" : ObjectId("5ea7f5678b22480431f1a460"),
            "ticket" : "Male Tickets",
            "noTickets" : 50,
            "price" : 5,
            "ticketsLeft" : 50
        }, 
        {
            "_id" : ObjectId("5ea7f5678b22480431f1a461"),
            "ticket" : "Female Tickets",
            "noTickets" : 50,
            "price" : 5,
            "ticketsLeft" : 50
        }
    ],
    "event" : ObjectId("5e9c0f0593ab3c058e282bfa"),
    "__v" : 0
}]

What i want is to find a way to remove the document in the nested typesOfTicket array, like lets say i want to remove the Object with id: typesOfTicketId.我想要的是找到一种方法来删除嵌套 typesOfTicket 数组中的文档,比如我想删除带有 id 的 Object:typesOfTicketId。 (eg typesOfTicketId = "5ea7f5678b22480431f1a461"), the female ticket one by passing its ID. (eg typesOfTicketId = "5ea7f5678b22480431f1a461"), 女票一通过其ID。

I have already tried this query:我已经尝试过这个查询:

await EventDate.update({event: eventid}, {
   $pull: {
      typesOfTicket: {
         _id: "typesOfTicketIDHERE"
      }
   }
});

But the above given query is only working if i am removing the first index of eventDays Array, like if i am deleting the ID: "5ea7f54b8b22480431f1a456", then this will work but if i am going for the id's on the second index like "Female tickets"/"5ea7f5678b22480431f1a461", then it is not working.但是上面给定的查询仅在我删除 eventDays 数组的第一个索引时才有效,例如如果我删除 ID:“5ea7f54b8b22480431f1a456”,那么这将起作用,但如果我要在第二个索引上查找 id,例如“Female”票“/”5ea7f5678b22480431f1a461”,那么它不工作。

I found the solution to my problem, the above query did work correctly after just some adjustments我找到了解决问题的方法,经过一些调整,上述查询确实可以正常工作

await EventDate.update({event: eventid}, {
   $pull: {
      typesOfTicket: {
         _id: "typesOfTicketIDHERE"
      }
   }
}, { multi: true });

Just specifying the multi params to true will do the trick.只需将多参数指定为 true 即可。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM