MongoDB的总磁盘阵列和两个字段 [英] MongoDB Aggregate Array with Two Fields

查看:143
本文介绍了MongoDB的总磁盘阵列和两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有收集车辆使用下面的模式,所有的文章都只是一般的产品(无子产品包括):

  {
    _id:的ObjectId(554995ac3d77c8320f2f1d2e),
    模特:ILX
    年:2015年,
    做:极品,
    发动机 : {
        圆筒:4,
        升:1.5
    },
    产品:[
        的ObjectId(554f92433d77c803836fefe3),
        ...
    ]
}

和我有产品的收藏,其中一些是与仓库的SKU与普通产品,有的产品是适合的倍数一般产品儿子产品,这些子产品也有仓库的SKU相关的:


  

    

一般产品


  

  {
  _id:的ObjectId(554b9f223d77c810e8915539),
  品牌:AIRTEX
  产品:E7113M,
类型:燃油泵
warehouse_sku:[
    1_5551536f3d77c870fc388a04
    2_55515e163d77c870fc38b00a
]
}


  

    

儿童产品


  

  {
    _id:的ObjectId(55524d0c3d77c8ba9cb2d9fd),
    品牌:性能,
    产品:P41K,
    类型:Repuesto邦巴Gasolina,
    general_products:[
        的ObjectId(554b9f223d77c810e8915539),
        的ObjectId(554b9f123d77c810e891552f)
    ]
    warehouse_sku:[
        1_555411043d77c8066b3b6720
        2_555411073d77c8066b3b6728
    ]
}

我的问题是获得一般产品(_id和儿童产品内general_products)的列表下面的模式warehouse_sku:1 _

我已经创建了一个聚集查询结构如下:

  list_products = db.getCollection(产品)。骨料([
... {$匹配:{warehouse_sku:/ ^ 1 \\ _ /}},
... {$组:{_id:$ _id}}
...])

和该查询给我成功的结果:

  {_id:的ObjectId(55524d0c3d77c8ba9cb2d9fd)}
{_id:的ObjectId(554b9f223d77c810e8915539)}

但我需要获得普通产品的列表,以便我可以在车辆集合中使用$。

  list_products = [的ObjectId(55524d0c3d77c8ba9cb2d9fd)的ObjectId(554b9f223d77c810e8915539)]


  

    

例如:db.vehicles.find({产品:{$于:list_products}})


  

这最后一个查询我无法实现这一目标。


解决方案

使用聚合光标的 地图() 方法如下返回的ObjectID的数组:

  VAR管道= [
    {$匹配:{warehouse_sku:/ ^ 1 \\ _ /}},
    {$组:{_id:$ _id}}
]
list_products = db.getCollection(产品)
                  .aggregate(管道)
                  .MAP(功能(DOC){返回doc._id});

找到() 光标的 地图() 将在这里工作,以及:

  VAR的查询= {'warehouse_sku':/ ^ 1 \\ _ /},
    list_products = db.getCollection(产品)
                      .find(查询)
                      .MAP(功能(DOC){返回doc._id});


更新

在pymongo,你可以使用 拉姆达 功能与地图功能。因为地图预计要传递的功能外,还恰好是地方之一拉姆达经常出现:

 进口重
regx = re.compile(^ 1 \\ _,re.IGNORECASE)
products_cursor = db.products.find({warehouse_sku:regx})
list_products =列表(图((拉姆达DOC:文档[_ ID]),products_cursor))

I have vehicles collection with the following schema, all the articles are just general products (no child products included):

{
    "_id" : ObjectId("554995ac3d77c8320f2f1d2e"),
    "model" : "ILX",
    "year" : 2015,
    "make" : "Acura",
    "motor" : {
        "cylinder" : 4,
        "liters" : "1.5"
    },
    "products" : [
        ObjectId("554f92433d77c803836fefe3"),
        ...
    ]
}

And I have products collection, some of them are general products related with warehouse sku's and some products are "son" products that fit in multiples general products, these son products are also related with warehouse sku's:

general products

{
  "_id" : ObjectId("554b9f223d77c810e8915539"),
  "brand" : "Airtex",
  "product" : "E7113M",
"type" : "Fuel Pump",
"warehouse_sku" : [
    "1_5551536f3d77c870fc388a04",
    "2_55515e163d77c870fc38b00a"
]
}

child product

{
    "_id" : ObjectId("55524d0c3d77c8ba9cb2d9fd"),
    "brand" : "Performance",
    "product" : "P41K",
    "type" : "Repuesto Bomba Gasolina",
    "general_products" : [
        ObjectId("554b9f223d77c810e8915539"),
        ObjectId("554b9f123d77c810e891552f")
    ],
    "warehouse_sku" : [
        "1_555411043d77c8066b3b6720",
        "2_555411073d77c8066b3b6728"
    ]
}

My question is to obtain a list of general products (_id and general_products inside child products) for warehouse_sku that follow the pattern : 1_

I have created an aggregate query with the following structure:

list_products = db.getCollection('products').aggregate([
...  {$match: {warehouse_sku: /^1\_/}},
...  {$group: { "_id": "$_id" } }
... ])

And that query give me successfully a result :

{ "_id" : ObjectId("55524d0c3d77c8ba9cb2d9fd") }
{ "_id" : ObjectId("554b9f223d77c810e8915539") }

but I need to obtain a list of general products so I can use $in in the vehicles collection.

list_products = [ ObjectId("55524d0c3d77c8ba9cb2d9fd"), ObjectId("554b9f223d77c810e8915539")] 

example: db.vehicles.find({products:{$in: list_products}})

This last query I could not achieve it.

解决方案

Use the aggregation cursor's map() method to return an array of ObjectIds as follows:

var pipeline = [
    {$match: {warehouse_sku: /^1\_/}},
    {$group: { "_id": "$_id" } }
],
list_products = db.getCollection('products')
                  .aggregate(pipeline)
                  .map(function(doc){ return doc._id });

The find() cursor's map() would work here as well:

var query = {'warehouse_sku': /^1\_/},
    list_products = db.getCollection('products')
                      .find(query)
                      .map(function(doc){ return doc._id });


UPDATE

In pymongo, you could use a lambda function with the map function. Because map expects a function to be passed in, it also happens to be one of the places where lambda routinely appears:

import re
regx = re.compile("^1\_", re.IGNORECASE)
products_cursor = db.products.find({"warehouse_sku": regx})
list_products = list(map((lambda doc: doc["_id"]), products_cursor))

这篇关于MongoDB的总磁盘阵列和两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