I am currently creating a request, but I can not.
So there is one the document of 1 user:
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"email" : "flarize.a473@gmail.com",
"password" : "$2a$10$eYeOtEkEUyD7TFkjKvhZOuSSpvBolkL17TrPHuoHhOT8JrsQR0UKW",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540713424488
}
]
And my query:
db.users.aggregate([{
$match:{
search: "flarize"
}},{
$lookup:{
from: "users",
let:{friendId:"$friend.id"},
pipeline:[{
$match:{
$expr:{
$in:["$_id","$$friendId"]
}
}},{
$limit:10},{
$skip:0},{
$project: {
name: 1,
search:1,
desc:1,
friend:1,
date:1,
banner:1,
profil:1,
color: 1
}
}],
as:"friends"}},{
$project:{
profil:1,
search:1,
name:1,
profile:1,
banner:1,
color:1,
date:1,
desc:1,
friend:1,
friends:1
}
}]).pretty();
I got this result:
{
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540713424488
}
],
"friends" : [
{
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540713424488
}
]
}
]
}
It's not really what I want.I would like Compare the document of the selection person, and the one who requests the query if the id of the person selected is present in friend then is_friend will be equal to true otherwise it will be equal to false.
I try this think:
db.users.aggregate([{
$match:{
search: "flarize"
}},{
$lookup:{
from: "users",
let:{friendId:"$friend.id"},
pipeline:[{
$match:{
$expr:{
$in:["$_id","$$friendId"]
}
}},{
$limit:10},{
$skip:0},{
$project: {
name: 1,
search:1,
desc:1,
friend:1,
date:1,
banner:1,
profil:1,
color: 1
is_friend:{
$lookup:{
from:"users",
let:{friendIdIs:"$_id"},
pipeline:[{
$match:{
$expr:{
$and:[{
$in:["$$friendIdIs", "$friend.is"]},{
$eq:["_id", ObjectId("5bd22f28f77cfb1f6ce503ca")]
}]
}
}
}],
as:"yes"}}
}
}
}],
as:"friends"}},{
$project:{
profil:1,
search:1,
name:1,
profile:1,
banner:1,
color:1,
date:1,
desc:1,
friend:1,
friends:1
}
}]).pretty();
But it's don't work.
Thank you for helping me