在Mongo数据库中,我有一个收藏店。我想做一个嵌套查询,看起来我似乎无法做到。
db.store.find()
这将返回所有集合。
现在每个商店都有商品
我将如何获得所有物品。不仅是一个商店中的所有商品,还包括每个商店的所有商品。
db.store.find(item:{i don't want a query i want them all})
问题是我不想对我想要的所有项目进行查询 感谢您非常感谢的输入
编辑嗯?我所尝试的是
当我输入db.store.find()
时 "_id" : ObjectId("52695741d6b0a8701a0000ad"),
"name" : "Non Dui Nec LLP",
"code" : "YFY37YEQ0VZ",
"url" : "www.txvhmjkcyirjmb.com",
"facebook_link" : "http://facebook.com/qhhonsm",
"twitter_link" : "http://twitter.com/sqwmpbcqx",
"items" : [
{
"user_id" : 6,
"code" : "HKS29TGT1QE",
"name" : "Lorem Donec LLP",
"description" : "adipiscing elit. Etiam laoreet, libero
et tristique pellentesque, tellus sem mollis dui, in sodales elit erat vitae ris
us. Duis a mi fringilla mi lacinia mattis. Integer eu lacus. Quisque imperdiet,
erat nonummy ultricies ornare, elit elit fermentum risus, at fringilla purus mau
ris a nunc. In at pede. Cras",
"featured" : "false",
"price" : 22278,
"quantity" : 2,
"views" : 229917,
"category_id" : 6,
"brand" : "Metus Sit Amet Inc.",
"rating" : 2,
"sale" : "true",
"sale_price" : 49437,
"online" : "true",
"store" : "true"
},
{
"user_id" : 6,
"code" : "UXS08MIR6ZV",
"name" : "Quisque Inc.",
"description" : "amet nulla. Donec non justo. Proin non
massa non ante bibendum ullamcorper. Duis cursus, diam at pretium aliquet,",
"featured" : "false",
"price" : 42511,
"quantity" : 8,
"views" : 372823,
"category_id" : 3,
"brand" : "Amet Metus PC",
"rating" : 5,
"sale" : "true",
"sale_price" : 31672,
"online" : "true",
"store" : "false"
},
{
"user_id" : 2,
"code" : "DBK19WNP4MK",
"name" : "Mauris Molestie Pharetra PC",
"description" : "volutpat. Nulla facilisis. Suspendisse
commodo tincidunt nibh. Phasellus nulla. Integer vulputate, risus a ultricies ad
ipiscing, enim mi tempor lorem, eget mollis lectus pede et risus. Quisque libero
lacus, varius et, euismod et, commodo at, libero. Morbi accumsan laoreet ipsum.
Curabitur consequat, lectus sit amet luctus vulputate, nisi sem semper",
"featured" : "true",
"price" : 39186,
"quantity" : 7,
"views" : 343691,
"category_id" : 5,
"brand" : "Malesuada LLP",
"rating" : 5,
"sale" : "false",
"sale_price" : 2929,
"online" : "true",
"store" : "true"
},
{
"user_id" : 2,
"code" : "VGT01QNU1AE",
"name" : "Amet Ornare Lectus Associates",
"description" : "ipsum. Phasellus vitae mauris sit amet
lorem semper auctor. Mauris vel turpis. Aliquam adipiscing lobortis risus. In mi
pede, nonummy ut, molestie in, tempus eu, ligula. Aenean euismod mauris eu elit
. Nulla facilisi. Sed neque. Sed eget lacus. Mauris non dui nec urna suscipit no
nummy.",
"featured" : "true",
"price" : 25581,
"quantity" : 9,
"views" : 237448,
"category_id" : 1,
"brand" : "Risus Nulla Eget LLP",
"rating" : 5,
"sale" : "false",
"sale_price" : 25998,
"online" : "true",
"store" : "false"
},
{
"user_id" : 8,
"code" : "XYT91KLX5LC",
"name" : "Nunc Nulla Consulting",
"description" : "posuere vulputate, lacus. Cras interdum
. Nunc sollicitudin commodo ipsum. Suspendisse non leo. Vivamus nibh dolor, nonu
mmy ac, feugiat non, lobortis quis, pede.",
"featured" : "true",
"price" : 55233,
"quantity" : 8,
"views" : 102238,
"category_id" : 4,
"brand" : "Diam Eu Dolor Company",
"rating" : 4,
"sale" : "true",
"sale_price" : 28494,
"online" : "false",
"store" : "true"
},
{
"user_id" : 3,
"code" : "QGD53OAH0LJ",
"name" : "Tempus Eu Incorporated",
"description" : "non enim. Mauris quis turpis vitae puru
s gravida sagittis. Duis gravida. Praesent eu nulla at sem molestie sodales. Mau
ris blandit enim consequat purus. Maecenas libero est, congue a, aliquet vel, vu
lputate eu, odio. Phasellus at augue id ante dictum cursus. Nunc mauris elit,",
"featured" : "false",
"price" : 36669,
"quantity" : 10,
"views" : 129981,
"category_id" : 6,
"brand" : "Felis Purus Ac Company",
"rating" : 2,
"sale" : "false",
"sale_price" : 28753,
"online" : "true",
"store" : "true"
},
{
"user_id" : 9,
"code" : "NCI04XCU9OE",
"name" : "Ipsum Suspendisse Ltd",
"description" : "luctus vulputate, nisi sem semper erat,
in consectetuer ipsum nunc id enim. Curabitur massa. Vestibulum accumsan neque
et nunc. Quisque ornare tortor",
"featured" : "true",
"price" : 67414,
"quantity" : 9,
"views" : 402667,
"category_id" : 4,
"brand" : "Quisque Porttitor PC",
"rating" : 5,
"sale" : "false",
"sale_price" : 10481,
"online" : "true",
"store" : "false"
}
]
}
{
"_id" : ObjectId("52695741d6b0a8701a0000ae"),
"name" : "Urna Institute",
"code" : "YRH20JFJ0PW",
"url" : "www.cfejurcgjtdvjf.com",
"facebook_link" : "http://facebook.com/yzcbixz",
"twitter_link" : "http://twitter.com/aehbnnloc",
"items" : [
{
"user_id" : 1,
"code" : "VEI96FAL1OQ",
"name" : "Ornare Facilisis Eget Corp.",
"description" : "arcu. Vestibulum ante ipsum primis in f
aucibus orci luctus et ultrices posuere cubilia Curae; Phasellus ornare. Fusce m
ollis. Duis sit amet diam eu dolor egestas rhoncus. Proin nisl sem, consequat ne
c, mollis vitae, posuere at, velit. Cras lorem lorem, luctus ut, pellentesque eg
et, dictum placerat, augue. Sed molestie. Sed id",
"featured" : "true",
"price" : 67040,
"quantity" : 1,
"views" : 231128,
"category_id" : 2,
"brand" : "Sed Nec Consulting",
"rating" : 3,
"sale" : "false",
"sale_price" : 7198,
"online" : "false",
"store" : "true"
},
{
"user_id" : 3,
"code" : "JKT20ZIN1WE",
"name" : "Nulla Tempor Augue Incorporated",
"description" : "eleifend. Cras sed leo. Cras vehicula a
liquet libero. Integer in magna. Phasellus dolor elit, pellentesque a, facilisis
non, bibendum sed, est. Nunc laoreet lectus quis massa. Mauris vestibulum, nequ
e sed dictum eleifend, nunc risus varius orci, in consequat enim diam vel arcu.
Curabitur ut",
"featured" : "true",
"price" : 51700,
"quantity" : 6,
"views" : 195004,
"category_id" : 5,
"brand" : "Mauris Institute",
"rating" : 4,
"sale" : "false",
"sale_price" : 44753,
"online" : "false",
"store" : "true"
},
{
"user_id" : 2,
"code" : "HNJ55IWD4TH",
"name" : "Ac Feugiat Limited",
"description" : "tristique ac, eleifend vitae, erat. Viv
amus nisi. Mauris nulla. Integer urna. Vivamus molestie dapibus ligula. Aliquam
erat",
"featured" : "false",
"price" : 70134,
"quantity" : 9,
"views" : 440143,
"category_id" : 4,
"brand" : "Eu Incorporated",
"rating" : 4,
"sale" : "false",
"sale_price" : 8213,
"online" : "false",
"store" : "false"
},
{
"user_id" : 3,
"code" : "OUZ13HAN7UV",
"name" : "At LLC",
"description" : "nonummy ipsum non arcu. Vivamus sit ame
t risus. Donec egestas. Aliquam nec enim. Nunc ut erat. Sed nunc est, mollis non
, cursus",
"featured" : "true",
"price" : 1468,
"quantity" : 10,
"views" : 371808,
"category_id" : 4,
"brand" : "Venenatis Associates",
"rating" : 4,
"sale" : "false",
"sale_price" : 48312,
"online" : "true",
"store" : "false"
},
{
"user_id" : 3,
"code" : "BAK14SFY4ZT",
"name" : "Consectetuer Euismod Corp.",
"description" : "Etiam ligula tortor, dictum eu, placera
t eget, venenatis a, magna. Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Etiam laoreet, libero et tristique pellentesque, tellus sem mollis dui, in
sodales elit erat vitae risus. Duis a mi fringilla mi lacinia mattis. Integer e
u lacus. Quisque imperdiet, erat nonummy",
"featured" : "true",
"price" : 95615,
"quantity" : 8,
"views" : 365175,
"category_id" : 3,
"brand" : "Donec Nibh Enim Incorporated",
"rating" : 3,
"sale" : "true",
"sale_price" : 12858,
"online" : "false",
"store" : "false"
},
{
"user_id" : 6,
"code" : "FNF49YPX4PQ",
"name" : "Morbi Institute",
"description" : "sit amet risus. Donec egestas. Aliquam
nec enim. Nunc ut erat. Sed nunc est, mollis non, cursus non, egestas a, dui. Cr
as pellentesque. Sed dictum. Proin eget odio. Aliquam vulputate ullamcorper magn
a. Sed eu eros. Nam consequat dolor vitae dolor. Donec fringilla. Donec feugiat
metus sit amet ante. Vivamus non",
"featured" : "true",
"price" : 94975,
"quantity" : 9,
"views" : 243181,
"category_id" : 3,
"brand" : "Elementum Purus Foundation",
"rating" : 1,
"sale" : "true",
"sale_price" : 49337,
"online" : "true",
"store" : "true"
},
{
"user_id" : 5,
"code" : "CAQ49OCW9SN",
"name" : "Sit Incorporated",
"description" : "Nunc ut erat. Sed nunc est, mollis non,
cursus non, egestas",
"featured" : "false",
"price" : 98576,
"quantity" : 10,
"views" : 265153,
"category_id" : 3,
"brand" : "Fringilla Euismod Corporation",
"rating" : 5,
"sale" : "false",
"sale_price" : 7037,
"online" : "false",
"store" : "false"
}
]
}
我希望得到所有商店都有的所有商品。我不确定如何更好地解释 但我想在所有商店买到所有商品。如果仍然难以理解,请告诉我。我可以发布更多例子
答案 0 :(得分:2)
你的意思是这样的:
db.store.aggregate([
{$unwind: "$items"},
{$project: {
"user_id" : "$items.user_id",
"code" : "$items.code",
"name" : "$items.name",
"description" : "$items.description",
"featured" : "$items.featured",
"quantity" : "$items.quantity",
"views" : "$items.views",
"category_id" : "$items.category_id",
"brand" : "$items.brand",
"rating" : "$items.rating",
"sale" : "$items.sale",
"sale_price" : "$items.sale_price",
"online" : "$items.online",
"store" : "$items.store",
"_id": 0
}}
])
{$unwind: "$items"}
为items
数组中的每个子文档创建单独的文档。它会给你这样的数组:
[
{
"_id" : ObjectId("52693d98e7792c81a7f4e5ad"),
"items" : {
"user_id" : 6,
"code" : "HKS29TGT1QE",
...
}
... //Other fields
},
{
"_id" : ObjectId("52693d98e7792c81a7f4e5ad"),
"items" : {
"user_id" : 6,
"code" : "UXS08MIR6ZV",
"name" : "Quisque Inc.",
...
}
... //Other fields
}
]
其中"_id"
是父文档的_id
。您可以使用{$project: {...}}
删除_id
并从items
子文档中提取有趣的字段。
修改强>
您可以在管道的开头使用简单的project
来减少下游文档的大小。
db.store.aggregate([
{$project: {"items": 1}},
{$unwind: "$items"},
{$project: {
"user_id" : "$items.user_id",
...
}}
]}