Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb $arrayElemAt';的第一个参数必须是数组_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb $arrayElemAt';的第一个参数必须是数组

Mongodb $arrayElemAt';的第一个参数必须是数组,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我从一个收藏中取出一组唱片,加入另一个收藏 我想在投影字段中添加一个字段,但我得到一个错误代码和错误如下: db.getCollection("ConnectionEntity").aggregate( // Pipeline [ // Stage 1 { $match: { "Id":"9c06cb0c-966a-4f6b-b087-816587629079" }

我从一个收藏中取出一组唱片,加入另一个收藏

我想在投影字段中添加一个字段,但我得到一个错误代码和错误如下:

db.getCollection("ConnectionEntity").aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $match: {
                "Id":"9c06cb0c-966a-4f6b-b087-816587629079"
            }
        },

        // Stage 2
        {
            $lookup: // Equality Match
            {
                from: "Enterprise",
                localField: "EnterpriseId",
                foreignField: "_id",
                as: "joined"
            }
        },

        // Stage 3
        {
            $project: {                 
                "Enterprise": {"$arrayElemAt": ["$joined.Profile", 0]}
            }
        },

        // Stage 4 - doesn't work
        {
            $addFields: {
                "Enterprise.Id": {"$arrayElemAt": ["$joined._id", 0]}
            }
        },
    ]
);
错误:

The following error occurred while attempting to execute the aggregate query

Mongo Server error (MongoCommandException): Command failed with error 28689: '$arrayElemAt's first argument must be an array'.

The full response is:
{ 

    "_t" : "OKMongoResponse", 

    "ok" : NumberInt(0), 

    "code" : NumberInt(28689), 

    "errmsg" : "$arrayElemAt's first argument must be an array", 

    "$err" : "$arrayElemAt's first argument must be an array"

}

在您的代码中,
Stage 4
试图引用
加入的
数组,该数组被放入
Stage 3
(您仅投影
企业
字段和
\u id
)。有很多方法可以解决这个问题,例如,您可以在
阶段4
中使用
$addFields
,以保持
已加入
数组,然后将其删除:

db.ConnectionEntity.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $match: {
                "Id":"9c06cb0c-966a-4f6b-b087-816587629079"
            }
        },

        // Stage 2
        {
            $lookup: // Equality Match
            {
                from: "Enterprise",
                localField: "EnterpriseId",
                foreignField: "_id",
                as: "joined"
            }
        },

        // Stage 3
        {
            $addFields: {                 
                "Enterprise": {"$arrayElemAt": ["$joined.Profile", 0]}
            }
        },

        // Stage 4
        {
            $addFields: {
                "Enterprise.Id": {"$arrayElemAt": ["$joined._id", 0]}
            }
        },

        //get rid of joined if it's no longer useful
        {
           $project: {
               joined: 0
           }
        }
    ]
);

ConnectionEntity
背后的模式是什么?你应该发布你的收藏