<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Mongo_DB_-_Related_Models</id>
		<title>Mongo DB - Related Models - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://logicwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Mongo_DB_-_Related_Models"/>
		<link rel="alternate" type="text/html" href="http://logicwiki.co.uk/index.php?title=Mongo_DB_-_Related_Models&amp;action=history"/>
		<updated>2026-10-29T14:55:05Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>http://logicwiki.co.uk/index.php?title=Mongo_DB_-_Related_Models&amp;diff=1640&amp;oldid=prev</id>
		<title>AliIybar: Created page with &quot;Category:Node.js Category:MongoDb  == Using References (Normalization) == Consistency  let author = {      name:'Ali'  }   let course = {     author:'id'   } === How i...&quot;</title>
		<link rel="alternate" type="text/html" href="http://logicwiki.co.uk/index.php?title=Mongo_DB_-_Related_Models&amp;diff=1640&amp;oldid=prev"/>
				<updated>2018-11-07T09:44:52Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&lt;a href=&quot;/Category:Node.js&quot; title=&quot;Category:Node.js&quot;&gt;Category:Node.js&lt;/a&gt; &lt;a href=&quot;/Category:MongoDb&quot; title=&quot;Category:MongoDb&quot;&gt;Category:MongoDb&lt;/a&gt;  == Using References (Normalization) == Consistency  let author = {      name:&amp;#039;Ali&amp;#039;  }   let course = {     author:&amp;#039;id&amp;#039;   } === How i...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Node.js]]&lt;br /&gt;
[[Category:MongoDb]]&lt;br /&gt;
&lt;br /&gt;
== Using References (Normalization) ==&lt;br /&gt;
Consistency&lt;br /&gt;
 let author = { &lt;br /&gt;
    name:'Ali'&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 let course = {&lt;br /&gt;
    author:'id' &lt;br /&gt;
 }&lt;br /&gt;
=== How it's defined in model ===&lt;br /&gt;
 const Course = mongoose.model('Course', new mongoose.Schema({&lt;br /&gt;
    name:string&lt;br /&gt;
    author:{&lt;br /&gt;
       type:mongoose.Schema.Types.ObjectId,&lt;br /&gt;
       ref:'Author'&lt;br /&gt;
    } &lt;br /&gt;
 }));&lt;br /&gt;
=== Querying data (Population) ===&lt;br /&gt;
 async function listCourses(){&lt;br /&gt;
    const courses = await Course&lt;br /&gt;
       .find()&lt;br /&gt;
       .populate('author')&lt;br /&gt;
       .select('name author');&lt;br /&gt;
    console.log(courses);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
in order to include only some of the fields of populated data&lt;br /&gt;
       .populate('author', 'name')&lt;br /&gt;
in order to exclude some of the fields of populated data&lt;br /&gt;
       .populate('author', 'name -_id')&lt;br /&gt;
&lt;br /&gt;
== Using Embedded Documents (Denormalization) ==&lt;br /&gt;
Performance&lt;br /&gt;
 let course = {&lt;br /&gt;
    author:{&lt;br /&gt;
      name:'Ali'&lt;br /&gt;
    } &lt;br /&gt;
 }&lt;br /&gt;
[https://www.udemy.com/nodejs-master-class/learn/v4/t/lecture/9993130?start=0 https://www.udemy.com/nodejs-master-class/learn/v4/t/lecture/9993130?start=0]&lt;br /&gt;
=== How it's defined in model ===&lt;br /&gt;
By giving the schema name :&lt;br /&gt;
 const Course = mongoose.model('Course', new mongoose.Schema({&lt;br /&gt;
    name:string&lt;br /&gt;
    author: authorSchema&lt;br /&gt;
 }));&lt;br /&gt;
=== Creating ===&lt;br /&gt;
 async function createCourse(name, author){&lt;br /&gt;
   const course = new Course({&lt;br /&gt;
     name,&lt;br /&gt;
     author&lt;br /&gt;
 });&lt;br /&gt;
 &lt;br /&gt;
 createCourse('Node Course', new Author({ name:'Ali'}));&lt;br /&gt;
it saves as subdocuments and only updated by parent doc. &lt;br /&gt;
&lt;br /&gt;
=== Update directly in DB ===&lt;br /&gt;
 async function updateAuthor(courseId){&lt;br /&gt;
   const course = await Course.update({ _id: courseId }, {&lt;br /&gt;
      $set: {&lt;br /&gt;
        'author.name:'John Smith'&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
 updateAuthor('123123123');&lt;br /&gt;
== Hybrid ==&lt;br /&gt;
it's good for snapshot of data in a given point of time&lt;br /&gt;
 let author = {&lt;br /&gt;
    name : 'Ali'&lt;br /&gt;
    // other properties &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
 let course = {&lt;br /&gt;
    author: {&lt;br /&gt;
      id:'ref',&lt;br /&gt;
      name:'Ali'&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
-----------------------&lt;br /&gt;
See Also : [[Mongo DB usage in Node]]&lt;/div&gt;</summary>
		<author><name>AliIybar</name></author>	</entry>

	</feed>