{
 "title":"Book",
 "price":12.99
}
{
 "name":"Bottle",
 "available":true
}
{
 "title":"Book",
 "price":12.99
}
{
 "title":"Bottle",
 "price":5.99,
 "available": true
}
{
 "title":"Book",
 "price":12.99
}
{
 "title":"Bottle",
 "price":5.99
}

Very  Different                                           Extra Data                                                  Equal

{_id:ObjectId(),name:"John",gender:"male"}
{_id:ObjectId(),name:"Rich",gender:"male"}
{_id:ObjectId(),name:"Mose",gender:"male"}
{_id:ObjectId(),name:"Sami",gender:"male"}
{_id:ObjectId(),name:"Cari",gender:"female"}
{_id:ObjectId(),name:"Mary",gender:"female"}
{
  "name":"Asjad",
  "age": 28,
  "address": {
    "street": "shahra e faisal",
    "city": "Karachi"
  }
}
{
  "name": "Asjad",
  "age": 28,
  "favBooks": ['id1','id2',...]
}
{
  "_id": "id1",
  "name": "Lords of the Rings 1"
}

Customers

Customers

Books

// person
{
  name: "Keon Kim",
  hometown: "Seoul",
  addresses: [
    { city: 'Manhattan', state: 'NY', cc: 'USA' },
    { city: 'Jersey City', state: 'NJ', cc: 'USA' }
  ]
}
// parts
{
  _id: ObjectID('AAAA'),
  partno: '123-aff-456',
  name: 'Awesometel 100Ghz CPU',
  qty: 102,
  cost: 1.21,
  price: 3.99
}
// products
{
  name: 'Weird Computer WC-3020',
  manufacturer: 'Haruair Eng.',
  catalog_number: 1234,
  parts: [
    ObjectID('AAAA'),
    ObjectID('DEFO'),
    ObjectID('EJFW')
  ]
}


// find product based on category_number.
> product = db.products
	.findOne({catalog_number: 1234});
// find all parts in the product's parts list.
> product_parts = db.parts.find({
                   _id: { $in : product.parts } 
                  }).toArray() ;
// host
{
  _id : ObjectID('AAAB'),
  name : 'goofy.example.com',
  ipaddr : '127.66.66.66'
}
// logmsg
{
  time : ISODate("2015-09-02T09:10:09.032Z"),
  message : 'cpu is on fire!',
  // references Host document
  host: ObjectID('AAAB')       
}

// Search parent host document
> host = db.hosts.findOne({ipaddr : '127.66.66.66'});
// Search the most recent 5000 logs
// for a host based on host's ObjectID
> last_5k_msg = db.logmsg.find({host: host._id})
                         .sort({time : -1})
                         .limit(5000)
                         .toArray()
// products - before
{
  name: 'Weird Computer WC-3020',
  manufacturer: 'Haruair Eng.',
  catalog_number: 1234,
  parts: [
    ObjectID('AAAA'),
    ObjectID('DEFO'),
    ObjectID('EJFW')
  ]
}
// products - after
{
  name: 'Weird Computer WC-3020',
  manufacturer: 'Haruair Eng.',
  catalog_number: 1234,
  parts: [ // denormalization
    { id: ObjectID('AAAA'), name: 'Awesometel 100Ghz CPU' },
    { id: ObjectID('DEFO'), name: 'AwesomeSize 100TB SSD' },
    { id: ObjectID('EJFW'), name: 'Magical Mouse' }
  ]
}
// parts - before
{
  _id: ObjectID('AAAA'),
  partno: '123-aff-456',
  name: 'Awesometel 100Ghz CPU',
  qty: 102,
  cost: 1.21,
  price: 3.99
}
// parts - after
{
  _id: ObjectID('AAAA'),
  partno: '123-aff-456',
  name: 'Awesometel 100Ghz CPU',
   // denormalization
  product_name: 'Weird Computer WC-3020',
  // denormalization
  product_catalog_number: 1234,           
  qty: 102,
  cost: 1.21,
  price: 3.99
}
// logmsg - before
{
  time : ISODate("2015-09-02T09:10:09.032Z"),
  message : 'cpu is on fire!',
  host: ObjectID('AAAB')
}
// logmsg - after
{
  time : ISODate("2015-09-02T09:10:09.032Z"),
  message : 'cpu is on fire!',
  host: ObjectID('AAAB'),
  ipaddr : '127.66.66.66'
}
> last_5k_msg = db.logmsg.find({ipaddr : '127.66.66.66'})
                         .sort({time : -1})
                         .limit(5000)
                         .toArray()
