Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 使用$slice时使用数组长度_Mongodb_Mongoose - Fatal编程技术网

Mongodb 使用$slice时使用数组长度

Mongodb 使用$slice时使用数组长度,mongodb,mongoose,Mongodb,Mongoose,是否可以获取数组posts的长度,并在$slice操作中使用该长度 Feed.find( { owner: { $in: joinedFeeds } }, { posts: { $slice: [<length>, fetcher] } } ) Feed.find( {所有者:{$in:joinedFeeds}}, {posts:{$slice:[,fetcher]} ) 我的目标是每

是否可以获取数组
posts
的长度,并在
$slice
操作中使用该长度

  Feed.find(
                { owner: { $in: joinedFeeds } },
                { posts: { $slice: [<length>, fetcher] } }
              )
Feed.find(
{所有者:{$in:joinedFeeds}},
{posts:{$slice:[,fetcher]}
)

我的目标是每次调用路由时获取一定数量的帖子。因此,我想取数组的最后一个片段

这是我收集的数据:

db.test.insert([
{ "_id" : 1, "item" : "ABC1", "description" : "product 1", colors: [ "blue", "black", "red" ] ,'fav':["1","2"]},
{ "_id" : 2, "item" : "ABC2", "description" : "product 2", colors: [ "purple" ],'fav':[] }
])
请检查以下查询,该查询将根据该记录的“fav”数组长度从“colors”数组中提取元素

db.test.aggregate([
   {
      $project: {
         finalTotal: {
            $let: {
               vars: {
                  numberOfColors: { $cond: { if: { $isArray: "$fav" }, then: { $size: "$fav" }, else: "NA"} }
               },
               in: { $slice: [ "$colors", "$$numberOfColors" ] }
            }
         }
      }
   }

] )

请让我知道这是否有帮助,如果您在实现时遇到任何问题,请告诉我。

是否要使用数组的大小来代替,那么我猜您得到的是空数组!!你的帖子看起来怎么样?你能提供你想要的o/p吗。。