这段时间遇到了一个业务需求,我有一些关于书籍的文档数据存储在MongoDB数据中,然后在修复数据之后,需要用一个查询去验证更新是否成功。
书籍数据大概长这个样子:
{
"books": [
{
"name": "UbuntuMeta",
"sku_id": "101",
"price": 30.5
},
{
"name": "freePHP",
"sku_id": "102",
"price": 75.5
}
],
"best_books": [
{
"name": "UbuntuMeta",
"sku_id": "101",
"price": 30.5
}
]
}
···
我想根据sku_id查询到在best_kooks里面元素但不再books里面的元素,根据思考我写出如下的查询语句:
···
$expr: {
$not: {
$setIsSubset: ['$best_books.sku_id', '$books.sku_id']
}
}
但是会出现如下报错:
Reason: error while multiplanner was selecting best plan :: caused by :: both operands of $setIsSubset must be arrays. First argument is of type: missing
这是因为best_books可能不存在或者不是array类型,所以需要增加一个条件:
{ $expr: { $eq: [{ $type: "$best_books" }, "array"] } },
标签:sku,MongoDB,price,奇妙,查询,books,id,best
From: https://www.cnblogs.com/freephp/p/18124970