I would like to know how to save a part of the document, the $lookup method return for me. Here is an example:
How do I save only the abbreviation array into eOTD_term
collection?
I have two collections:
ex. doc from eOTD_abbreviation
{
"_id" : ObjectId("59bc32fd7d7934a6a7a47d08"),
"abbreviationID" : "0161-1#AB-000282#1",
"termID" : "0161-1#TM-623205#1",
"abbreviation" : "CONSTR,LGHT"
}
ex. doc from eOTD_term
{
"_id" : ObjectId("59bc2d7e7d7934a6a7777540"),
"termID" : "0161-1#TM-623205#1",
"conceptID" : "0161-1#01-082401#1",
"term" : "CONSTRUCT,LIGHT",
}
termID
is my unique key that exist in both collections.
I have tried the following:
db.eOTD_term.aggregate([
{
$lookup: {
from: "eOTD_abbreviation",
localField: "termID",
foreignField: "termID",
as: "abbreviation"
}
},
{
$match: { abbreviation: { $ne: [] } }
}
]);
And I get back (one of the docs the aggregation returns):
{emphasized text
"_id" : ObjectId("59bc2d7e7d7934a6a7777540"),
"termID" : "0161-1#TM-623205#1",
"conceptID" : "0161-1#01-082401#1",
"term" : "CONSTRUCT,LIGHT",
"abbreviation" : [ <----------- THIS ARRAY
{
"_id" : ObjectId("59bc32fd7d7934a6a7a47d08"),
"abbreviationID" : "0161-1#AB-000282#1",
"termID" : "0161-1#TM-623205#1",
"abbreviation" : "CONSTR,LGHT"
}
]
}