简体   繁体   English

如何在mongodb中的同一集合中查询大于不同文档中的值?

[英]How to query greater than values in different documents from same collection in mongodb?

I have a sales collection as following.我有一个销售集合如下。 I want to query using mongo shell to find product details which cost greater than 20.我想使用 mongo shell 查询以查找成本大于 20 的产品详细信息。

[{
  "Customer_Info":{
        "Customer_Id": 1,
      "First_Name": "John",
      "Last_Name": "Smith",
      "Address": "Sydney",
      "State": "Sydney",
      "Postcode": "0920",
      "Phone": "0143464663"
  },
  "Order_Info": [
    {
      "Order_Id": 1,
      "Order_Date": "01-01-2020",
      "Delivery_Date": "01-01-2020",
      "Line_Info": [
        {
          "Line_Id": 1,
          "Line_Price": 100,
          "Line_Units_Added": 2,
          "Product_Info": {
            "Product_Id": 300,
            "Product_Name": "Orange",
            "Product_Description": "Fruit",
            "Number_in_Stock": 50,
            "Product_Weight": 1,
            "Product_Cost": 300,
            "Product_in_Date": "01-01-2020",
            "Product_Type": "Fruits",
            "Farmer_Info": {
              "Farmer_Id": 123,
              "Farmer_First_Name": "Drake",
              "Farmer_Last_Name": "Levine",
              "Farmer_Address": "Sydney",
              "Farmer_State": "Sydney",
              "Farmer_Post_Code": "1200"
            }
          }
        },
        {
          "Line_Id": 5,
          "Line_Price": 25,
          "Line_Units_Added": "1",
          "Product_Info": {
            "Product_Id": 305,
            "Product_Name": "Banana",
            "Product_Description": "Fruit",
            "Number_in_Stock": 100,
            "Product_Weight": 1,
            "Product_Cost": 100,
            "Product_in_Date": "01-01-2020",
            "Product_Type": "Fruits",
            "Farmer_Info": {
              "Farmer_Id": 123,
              "Farmer_First_Name": "Drake",
              "Farmer_Last_Name": "Levine",
              "Farmer_Address": "Sydney",
              "Farmer_State": "Sydney",
              "Farmer_Post_Code": "1200"
            }
          }
        },
        {
          "Line_Id": 6,
          "Line_Price": 100,
          "Line_Units_Added": "1",
          "Product_Info": {
            "Product_Id": 305,
            "Product_Name": "Jackfruit",
            "Product_Description": "Fruit",
            "Number_in_Stock": 20,
            "Product_Weight": 1,
            "Product_Cost": 50,
            "Product_in_Date": "01-01-2020",
            "Product_Type": "Fruits",
            "Farmer_Info": {
              "Farmer_Id": 123,
              "Farmer_First_Name": "Drake",
              "Farmer_Last_Name": "Levine",
              "Farmer_Address": "Sydney",
              "Farmer_State": "Sydney",
              "Farmer_Post_Code": "1200"
            }
          }
        },
        {
          "Line_Id": 7,
          "Line_Price": 200,
          "Line_Units_Added": 1,
          "Product_Info": {
            "Product_Id": 310,
            "Product_Name": "Dragon Fruit",
            "Product_Description": "Fruit",
            "Number_in_Stock": 10,
            "Product_Weight": 1,
            "Product_Cost": 2,
            "Product_in_Date": "01-01-2020",
            "Product_Type": "Fruits",
            "Farmer_Info": {
              "Farmer_Id": 123,
              "Farmer_First_Name": "Drake",
              "Farmer_Last_Name": "Levine",
              "Farmer_Address": "Sydney",
              "Farmer_State": "Sydney",
              "Farmer_Post_Code": "1200"
            }
          }
        }
      ]
    },
    {
      "Order_Id": 2,
      "Order_Date": "01-01-2020",
      "Delivery_Date": "01-01-2020",
      "Line_Info": [
        {
          "Line_Id": 2,
          "Line_Price": 200,
          "Line_Units_Added": 2,
          "Product_Info": {
            "Product_Id": 301,
            "Product_Name": "Mango",
            "Product_Description": "Fruit",
            "Number_in_Stock": 50,
            "Product_Weight": 1,
            "Product_Cost": 500,
            "Product_in_Date": "01-01-2020",
            "Product_Type": "Fruits",
            "Farmer_Info": {
              "Farmer_Id": 123,
              "Farmer_First_Name": "Drake",
              "Farmer_Last_Name": "Levine",
              "Farmer_Address": "Sydney",
              "Farmer_State": "Sydney",
              "Farmer_Post_Code": "1200"
            }
          }
        }
      ]
    }
  ]
},{
  "Customer_Info":{
  "Customer_Id": 2,
  "First_Name": "Popeye",
  "Last_Name": "Sailorman",
  "Address": "Sydney",
  "State": "Sydney",
  "Postcode": "0920",
  "Phone": "123456"},
  "Order_Info": [
    {
      "Order_Id": 3,
      "Order_Date": "01-01-2020",
      "Delivery_Date": "01-01-2020",
      "Line_Info": [
        {
          "Line_Id": 3,
          "Line_Price": 50,
          "Line_Units_Added": 5,
          "Product_Info": {
            "Product_Id": 500,
            "Product_Name": "Spinach",
            "Product_Description": "Vegetables",
            "Number_in_Stock": 30,
            "Product_Weight": 1.5,
            "Product_Cost": 30,
            "Product_in_Date": "01-01-2020",
            "Product_Type": "Veg",
            "Farmer_Info": {
              "Farmer_Id": "420",
              "Farmer_First_Name": "Olive",
              "Farmer_Last_Name": "Lewine",
              "Farmer_Address": "Sydney",
              "Farmer_State": "Sydney",
              "Farmer_Post_Code": "1200"
            }
          }
        }
      ]
    }
  ]
}]

