Hi guys, I’m quite new to MongoDB. I read a blog post about best practices in designing schema in MongoDB. One way to handle one-to-many relationship is to reference it and this is the example from the blog post.
try localField:"parts" and foreignField:"_id" and see if it resolves correct.
if not, from your given data, it is possible your parts array does not hold actual IDs but just some strings that look like some ID. "ObjectID('AAA')" is a string as is it surrounded by double-quotes.
PS: I believe the author of that blog post has typos for some reason. He uses "_id": "ObjectId('AAA')" for a while and then "_id": ObjectID("AAAB"). note the double quotes around ObjectId function. The first one is just a string but the second is the representation of an Id value.
If you like to watch video courses along with written ones, you may want to watch this MongoDB Schema Design Best Practices video from that very same author on that very same topic explained in the blog post.
you need to be careful with opening and closing of curly and square brackets. square brackets [] are for arrays, curlies {} are for objects. your $lookup is inside the brackets of $match hence the error “must contain one field”
each pipeline is a separate object, so it should look like this:
[
{ $match: {...} },
{ $lookup: {...} }
]
PS: do not forget ObjectID('AAAA') is an object ID, but "ObjectID('AAAA')" is not. the second is in double quotes making it a string.
I have made a mistake. I have forgotten a feature of mongodb
“ObjectID(‘AAAA’)” is not normally an object id in the sense of an id mongodb uses; it is a value represented by 24 char hexadecimal value such as “5d505646cf6d4fe581014ab2”. but you can still overwrite _id field with something else like a number or a string.
if take those 2 data from the blog post and put them into a test database, 1 for products and 1 for parts, you will see the product will get assigned a proper _id but the part will have "ObjectID('AAAA')" as its id.
however, the $lookup will still hold true and fill in the details of the part into the product as it also stores that string in the parts array.
also, beware if you use as:"parts", you will override the original parts array with this new filled-in details array.