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

I want to request my mongodb to perform a mismatch between the two 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 elements in B for which there are no lines in (site_account_key is unique and allows finding 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 don’t have anything unless I add some test data that should work :(

Here is the 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 extract strings in links for which I cannot find elements in products (strings in links not found in products (code - link))

+1
source share

Source: https://habr.com/ru/post/1589530/


All Articles