根据 MongoDB 中的查找值过滤数据
Filter data based on lookup value in MongoDB
假设我在 bookUser
集合中有如下文档:
{
'authored_by':ObjectId('asdfs'),
'general':{'address':{'city':'XYZ'}}
}
{
'authored_by':ObjectId('jkfo21'),
'general':{'address':{'city':'YYZ'}}
}
和另一个集合 center_data 作为:
{
'authored_by': ObjectId('asdfs'),
'books':{'name':'alpha'},
'book_type':'adventure'
'general':{'address':{'city':'ABC'}}
}
{
'authored_by': ObjectId('jkfo21'),
'books':{'name':'sigma'},
'book_type':'drama'
'general':{}
}
我想获取所有对象并从 center_data
集合中获取详细信息:
样本O/P:
[
{
'authored_by':ObjectId('asdfs'),
'general':{'address':{'city':'XYZ'}},
'books':[
{
'address': 'ABC'
}
]
},
{
'authored_by':ObjectId('jkfo21'),
'general':{'address':{'city':'YYZ'}},
'books':[
{
'address': ''
}
]
}
]
为此我尝试了 ::
db.bookUser.aggregate([
{
$lookup: {
from: "center_data",
localField: "_id",
foreignField: "authored_by",
as: "books"
}
},
{
$project: {
books: {
$filter: {
input: "$books",
as: "book",
cond: {
$and: [
{
$eq: [
"book_type",
"adventure"
]
}
]
}
}
}
}
}
])
但是在查询中,它给出了一个空数组。我想要 bookUser
集合中的所有文档并从 center_data
集合中获取地址,如果 book_type 不是冒险,则将地址填充为空。
如果有人需要任何进一步的信息,请告诉我。
担忧
bookUser
集合的文档中没有_id
字段。根据您提供的数据,localField
应该是 authored_by
。
从下面的陈述中,您比较的是“book_type”和“adventure”这两个词,它们从来都不是同一个词。
$and:[
{
$eq: ['book_type','adventure']
}
]
您需要使用$$book.book_type
来访问变量值。
解决方案
db.bookUser.aggregate([
{
$lookup: {
from: "center_data",
localField: "authored_by",
foreignField: "authored_by",
as: "books"
}
},
{
$project: {
authored_by: 1,
general: 1,
books: {
$filter: {
input: "$books",
as: "book",
cond: {
$and: [
{
$eq: [
"$$book.book_type",
"adventure"
]
}
]
}
}
}
}
}
])
假设我在 bookUser
集合中有如下文档:
{
'authored_by':ObjectId('asdfs'),
'general':{'address':{'city':'XYZ'}}
}
{
'authored_by':ObjectId('jkfo21'),
'general':{'address':{'city':'YYZ'}}
}
和另一个集合 center_data 作为:
{
'authored_by': ObjectId('asdfs'),
'books':{'name':'alpha'},
'book_type':'adventure'
'general':{'address':{'city':'ABC'}}
}
{
'authored_by': ObjectId('jkfo21'),
'books':{'name':'sigma'},
'book_type':'drama'
'general':{}
}
我想获取所有对象并从 center_data
集合中获取详细信息:
样本O/P:
[
{
'authored_by':ObjectId('asdfs'),
'general':{'address':{'city':'XYZ'}},
'books':[
{
'address': 'ABC'
}
]
},
{
'authored_by':ObjectId('jkfo21'),
'general':{'address':{'city':'YYZ'}},
'books':[
{
'address': ''
}
]
}
]
为此我尝试了 ::
db.bookUser.aggregate([
{
$lookup: {
from: "center_data",
localField: "_id",
foreignField: "authored_by",
as: "books"
}
},
{
$project: {
books: {
$filter: {
input: "$books",
as: "book",
cond: {
$and: [
{
$eq: [
"book_type",
"adventure"
]
}
]
}
}
}
}
}
])
但是在查询中,它给出了一个空数组。我想要 bookUser
集合中的所有文档并从 center_data
集合中获取地址,如果 book_type 不是冒险,则将地址填充为空。
如果有人需要任何进一步的信息,请告诉我。
担忧
bookUser
集合的文档中没有_id
字段。根据您提供的数据,localField
应该是authored_by
。从下面的陈述中,您比较的是“book_type”和“adventure”这两个词,它们从来都不是同一个词。
$and:[
{
$eq: ['book_type','adventure']
}
]
您需要使用$$book.book_type
来访问变量值。
解决方案
db.bookUser.aggregate([
{
$lookup: {
from: "center_data",
localField: "authored_by",
foreignField: "authored_by",
as: "books"
}
},
{
$project: {
authored_by: 1,
general: 1,
books: {
$filter: {
input: "$books",
as: "book",
cond: {
$and: [
{
$eq: [
"$$book.book_type",
"adventure"
]
}
]
}
}
}
}
}
])