简体   繁体   English

如何查询包含数组MongoDB数组中某些元素的特定值的单个文档

[英]How to query single document which contains specific value of some element in array of arrays MongoDB

I have an document that contains array of arrays i am using embedded document in MongoDb.Say i have collection name Orders looks like:- 我有一个包含数组数组的文档,我在MongoDb中使用嵌入式文档。说我有集合名称。订单看起来像:-

"_id" : "HjPGrdkffg7dQPtiX",
    "ListOrdersResult" : [
        {
            "Orders" : {
                "Order" : [
                    {
                        "LatestShipDate" : "2016-01-13T18:29:59Z",
                        "OrderType" : "StandardOrder",
                        "PurchaseDate" : "2016-01-11T10:24:49Z",
                        "PaymentExecutionDetail" : {
                            "PaymentExecutionDetailItem" : {
                                "PaymentMethod" : "COD",
                                "Payment" : {
                                    "CurrencyCode" : "INR",
                                    "Amount" : "839.30"
                                }
                            }
                        },
                        "BuyerEmail" : "vccdbptpx2ssd74882@marketplace.amazon.in",
                        "AmazonOrderId" : "402-4031538-7451469",
                        "LastUpdateDate" : "2016-01-14T06:47:17Z",
                        "ShipServiceLevel" : "IN Exp Dom 2",
                        "NumberOfItemsShipped" : "1",
                        "OrderStatus" : "Shipped",
                        "SalesChannel" : "Amazon.in",
                        "ShippedByAmazonTFM" : "false",
                        "LatestDeliveryDate" : "2016-01-19T18:29:59Z",
                        "NumberOfItemsUnshipped" : "0",
                        "BuyerName" : "xyz",
                        "EarliestDeliveryDate" : "2016-01-13T18:30:00Z",
                        "OrderTotal" : {
                            "CurrencyCode" : "INR",
                            "Amount" : "839.30"
                        },
                        "IsPremiumOrder" : "false",
                        "EarliestShipDate" : "2016-01-11T18:30:00Z",
                        "MarketplaceId" : "A21TJRRWUN4KGVC",
                        "FulfillmentChannel" : "MFN",
                        "TFMShipmentStatus" : "Delivered",
                        "PaymentMethod" : "COD",
                        "ShippingAddress" : {
                            "StateOrRegion" : "HARYANA",
                            "City" : "GURGAON",
                            "Phone" : "9999999999",
                            "CountryCode" : "IN",
                            "PostalCode" : "122001",
                            "Name" : "Murthy",
                            "AddressLine1" : "House No. , J Block, Badshahpur"
                        },
                        "IsPrime" : "false",
                        "ShipmentServiceLevelCategory" : "Expedited"
                    },
                    {
                        "LatestShipDate" : "2016-01-13T18:29:59Z",
                        "OrderType" : "StandardOrder",
                        "PurchaseDate" : "2016-01-11T13:16:49Z",
                        "PaymentExecutionDetail" : {
                            "PaymentExecutionDetailItem" : {
                                "PaymentMethod" : "COD",
                                "Payment" : {
                                    "CurrencyCode" : "INR",
                                    "Amount" : "899.40"
                                }
                            }
                        },
                        "BuyerEmail" : "xyz@marketplace.amazon.in",
                        "AmazonOrderId" : "402-2142159-5087541",
                        "LastUpdateDate" : "2016-01-14T06:47:15Z",
                        "ShipServiceLevel" : "IN Exp Dom 2",
                        "NumberOfItemsShipped" : "1",
                        "OrderStatus" : "Cancel",
                        "SalesChannel" : "Amazon.in",
                        "ShippedByAmazonTFM" : "false",
                        "LatestDeliveryDate" : "2016-01-19T18:29:59Z",
                        "NumberOfItemsUnshipped" : "0",
                        "BuyerName" : "demo prakash",
                        "EarliestDeliveryDate" : "2016-01-13T18:30:00Z",
                        "OrderTotal" : {
                            "CurrencyCode" : "INR",
                            "Amount" : "899.40"
                        },
                        "IsPremiumOrder" : "false",
                        "EarliestShipDate" : "2016-01-11T18:30:00Z",
                        "MarketplaceId" : "A21TJEUUN4WGV",
                        "FulfillmentChannel" : "MFN",
                        "TFMShipmentStatus" : "Delivered",
                        "PaymentMethod" : "COD",
                        "ShippingAddress" : {
                            "StateOrRegion" : "DELHI",
                            "City" : "DELHI",
                            "Phone" : "99999999",
                            "CountryCode" : "IN",
                            "PostalCode" : "110038",
                            "Name" : "Demo prakash",
                            "AddressLine1" : "Hn 638 gali n 04 Wazirabad new delhi"
                        },
                        "IsPrime" : "false",
                        "ShipmentServiceLevelCategory" : "Expedited"
                    },
                    }
                ]
            },
            "CreatedBefore" : "2015-03-19T06:17:59Z"
        }
    ],
    "ResponseMetadata" : {
        "RequestId" : "cf94645e-ada7-4ec6-b161-a97d07a77817"
    },
    "seller_user_id" : "yg4e34ccodzf3GPR2",
}

So as you can see this is the single document that contains the whole data of array i want to fetch the orders whose status is cancel from this order array. 因此,如您所见,这是包含数组的整个数据的单个文档,我想从该订单数组中获取状态为取消的订单。

So for that i have use :- 所以为此我使用了:-

var orderDetails = orders.find({"ListOrdersResult.Orders.Order":{$elemMatch: { OrderStatus:"Canceled"}}}).fetch(); var orderDetails = orders.find({“ ListOrdersResult.Orders.Order”:{$ elemMatch:{OrderStatus:“ Canceled”}}})。fetch();

Also i tried with:- 我也尝试过:-

orders.find({"ListOrdersResult.Orders.Order.OrderStatus":'Canceled'}).fetch(); orders.find({“ ListOrdersResult.Orders.Order.OrderStatus”:'Canceled'})。fetch();

So this will return the whole document that contains status as canceled and other as well but i want only selected result from the document that contains status as pending. 因此,这将返回包含状态为“已取消”和其他状态的整个文档,但是我只希望从包含状态为“未决”的文档中选择结果。

So is there any way in mongoDb to query a selected value from a single document that contains nested array of arrays as object. 那么mongoDb中有什么方法可以从包含嵌套数组数组作为对象的单个文档中查询选定的值。

Or I need to staore the values into diff diff documents thats only the solution. 或者我需要将值存储到diff diff文档中,这仅是解决方案。

Any help would be appriciated please contribute 任何帮助将被请请贡献

Thanks! 谢谢!

You can go with any meteor aggregate package. 您可以使用任何流星聚合程序包。 You can use match and then group the data then send it to client-side.Like : 您可以使用match然后对数据进行分组,然后将其发送到客户端。

var ordersLines = orders.aggregate([
                      {$unwind : "$ListOrdersResult.Orders.Order"},
                      {$match : { OrderStatus:"Canceled"} },
                      {$project : {
                      OrderType : '$ListOrdersResult.Orders.Order.OrderType'
                      ....  
                           }
                      }
                  ]);
return ordersLines;

But I suggest you go with a different document. 但我建议您使用其他文档。

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

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