简体   繁体   English

异常的Mongo DB查询

[英]Unusual Mongo DB Query

I need to do a special query. 我需要做一个特殊的查询。 I have got an list of items availiable to do some Production. 我有一些可用于生产的物品清单。 With this data I need to query the Documents where each type of item is in the List. 使用此数据,我需要查询文档中每种类型的项目在列表中的位置。 A few (slightly) simplyfied Document looks may like this. 一些(略)简化的Document外观可能像这样。

{

    "productname": "iron",
    "requirements": [
        {
            "ammount": 2,
            "item": "coal"
        },
        {
            "ammount": 2,
            "item": "ironore"
        }
    ],
}

{

    "productname": "coal",
    "requirements": [
        {
            "ammount": 2,
            "item": "wood"
        }
    ],
}

{

    "productname": "copper",
    "requirements": [
        {
            "ammount": 2,
            "item": "coal"
        },
        {
            "ammount": 2,
            "item": "copperore"
        }
    ],
}

{

    "productname": "Chair",
    "requirements": [
        {
            "ammount": 2,
            "item": "wood"
        },
        {
            "ammount": 2,
            "item": "nails"
        },
        {
            "ammount": 2,
            "item": "paint"
        }
    ],
}
{

    "productname": "Wooden Toy",
    "requirements": [
        {
            "ammount": 2,
            "item": "wood"
        },
        {
            "ammount": 2,
            "item": "paint"
        }
    ],
}

An example list may look like: ["wood", "beer","coal", "paint", "copperore"] 示例列表可能如下所示:[“木头”,“啤酒”,“煤”,“油漆”,“铜”]

This should return the Documents of "Coal", "Copper" and "Wooden Toy", thus all of their requirements are in the List. 这应该返回“煤炭”,“铜”和“木制玩具”的文件,因此它们的所有要求都在列表中。 For "Chair" the "nails" are missing and for "Iron" the "Ironore" is missing. 对于“主席”,缺少“钉子”,对于“铁”,则缺少“ Ironore”。

(Sorry for my bad english ;) ) (对不起,我的英语不好 ;) )

From your given documents, if you want to find the document that contains requirements with coal and coppercore then the following query: 从给定的文档中,如果要查找包含coalcoppercore requirements的文档,请执行以下查询:

> db.materials.find({"requirements.item" : "coal", "requirements.item": "copperore" }).pretty()

Would return: 将返回:

{
        "_id" : ObjectId("584990f083842cff8826d44f"),
        "productname" : "copper",
        "requirements" : [
                {
                        "ammount" : 2,
                        "item" : "coal"
                },
                {
                        "ammount" : 2,
                        "item" : "copperore"
                }
        ]
}

如果我正确理解了您的问题(不确定是否可以这样做),则可以使用all运算符:

db.materials.find({"requirements.item": {$all: ["coal", "copperore"]}})

You can do this with $redact and $setIsSubset as a $cond tion 您可以使用$redact $setIsSubset$setIsSubset作为$cond tion来执行此操作

var myList =  ["wood", "beer","coal", "paint", "copperore"]
db.collection.aggregate([
    { "$match": { "requirements.item": { "$in":  myList } } },
    { "$redact": { 
        "$cond": [ 
            { 
                "$setIsSubset": [ 
                    "$requirements.item", 
                    myList 
                ] 
            }, 
            "$$KEEP", 
            "$$PRUNE"
       ]
    }}
])

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

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