现在我有一个聚合管道返回以下数组:
[{
"_id": 1,
"items": {
"_id": 1,
"transactions": []
}
},
{
"_id": 2,
"items": {
"_id": 2,
"transactions": [
{
"_id": "5a536dc1bc9b2113986a9047",
"price": 5.56
},
{
"_id": "5a536e1bbc9b2113986a904e",
"price": 11.56,
}
]
}
},
{
"_id": 3,
"items": {
"_id": 1,
"transactions": []
}
}]
可以过滤字段"事务"在子文档"项目"不是空的。像这样:
[{
"_id": 2,
"items": {
"_id": 2,
"transactions": [
{
"_id": "5a536dc1bc9b2113986a9047",
"price": 5.56
},
{
"_id": "5a536e1bbc9b2113986a904e",
"price": 11.56,
}
]
}
}]
汇总管道不是用于查询
答案 0 :(得分:0)
在聚合管道中,最后添加import pygame as pg
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
white = pg.Color('white')
x_change = 0
y_change = 0
x = 400
y = 400
# Load the images before the main loop starts. You could also
# do this in the global scope.
right_images = []
for img_name in ['knight_right.png','knight_right1.png','knight_right2.png']:
right_images.append(pg.image.load(img_name).convert_alpha())
left_images = []
for img_name in ['knight_left.png', 'knight_left1.png', 'knight_left2.png']:
left_images.append(pg.image.load(img_name).convert_alpha())
# This variable refers to the currently selected image list.
active_images = left_images
counter = 0 # There's no need for a second counter.
player = active_images[counter] # Currently active animation frame.
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
x_change = 5
active_images = right_images # Swap the image list.
elif event.key == pg.K_a:
x_change = -5
active_images = left_images # Swap the image list.
elif event.key == pg.K_w:
y_change -= 5
elif event.key == pg.K_s:
y_change += 5
elif event.type == pg.KEYUP:
if event.key == pg.K_d and x_change > 0:
x_change = 0
counter = 0 # Switch to idle pose.
player = active_images[counter]
elif event.key == pg.K_a and x_change < 0:
x_change = 0
counter = 0 # Switch to idle pose.
player = active_images[counter]
elif event.key == pg.K_w and y_change < 0:
y_change = 0
elif event.key == pg.K_s and y_change > 0:
y_change = 0
if x_change != 0: # If moving.
# Increment the counter and keep it in the correct range.
counter = (counter+1) % len(active_images)
player = active_images[counter] # Swap the image.
x += x_change
y += y_change
screen.fill(white)
screen.blit(player, (x, y))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
管道以根据事务数组大小进行过滤
$match
输出
> db.foo.aggregate([{$match : {"items.transactions" : { $gt : {$size : 0}}}}]).pretty()
示例文件
{
"_id" : 2,
"items" : {
"_id" : 2,
"transactions" : [
{
"_id" : "5a536dc1bc9b2113986a9047",
"price" : 5.56
},
{
"_id" : "5a536e1bbc9b2113986a904e",
"price" : 11.56
}
]
}
}
>