Want to find those product name and product cost which are greater than 20 using mongo shell.想使用 mongo shell 查找那些大于 20 的产品名称和产品成本。 I have done like below:我做了如下:

db.sales.find( { "Order_Info.Line_Info.Product_Info.Product_Cost": { $gt: 20} }, {"_id": 0, 
    "Order_Info.Line_Info.Product_Info.Product_Name": 1, 
    "Order_Info.Line_Info.Product_Info.Product_Cost": 1} ).pretty();

But unfortunately it returning only the first document values.但不幸的是它只返回第一个文档值。 How to achieve it?如何实现?

You can achieve this with aggregation您可以通过聚合来实现这一点

  1. $map helps to go with each element in array $map有助于处理数组中的每个元素
  2. $filter helps to use condition and filter $filter有助于使用条件和过滤器

Mongo script蒙戈脚本

[
  {
    $project: {
      Order_Info: {
        $map: {
          input: "$Order_Info",
          in: {
            $filter: {
              input: "$$this.Line_Info",
              cond: {
                $gt: [
                  "$$this.Product_Info.Product_Cost",
                  30
                ]
              }
            }
          }
        }
      }
    }
  }
]

Working Mongo playground工作Mongo 游乐场

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

相关问题 如何从 MongoDB 中的不同集合文档中查询所有参考 ID - How to query all the reference ids as from different documents of collection in MongoDB 如何对同一集合中不同文档的值求和? - How can I sum values from different documents in the same collection? mongodb:查询平均值大于某个数的文档 - mongodb: Query documents with average greater than a number MongoDB-如何从集合中查询嵌入式文档 - MongoDB - How to query Embedded Documents from a collection 如何查询MongoDB中的集合值和引用文档值? - How to query on collection values ​and referenced documents values ​in MongoDB? 如何通过相同键的两个值查找集合 Mongodb 中的所有文档? - How to find all documents in a collection Mongodb by two values of the same key? MongoDB - 在同一个集合上有不同文档的缺点? - MongoDB - Downside to having different documents on same collection? 如何将 MongoDB-atlas 集合中的特定文档与存储在同一集群中不同集合中的文档相关联? - How do I associate specific documents in a collection in MongoDB-atlas to documents that are stored in a different collection in the same cluster? 查询同一集合中的父子文档 MongoDB - Query documents with parent and child in same collection MongoDB 如何使用相同的键但值不同来更新集合中的所有文档? - How to update all documents in a collection with same key but different values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM