如何从子记录数组中的项目中进行$查找?
好的,我在这个MongoDB世界中是新手,我对此有一些麻烦。
我有3个收藏品,产品,评论和用户。 产品存储来自注释中的objectid数组,评论存储user_id。
这是产品文档示例。
{
"_id": {
"$oid": "625f1825fd0569646907dbb2"
},
"id": {
"$numberInt": "1"
},
"title": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
"price": {
"$numberDouble": "109.95"
},
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg",
"rating": {
"rate": {
"$numberDouble": "3.9"
},
"count": {
"$numberInt": "120"
}
},
"comments": [
{
"id": {
"$oid": "62607a918aaf3e14a2cd1f0e"
}
}
]
}
这是评论示例
{
"_id": {
"$oid": "62607a918aaf3e14a2cd1f0e"
},
"user_id": {
"$oid": "625f1ae4b0e88cd486276a54"
},
"comment": "asdjfaslkdf",
"product_id": {
"$oid": "625f1825fd0569646907dbb2"
},
"rating": {
"rate": {
"$numberInt": "0"
},
"votes": {
"$numberInt": "0"
}
}
}
,这是用户示例。
{
"_id": {
"$oid": "625f1ae4b0e88cd486276a54"
},
"name": "Ursula",
"username": "lahee",
"create_at": {
"$date": {
"$numberLong": "1650399972095"
}
},
"password": "$2a$10$Av3HTLljhDkDxoVEyRKGwe4M42A4HHZMWcCfThdOQHbmA3H5EGsny",
"email": "[email protected]",
"phone": "102394523",
"preferences": [],
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYyNWYxYWU0YjBlODhjZDQ4NjI3NmE1NCIsInVzZXJuYW1lIjoibGFoZWUiLCJpYXQiOjE2NTA0ODMzMzEwODUsImV4cCI6MTY1MDQ4MzMzNDY4NX0.UXV7I-1XBZ3T_Y0aPBPdP-gdkPMqvM6dEnZcHXg5_5g",
"role": "user",
"__v": {
"$numberInt": "0"
}
}
使用此文档,我想以这样的评论,用户名和电子邮件来获取所有产品:
{
"_id": {
"$oid": "625f1825fd0569646907dbb2"
},
"price": 109.95,
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"comments": [
{
"_id": {
"$oid": "62607a918aaf3e14a2cd1f0e"
},
"comment": "asdjfaslkdf",
"user": [
{
"_id": {
"$oid": "625f1ae4b0e88cd486276a54"
},
"name": "Ursula",
"email": "[email protected]"
}
]
}
]
}
我尝试了一些规则和查询,最好给我所有评论的用户。
db.products.aggregate([
{
$lookup: {
from: "comments",
let: { comment_id: "$comments.id" },
pipeline: [
{ $match: { $expr:{ $eq:[ "$_id", "$$comment_id" ] } } },
{
$lookup: {
from: "users",
localField: "user_id",
foreignField: "_id",
as: "user"
}
},
],
as: "comments"
}
},
{
$project:{
_id:1,
name:1,
price:1,
description:1,
comments:{
_id:1,
comment:1,
date_at:1,
rate:1,
user:{
_id:1,
name:1,
email:1
},
},
}
}
])
任何人都可以帮助我解决这个问题,我希望更好地理解管道的问题。
谢谢。
Ok , im new in this mongodb world an i have some troubles with it.
i have 3 collections , products , comments and users.
products store an array of objectid from comments , an comments store the user_id.
this is the product document example.
{
"_id": {
"$oid": "625f1825fd0569646907dbb2"
},
"id": {
"$numberInt": "1"
},
"title": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
"price": {
"$numberDouble": "109.95"
},
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg",
"rating": {
"rate": {
"$numberDouble": "3.9"
},
"count": {
"$numberInt": "120"
}
},
"comments": [
{
"id": {
"$oid": "62607a918aaf3e14a2cd1f0e"
}
}
]
}
this is the comments example
{
"_id": {
"$oid": "62607a918aaf3e14a2cd1f0e"
},
"user_id": {
"$oid": "625f1ae4b0e88cd486276a54"
},
"comment": "asdjfaslkdf",
"product_id": {
"$oid": "625f1825fd0569646907dbb2"
},
"rating": {
"rate": {
"$numberInt": "0"
},
"votes": {
"$numberInt": "0"
}
}
}
And this is the user example.
{
"_id": {
"$oid": "625f1ae4b0e88cd486276a54"
},
"name": "Ursula",
"username": "lahee",
"create_at": {
"$date": {
"$numberLong": "1650399972095"
}
},
"password": "$2a$10$Av3HTLljhDkDxoVEyRKGwe4M42A4HHZMWcCfThdOQHbmA3H5EGsny",
"email": "[email protected]",
"phone": "102394523",
"preferences": [],
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYyNWYxYWU0YjBlODhjZDQ4NjI3NmE1NCIsInVzZXJuYW1lIjoibGFoZWUiLCJpYXQiOjE2NTA0ODMzMzEwODUsImV4cCI6MTY1MDQ4MzMzNDY4NX0.UXV7I-1XBZ3T_Y0aPBPdP-gdkPMqvM6dEnZcHXg5_5g",
"role": "user",
"__v": {
"$numberInt": "0"
}
}
with this documents i want to get all product with comments and user name and email like this:
{
"_id": {
"$oid": "625f1825fd0569646907dbb2"
},
"price": 109.95,
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"comments": [
{
"_id": {
"$oid": "62607a918aaf3e14a2cd1f0e"
},
"comment": "asdjfaslkdf",
"user": [
{
"_id": {
"$oid": "625f1ae4b0e88cd486276a54"
},
"name": "Ursula",
"email": "[email protected]"
}
]
}
]
}
i tried with some rules and querys , the best give me the same user for all comments.
db.products.aggregate([
{
$lookup: {
from: "comments",
let: { comment_id: "$comments.id" },
pipeline: [
{ $match: { $expr:{ $eq:[ "$_id", "$comment_id" ] } } },
{
$lookup: {
from: "users",
localField: "user_id",
foreignField: "_id",
as: "user"
}
},
],
as: "comments"
}
},
{
$project:{
_id:1,
name:1,
price:1,
description:1,
comments:{
_id:1,
comment:1,
date_at:1,
rate:1,
user:{
_id:1,
name:1,
email:1
},
},
}
}
])
anyone can help me with this problem , i want understand better the pipelines an mongodb querys .
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您可以获取所需输出的一种方法。
尝试一下 mongoplayground.net.net 。
Here's one way you could get your desired output.
Try it on mongoplayground.net.