我当前正在创建请求,但不能。
因此,一个用户的文档只有一个:
"_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
}
]
我的查询:
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();
我得到了这个结果:
{
"_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
}
]
}
]
}
这不是我真正想要的。我想比较选择人员的文档,如果要求的人的ID是否存在于好友中,则请求查询的人is_friend将等于true,否则它将是等于false。
我尝试这种想法:
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();
但这是行不通的。
感谢您对我的帮助