一般来说,对test
的数组然后对原始$unwind
的{{1}}而言,这是一种反模式,因为大多数操作可以在一个阶段中直接在数组上完成。这是一个这样的阶段:
$group
这是创建一个数组,其中每个元素都是两个元素的数组(这是一种语法,_id
可以转换为第一个元素是键名,第二个元素是值的对象),输入是一个唯一的一组要约,对于每个要约,我们都累积了一组产品,除去重复项(使用{$addFields:{
offers:{$arrayToObject:{
$map:{
input:{$setUnion:"$itemsPurchased.offer"},as:"o",in:[
"$$o",{$size:{$setUnion:{$let:{
vars:{items:{$filter:{
input:"$itemsPurchased",cond:{$eq:["$$this.offer","$$o"]}
}}},in:{$reduce:{
input:"$$items",initialValue:[],in:{$concatArrays:["$$value","$$items.items"]}
}}
}}}
}]
}
}}
}}
),然后得出结果的大小。这会在您的输入上产生以下结果:
$arrayToObject
,
您需要运行$unwind和$group两次。要仅计算唯一的dat <- read.csv("Bird_Dataset_2019.csv")
L_starling <- dat[dat$Species=="Starling",]
L_skylark <- dat[dat$Species=="Skylark",]
L_yellow_wagtail <- dat[dat$Species=="Yellow Wagtail",]
L_kestrel <- dat[dat$Species=="Kestrel",]
L_yellowhammer <- dat[dat$Species=="Yellowhammer",]
L_greenfinch <- dat[dat$Species=="Greenfinch",]
L_swallow <- dat[dat$Species=="Swallow",]
L_lapwing <- dat[dat$Species=="Lapwing",]
L_housemartin <- dat[dat$Species=="House Martin",]
L_linnet <- dat[dat$Species=="Linnet",]
L_greypartridge <- dat[dat$Species=="Grey Partridge",]
L_turteldove <- dat[dat$Species=="Turtle Dove",]
L_cornbunting <- dat[dat$Species=="Corn Bunting",]
L_bullfinch <- dat[dat$Species=="Bullfinch",]
L_songthrush <- dat[dat$Species=="Song Thrush",]
L_blackbird <- dat[dat$Species=="Blackbird",]
L_dunnock <- dat[dat$Species=="Dunnock",]
#code for population size and farmland occupants in years--------
years <- 1994:2013
starling_means <- vector(length = length(years))
skylark_means <- vector(length = length(years))
yellow_wagtail_means <- vector(length = length(years))
kestrel_means <- vector(length = length(years))
yellowhammer_means <- vector(length = length(years))
greenfinch_means <- vector(length = length(years))
swallow_means <- vector(length = length(years))
lapwing_means <- vector(length = length(years))
housemartin_means <- vector(length = length(years))
linnet_means <- vector(length = length(years))
greypartridge_means <- vector(length = length(years))
turtledove_means <- vector(length = length(years))
cornbunting_means <- vector(length = length(years))
bullfinch_means <- vector(length = length(years))
songthrush_means <- vector(length = length(years))
blackbird_means <- vector(length = length(years))
dunnock_means <- vector(length = length(years))
#---------------------------------------------------
for(i in 1:length(years)){
starling_means[i] <- mean(L_starling$Pop_Index[L_starling$Year==years[i]],na.rm = TRUE)
skylark_means[i] <- mean(L_skylark$Pop_Index[L_skylark$Year==years[i]],na.rm = TRUE)
yellow_wagtail_means[i] <- mean(L_yellow_wagtail$Pop_Index[L_yellow_wagtail$Year==years[i]],na.rm = TRUE)
kestrel_means[i] <- mean(L_kestrel$Pop_Index[L_kestrel$Year==years[i]],na.rm = TRUE)
yellowhammer_means[i] <- mean(L_yellowhammer$Pop_Index[L_yellowhammer$Year==years[i]],na.rm = TRUE)
greenfinch_means[i] <- mean(L_greenfinch$Pop_Index[L_greenfinch$Year==years[i]],na.rm = TRUE)
swallow_means[i] <- mean(L_swallow$Pop_Index[L_swallow$Year==years[i]],na.rum = TRUE)
lapwing_means[i] <- mean(L_lapwing$Pop_Index[L_lapwing$Year==years[i]],na.rm = TRUE)
housemartin_means[i] <- mean(L_housemartin$Pop_Index[L_housemartin$Year==years[i]],na.rm = TRUE)
linnet_means[i] <- mean(L_linnet$Pop_Index[L_linnet$Year==years[i]],na.rm = TRUE)
greypartridge_means[i] <- mean(L_greypartridge$Pop_Index[L_greypartridge$Year==years[i]],na.rm = TRUE)
turtledove_means[i] <- mean(L_turteldove$Pop_Index[L_turteldove$Year==years[i]],na.rm = TRUE)
cornbunting_means[i] <- mean(L_cornbunting$Pop_Index[L_cornbunting$Year==years[i]],na.rm = TRUE)
bullfinch_means[i] <- mean(L_bullfinch$Pop_Index[L_bullfinch$Year==years[i]],na.rm = TRUE)
songthrush_means[i] <- mean(L_songthrush$Pop_Index[L_songthrush$Year==years[i]],na.rm = TRUE)
blackbird_means[i] <- mean(L_blackbird$Pop_Index[L_blackbird$Year==years[i]],na.rm = TRUE)
dunnock_means[i] <- mean(L_dunnock$Pop_Index[L_dunnock$Year==years[i]],na.rm = TRUE)
}
# All means placed into a data.frame-----------------------------
L_population_frame <- data.frame(years,log(starling_means),log(skylark_means),yellow_wagtail_means,kestrel_means,yellowhammer_means,log(greenfinch_means),log(swallow_means),lapwing_means,housemartin_means,linnet_means,greypartridge_means,turtledove_means,cornbunting_means,bullfinch_means,songthrush_means,log(blackbird_means),dunnock_means)
colnames(L_population_frame) <- c("Years","Starling","Skylark","YellowWagtail","Kestrel","Yellowhammer","Greenfinch","Swallow","Lapwing","Housemartin","Linnet","GreyPartridge","TurtleDove","Cornbunting","Bullfinch","Songthrush","Blackbird","Dunnock")
,可以使用$addToSet。要动态构建密钥,您需要使用$arrayToObject:
items
Mongo Playground
本文链接:https://www.f2er.com/3042247.html