我希望有一个步骤,如果数组中包含一个字段,则返回一个布尔值,如下所示:
$project: {
// would return whether the field 'type' is banana or apple
isFruit: { $type: { $in: ['apple', 'banana'] } },
},但这不管用。查看doc,我在容器测试周围什么也看不到。这有可能吗?
发布于 2016-09-21 10:16:00
这有点复杂,但是您可以使用$filter将输入数组过滤为与type匹配的元素(如果有的话),然后将结果与[]进行比较
db.test.aggregate([
{$project: {
isFruit: { $ne: [[], { $filter: {
input: ['apple', 'banana'],
as: 'fruit',
cond: { $eq: ['$$fruit', '$type'] }
}}]}
}}
])请注意,在MongoDB 3.2中添加了$filter。
https://stackoverflow.com/questions/39606208
复制相似问题