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吗。。