我有兩個系列,
Collection journalists: {
_id: ObjectId("6125f93f5fb88535beecd8ee")
journalist_FirstName: Shyam,
journalist_LastName: Kapoor,
.........
_id: ObjectId("6125f26b5fb88535beecd8c7"),
journalist_FirstName: Kabita
journalist_LastName: Mohan
}
Collection Stories: {
_id: ObjectId("5ec3ca7fa248314329a5a287"),
journalistId:ObjectId("6125f26b5fb88535beecd8c7"), //local id to journalists collection
storyTitle: "Joining of two tables",
briefDescription: "MongoDB joining of tables",
.....
}
現在,如果有人只是在搜索欄位中輸入“Kabita”,那么我的查詢應該顯示帶有標題的故事,因為“Kabita”是負責創建這個故事的記者。
“加入兩個表”
MongoDB 加入表
發布日期:2021 年 11 月 21 日
我的搜索查詢需要考慮與記者收藏中的任何記者的姓名相匹配的任何詞,以及來自故事集的故事標題或描述的任何詞。
在此先感謝并等待合適的答復。
uj5u.com熱心網友回復:
一個簡單$lookup的aggregation將完成你的作業。你可以在這里測驗它。
db.journalists.aggregate([
{
"$lookup": {
"from": "Stories",
"localField": "_id",
"foreignField": "journalistId",
"as": "stories_docs"
}
}
])
然后,你必須separe每個故事DOC執行$match與$unwind
{
"$unwind": "$stories_docs"
}
在這一步之后,您可以添加一個$match階段來查找您需要的資訊,如果您必須在所有這些欄位中查找,我將使用 a$regex但查詢不會非常有效...
{
$match: {
$or: [
{
"journalist_FirstName": {
"$regex": "tables",
"$options": "i"
}
},
{
"stories_docs.briefDescription": {
"$regex": "tables",
"$options": "i"
}
},
{
"stories_docs.storyTitle": {
"$regex": "tables",
"$options": "i"
}
},
]
}
},
{
"$match": {
"stories_docs.keywordName": {
"$regex": "Heritage",
"$options": "i"
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/374324.html
上一篇:T-SQL::加入sys.masked_columns回傳表中的所有列
下一篇:mysqlgroupby并加入
