我在mongoDB中有一个对象列表
[{" name":" Joy"," age":23},{" name":" Nick& #34;,"年龄":26},{ " name":" Merry"," age":27},{" name":" Ben",& #34;年龄":20}]
我需要一个应该有年龄列表的结果,如
年龄:[23,26,27,20]
实际上这些对象只是一个例子。我正在使用一些不同的对象,我可以在$ push之后使用$ group来获得结果但是我们可以在没有$ group的情况下得到这个结果吗?
答案 0 :(得分:0)
如果“对象”是指MongoDB文档,那么您可以使用db.collection.aggregate([
{
$group: {
_id: null,
ages: { $push: "$age" }
}
}
])
分组技巧,这将基本上将所有文档合并为一个结果:
{ "_id" : null, "ages" : [ 23, 26, 27, 20 ] }
您将获得以下结果:
# loop forever until they confirm their choice
while True:
variable = input("Make a choice between a, b, c or d. ")
while variable not in ("a","b","c","d"):
variable = input("Make a correct choice. ")
confirm = input("You entered %s. Is this correct?" % variable)
if confirm == "yes":
break