Referencing other collections using _id

Hi,
This is my first post and I’m relatively new to Mongo.
I have three collections - Sources, Tags, and Excerpts.
A source is anything like a book, url, paper, etc.
An excerpt is a extract or quotation taken from a source.
A tag is simply a way of categorising Excerpts by area of interest.
When I create a new source I want to reference the Source it came from, and add Tags from the Tags collection. This is what I have so far:

db.rfmExcerpts.insertMany([
	{
		isSample: false,
		tags: [],
		owner: "d",
		title: "Rating agencies slow to react",
		pages: 26,
		body: "The ratings agencies should have been the first one to detect problems in the housing market. ",
		use: "In his book 'The Undercover Economist', (Harford, J. 2007) Joe Harford points out that etc, etc.",
		notes: "Need to find other parallels",
		source: {}
	}
]}

How do I populate the ‘tags’ field with the IDs of some specific tags I want to attach to this excerpt. for example ‘economics’ and ‘banking’ which already exist in the Tags collection, and similarly, how do I fetch the ID of the source ‘The Undercover Economist’ which I know exists in the Source table.

Any help greatly appreciated, thank you.

Hi @Daniel_McLoughlin,

I think what you need is to query the ids from the collection and use $addToSet update to push them to the arrays.

Having said that, it feels like an anti pattern for MongoDB where you can store all this information in the same document which makes query a book/article directly and also index tags which allows you to search based on them:

{
		isSample: false,
		tags: [ tag1, tag2],
		owner: "d",
		title: "Rating agencies slow to react",
		pages: 26,
		body: "The ratings agencies should have been the first one to detect problems in the housing market. ",
		use: "In his book 'The Undercover Economist', (Harford, J. 2007) Joe Harford points out that etc, etc.",
		notes: "Need to find other parallels",
		source: [{ name : xxxx , source : "www.example.com"}],
excerpts : [ "...","..."]
	}

Embedded arrays and objects make your objects map to application needs.

Let me know if you have any further questions

Pavel