MongoDB - Lookup with pipeline get error: $nin needs an array
MongoDB - Lookup with pipeline get error: $nin needs an array
大家好,我遇到了一些麻烦,基本上我正在接受
$nin needs an array
即使在传递了一个数组之后,但在尝试 运行 以下查询时使用了一个变量。
我想从包含 属性 value 的集合 numberData 中获取文档,该值未分配给数组在另一个名为 assignedData.
的集合中
numbersData 文档示例如下:
/* 1 */
{
"_id" : ObjectId("61f2b5923327b3f6f4dad27e"),
"value" : 1.0
}
/* 2 */
{
"_id" : ObjectId("61f2b5923327b3f6f4dad27f"),
"value" : 2.0
}
/* 3 */
{
"_id" : ObjectId("61f2b5923327b3f6f4dad280"),
"value" : 3.0
}
/* 4 */
{
"_id" : ObjectId("61f2b5923327b3f6f4dad281"),
"value" : 4.0
}
和assignedData的内容如下:
/* 1 */
{
"_id" : ObjectId("61f2b5e43327b3f6f4dad282"),
"name" : "john",
"assigned" : [
1.0,
4.0
]
}
我使用的查询:
db.assignedData.aggregate([
{
//i only want to get single doc from assigned data
//so using this match
$match: {
_id: ObjectId("61f2b5e43327b3f6f4dad282")
},
},
{
$lookup: {
from: 'numbersData',
let: { aArr: "$assigned" },
pipeline: [
{
$match: {
//here i am using $$maArr
//which is refering to assigned array
value:
{
$nin: "$$aArr"
}
}
}
],
as: 'matchData'
}
}
])
在 运行 执行此查询后我得到了错误:
$nin needs an array
我不知道为什么,如果可以的话请给我建议解决方案。
来自Join Conditions and Subqueries on a Joined Collection部分(参考table),
let
A $match stage requires the use of an $expr operator to access the variables. The $expr operator allows the use of aggregation expressions inside of the $match syntax.
不使用 $nin
运算符,更改如下:
{
$match: {
$expr: {
$not: {
$in: [
"$value",
"$$aArr"
]
}
}
}
}
大家好,我遇到了一些麻烦,基本上我正在接受
$nin needs an array
即使在传递了一个数组之后,但在尝试 运行 以下查询时使用了一个变量。
我想从包含 属性 value 的集合 numberData 中获取文档,该值未分配给数组在另一个名为 assignedData.
的集合中numbersData 文档示例如下:
/* 1 */
{
"_id" : ObjectId("61f2b5923327b3f6f4dad27e"),
"value" : 1.0
}
/* 2 */
{
"_id" : ObjectId("61f2b5923327b3f6f4dad27f"),
"value" : 2.0
}
/* 3 */
{
"_id" : ObjectId("61f2b5923327b3f6f4dad280"),
"value" : 3.0
}
/* 4 */
{
"_id" : ObjectId("61f2b5923327b3f6f4dad281"),
"value" : 4.0
}
和assignedData的内容如下:
/* 1 */
{
"_id" : ObjectId("61f2b5e43327b3f6f4dad282"),
"name" : "john",
"assigned" : [
1.0,
4.0
]
}
我使用的查询:
db.assignedData.aggregate([
{
//i only want to get single doc from assigned data
//so using this match
$match: {
_id: ObjectId("61f2b5e43327b3f6f4dad282")
},
},
{
$lookup: {
from: 'numbersData',
let: { aArr: "$assigned" },
pipeline: [
{
$match: {
//here i am using $$maArr
//which is refering to assigned array
value:
{
$nin: "$$aArr"
}
}
}
],
as: 'matchData'
}
}
])
在 运行 执行此查询后我得到了错误:
$nin needs an array
我不知道为什么,如果可以的话请给我建议解决方案。
来自Join Conditions and Subqueries on a Joined Collection部分(参考table),
let
A $match stage requires the use of an $expr operator to access the variables. The $expr operator allows the use of aggregation expressions inside of the $match syntax.
不使用 $nin
运算符,更改如下:
{
$match: {
$expr: {
$not: {
$in: [
"$value",
"$$aArr"
]
}
}
}
}