简体   繁体   中英

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:-

"_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();

Also i tried with:-

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.

Or I need to staore the values into diff diff documents thats only the solution.

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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