db.createCollection("students", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: [ "name", "year", "major" ],
         properties: {
            name: {
               bsonType: "string",
               description: "must be a string and is required"
            },
            year: {
               bsonType: "int",
               minimum: 2017,
               maximum: 3017,
               description: "must be an integer in [ 2017, 3017 ] and is required"
            },
            major: {
               enum: [ "Math", "English", "Computer Science", "History", null ],
               description: "can only be one of the enum values and is required"
            },
            gpa: {
               bsonType: [ "double" ],
               description: "must be a double if the field exists"
            }
         }
      }
   }
})
[{
  country : 'Spain',
  city : 'Salamanca',
  name : 'USAL',
  location : {
    type : 'Point',
    coordinates : [ -5.6722512,17, 40.9607792 ]
  },
  students : [
    { year : 2014, number : 24774 },
    { year : 2015, number : 23166 },
    { year : 2016, number : 21913 },
    { year : 2017, number : 21715 }
  ]
},
{
  country : 'Spain',
  city : 'Salamanca',
  name : 'UPSA',
  location : {
    type : 'Point',
    coordinates : [ -5.6691191,17, 40.9631732 ]
  },
  students : [
    { year : 2014, number : 4788 },
    { year : 2015, number : 4821 },
    { year : 2016, number : 6550 },
    { year : 2017, number : 6125 }
  ]
}]
{
	"_id" : ObjectId("5b7d9d9efbc9884f689cdba9"),
	"country" : "Spain",
	"city" : "Salamanca",
	"name" : "USAL",
	.....
	"foundation_year" : 1218
}

[
{ "_id" : "USAL", "totalalumni" : 91568 },
{ "_id" : "UPSA", "totalalumni" : 22284 }]
[
{ "students" : { "year" : 2014, "number" : 24774 } },
{ "students" : { "year" : 2015, "number" : 23166 } },
{ "students" : { "year" : 2016, "number" : 21913 } },
{ "students" : { "year" : 2017, "number" : 21715 } }]
{ "country" : "Spain", "city" : "Salamanca", "name" : "USAL" }
{ "country" : "Spain", "city" : "Salamanca", "name" : "UPSA" }
{ "_id" : "UPSA", "totaldocs" : 1 }
{ "_id" : "USAL", "totaldocs" : 1 }
[{
	"_id" : ObjectId("5b7d9d9efbc9884f689cdba9"),
	"name" : "USAL",
	"students" : {
		"year" : 2014,
		"number" : 24774
	},
    ...
},
{
	"_id" : ObjectId("5b7d9d9efbc9884f689cdba9"),
	"name" : "USAL",
	"students" : {
		"year" : 2015,
		"number" : 23166
	},
    ...
},
{
	"_id" : ObjectId("5b7d9d9efbc9884f689cdba9"),
	"name" : "USAL",
	"students" : {
		"year" : 2016,
		"number" : 21913
	},
    ....
},
....]
// person
{
  _id: ObjectID("AAF1"),
  name: "Koala",
  tasks [ // reference task document
    ObjectID("ADF9"),
    ObjectID("AE02"),
    ObjectID("AE73")
  ]
}
// tasks
{
  _id: ObjectID("ADF9"),
  description: "Practice Jiu-jitsu",
  due_date:  ISODate("2015-10-01"),
  // reference person document
  owner: ObjectID("AAF1") 
}
Made with Slides.com