Mongodb:查找集合中不存在的项目

时间:2017-11-16 19:34:35

标签: mongodb

给出一个名为" inventory"有2个项目

currentCityList: string[];

getCitiesSaved(): Observable<string[]> {
  return this.http.get(this.url).map((data: Response) => {
    this.currentCityList = data.json().cityAlerts;
    return data.json().cityAlerts;
  });
}

如果我这样做一个查询

{
    "_id" : ObjectId("5a0de48bce523d1df7546e9c"),
    "name" : "tshirt",
    "qty" : 5
},

{
    "_id" : ObjectId("5a0de4acce523d1df7546ea8"),
    "name" : "pant",
    "qty" : 15
}

它将给出第一个对象。

我想稍后以json形式构造这样的结果,以便通过API返回。

db.inventory.find( { qty: { $in: [ 5, 10 ] } } )

我想到了以下选项

  • 创建[10,15]的临时集合,然后使用聚合函数$ lookup
  • 循环浏览项目列表并与[10,15]进行比较检查并构建所需的输出。

还可以做哪些其他选择?这个例子很小,但在实际应用中,项目数量将达到数百个。

1 个答案:

答案 0 :(得分:0)

这里最大的挑战实际上是将输入数组中的int值转换为string个键。我想我曾经看过Stack Overflow上的代码可以做到这一点,但我似乎也记得它非常hacky。

但是,以下代码可以让您非常接近目标。

db.collection.aggregate({
    $group: {
        "_id": null,
        "qties": {
            $addToSet: "$qty" // create an array of all unique "qty" values
        }
    }
}, {
    $project: {
        "_id": 0,
        "results": {
            $map: {
                input: values, // go through all values that we want to test, e.g. [ 5, 10 ]
                in: { // and map them into the following new structure
                    $arrayToObject: // transform our generated array of key-value pairs into an object
                    [[
                        {
                            "v": "$$this", // this will be just each value we look at - one by one
                            "k": { $cond: [ { $in: [ "$$this", "$qties" ] }, "true", "false" ] } // and this will tell us if the looked at value exists in our documents (or rather: in our array of unique values that we've generated in the $group stage)
                        }
                    ]]

                }
            }
        }
    }
})

这将吐出以下结构:

{
    "results" : [ 
        {
            "true" : 5.0
        }, 
        {
            "false" : 10.0
        }
    ]
}

或者,您可以编写以下内容:

var values = [ 5, 10 ]

db.getCollection('test').aggregate({
    $group: {
        "_id": null,
        "qties": {
            $addToSet: "$qty" // create an array of all unique "qty" values
        }
    }
}, {
    $project: {
        "results": {
            $map: {
                input: values, // go through all values that we want to test, e.g. [ 5, 10 ]
                in: { // and map them into the following new structure
                    "value": "$$this", // this will be just the value we look at
                    "found": { $cond: [ { $in: [ "$$this", "$qties" ] }, "true", "false" ] } // and this will tell us if the looked at value exists in our documents (in the array of unique values rather)
                }
            }
        }
    }
}, {
    $unwind: "$results" // flatten the results array
}, {
    $replaceRoot: {
        newRoot: "$results" // move the contents of the "results" field all the way up
    }
})

...将产生这两个文件:

{
    "value" : 5.0,
    "found" : "true"
}
{
    "value" : 10.0,
    "found" : "false"
}