How to find items in a collections which are not in another collection with MongoDB

你离开我真会死。 提交于 2019-12-13 01:09:43

问题


I want to query my mongodb to perform a non-match between 2 collections. Here is my structure :

CollectionA :

_id, name, firstname, website_account_key, email, status

CollectionB :

_id, website_account_key, lifestage, category, target, flag_sistirt

I'am trying to find Items in B, for which, there is no lines in A (website_account_key is unique and allows to find elements in B for each A [one-to-one])

I tried to do :

dataA_ids = db.dataA.find().map(function(a){return a.website_account_key;})

db.dataB.find( { website_account_key: { $nin: [dataA_ids] } } )

But i have nothing unless, i add some test data who are supposed to work :(

Here is whole code of my test :

db.products.insert( { item: "card", qty: 15, code:100 } )
db.products.insert( { item: "pass", qty: 15, code:230 } )
db.products.insert( { item: "badge", qty: 15, code:543 } )
db.products.insert( { item: "passX", qty: 15, code:876 } )

db.references.insert( { code:230, ref:"AAZRT"})
db.references.insert( { code:888, ref:"RUBFE"})
db.references.insert( { code:876, ref:"ERHAA"})
db.references.insert( { code:120, ref:"DRETAR"})

codeInProducts = db.products.find().map(function(a){return a.code;})
db.references.find( { code: { $nin: [codeInProducts] } } )

My goal is to retrieves lines in references for which i dont find elements in products (lines in references not found in products (code is link))

来源:https://stackoverflow.com/questions/29076006/how-to-find-items-in-a-collections-which-are-not-in-another-collection-with-mong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!