I have 2 collections in mongoDB:
db.getCollection('orderTest').insert([
{"id":1,"code":"abc","atrub1":"value1","atrub2":"value2","atrub100":"value100g"},
{"id":2,"code":"abc","atrub1":"value11","atrub2":"value42","atrub100":"value100gr"},
{"id":3,"code":"efg","atrub1":"value111","atrub2":"value22","atrub100":"value100g45"},
{"id":4,"code":"fff","atrub1":"value123","atrub2":"valuer322","atrub100":"value100g45r"},
{"id":5,"code":"fff","atrub1":"value42","atrub2":"valuer2","atrub100":"value100gf45r"},
{"id":6,"code":"abc","atrub1":"value4321","atrub2":"valueq2","atrub100":"value100re"},
])
db.getCollection('orderTag').insert([
{"id":1,"code":"abc","tags":["a","b","c","d"]},
{"id":2,"code":"efg","tags":["a","c","g","f"]},
])
I want to merge 2 in 1 and 'orderTag' collection only need 'tags' and search by "tags" where include "b": I expect result is:
[
{"id":1,"code":"abc","atrub1":"value1","atrub2":"value2","atrub100":"value100g","tags":["a","b","c","d"]},
{"id":2,"code":"abc","atrub1":"value11","atrub2":"value42","atrub100":"value100gr","tags":["a","b","c","d"]},
{"id":6,"code":"abc","atrub1":"value4321","atrub2":"valueq2","atrub100":"value100re","tags":["a","b","c","d"]},
]
I try it:
db.getCollection('orderTest').aggregate([
{
$lookup: {
from: "orderTag",
localField: "code",
foreignField: "code",
as: "fromItems"
}
},
{ $match: {
$and: [
{'fromItems.tags':'b'},
]}}
])
but 'tags' is so deep, It's not my expect result. How can I do?