Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
ObjectId函数对象id长度无效,使用Mongodb 3.4.15时$project出现错误_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

ObjectId函数对象id长度无效,使用Mongodb 3.4.15时$project出现错误

ObjectId函数对象id长度无效,使用Mongodb 3.4.15时$project出现错误,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,这是我的聚合 db.getCollection("entities").aggregate([ {$match : {...omissis..}, {$project : {ancestors : 1}}, {$unwind: "$ancestors"}]); 结果是 { "_id" : ObjectId("5b855ffb17285c29501dd801"), "ancestors" : "5c62ef9b8521e37b80517583" }

这是我的聚合

db.getCollection("entities").aggregate([
    {$match : {...omissis..},
    {$project : {ancestors : 1}},  
    {$unwind: "$ancestors"}]);
结果是

{ 
    "_id" : ObjectId("5b855ffb17285c29501dd801"), 
    "ancestors" : "5c62ef9b8521e37b80517583"
},
{ 
    "_id" : ObjectId("5b8537d3571c4f3e3c0dcf54"), 
    "ancestors" : "5c75565b3e44853a18cc9d11"
}
我想将祖先字符串转换为
ObjectId
。 我做了很多测试,最后一个是添加这个管道

{$project: {
             result : {
                    "$let" : {
                              "vars" : { "id" : "$ancestors" },
                              "in" : ObjectId('$$id')
                             }
                       }
           } 
 }
我做的每一次测试都有同样的错误

错误:无效的对象id:长度:

祖先字符串是有效的
ObjectId
,我不知道如何解决这个错误


我知道Mongodb 4.0中有新的命令,但这个项目仍然使用3.4.15。

我使用
游标
javascript
来实现这一点。请试试这个

db.getCollection("entities").aggregate([
    {$match : {...omissis..},
    {$project : {ancestors : 1}},  
    {$unwind: "$ancestors"}]).forEach(function(doc){
    doc.ancestors = ObjectId(doc.ancestors)
    print(doc);
    })

它起作用了!你知道是否有办法继续使用其他管道吗?我尝试了很多其他选择,但这是唯一可行的选择是的,这是更好的选择