SQL版本:
select a.id, a.name, a.xxx
from A a
where exists (select 1 from B b where b.flag=0 and b.pid = a.id)
and a.name like '%xx%'
兩個集合,分別都是百萬,甚至千萬資料;請問MongoDB中如何實作以上需求?
看網上說mongodb都是扁平設計,不適合做這種關聯查詢;那這種需求又是如何實作的呢?
例如:A是用戶表,B是訂單表
想要查詢在【10月1日至10月3日,購買了移動硬碟的用戶】(訂單表),且【用戶屬地在北京】(用戶表),最侄訓傳的結果,只要 用戶表的欄位
Oracle中已磁區、建立索引,但查詢效率還是很低。
uj5u.com熱心網友回復:
mongodb 不適合做關系查詢uj5u.com熱心網友回復:
使用嵌套檔案的方式,不知道效率如何。
db.test1.aggregate([
{$match: {"users.account":"user5"} },
{$project: {id:1, name:1}}
])
uj5u.com熱心網友回復:
關系資料建議還是存盤到關系型資料庫在關系資料庫中,這個查詢性能差主要在于你的 LIKE 吧,如果不是 LIKE 查詢,這種需求是很快的
uj5u.com熱心網友回復:
db.a.insertMany([ {id:1, name:'xx1'}, {id:2, name:'xx2'}, {id:3, name:'aa1'}, {id:4, name:'aa2'} ])
db.b.insertMany([ {id:1, pid:1, flag:0}, {id:2, pid:2, flag:1}, {id:3, pid:3, flag:0}, {id:4, pid:4, flag:1} ])
db.a.aggregate([
{$match:{ name:/xx/ }},
{$lookup:{
from:'b', as:'b_doc',
let:{ id:'$id' },
pipeline:[
{$match:{ flag:0, $expr:{$eq:['$pid', '$$id']} }}
]
}},
{$match:{ 'b_doc._id':{$exists:true} }},
{$project:{ _id:0, b_doc:0 }}
])
uj5u.com熱心網友回復:
新建地區索引,索引的id包含能區別地區的特征字符檢索時
1 掃描索引獲得實際資料的id,
2 用得到的id去取資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/43457.html
標籤:MongoDB
下一篇:flask的g物件
