db.Shirts.remove({});
db.Shirts.insertMany(
[
{
Brand:"Calvin",
Name: "Name 1",
Color: ["Blue", "Red"],
Price: 100,
Size: {
height:10, width:5
}
},
{
Brand:"Tommy",
Name: "Name 2",
Color: ["Blue", "Red", "Orange"],
Price: 10,
Size: {
height:10, width:5
}
}
,
{
Brand:"Calvin Kleins",
Name: "Name 3",
Color: ["Blue", "Red", "Orange"],
Price: 50,
Size: {
height:10, width:5
}
}
]
);
// related to: Update the Brand to Calvin Klein when you see only Calvin
db.Shirts.find( {Brand:"Calvin"} );
db.Shirts.find({Brand:{regex:/Calvin/}});
db.Shirts.find({Brand:"Calvin"});
db.Shirts.find({Brand:"Tommy"});
db.Shirts.updateMany({Brand:"Calvin"}, {$set:{Brand:"Calvin Klein"}} );
db.Shirts.find({});
db.Shirts.find(
{
Brand:"Calvin"
}
);
db.Shirts.find( {
Brand:{
$gt:2
}
}
);
// https://www.mongodb.com/docs/manual/reference/operator/query/regex/
db.Shirts.find({
Brand: { $regex: /Cal/ }
});
// Find Shirts that costs between 50 and 100
db.Shirts.find(
{
Price:
{
$gte:50, $lte:100
}
}
);
// Find Shirts that costs between 50 and 100
db.Shirts.find(
{
$and:[
{Price:{$gte:50}},
{Price:{$lte:100}}
]
}
);
// Find shirts where the color is Blue or Red
db.Shirts.find (
{
Color: {$in:["Red", "Blue"]}
}
);
db.Shirts.find({"_id" : ObjectId("660191c185c168d7ae91df2f")});
db.collection.findById('4ecbe7f9e8c1c9092c000027');
let id = '660191c185c168d7ae91df2f';
let o_id = new ObjectId(id);
db.Shirts.findOne({"_id":o_id});
db.runCommand(
{
create: "Shirts",
clusteredIndex: {
"key": { _id: 1 },
"unique": true,
"name": "ShirtsClusteredKey"
}
}
);
db.movies.aggregate([
{
$search: {
index: "default", // optional unless you named your index something other than "default"
text: {
query: "star wars",
path: "title"
},
},
},
{
$project: {
title: 1,
year: 1,
}
}
])
db.runCommand( {
create: "Shirts",
clusteredIndex: { "key": { price: 1 }, "unique": true, "name": "products clustered key" }
} )
db.Shirts.createIndex( { Price:1 } )
db.Shirts.aggregate([
{
$search: {
index: "Price", // optional unless you named your index something other than "default"
text: {
query: "Tommy",
path: "Brand"
},
},
},
{
$project: {
Brand: 1,
Price: 1,
}
}
])
db.Shirts.getIndexes()
db.Shirts.find({Price:{$gt:10}});
var shrt = db.Shirts.find({});
while (shrt.hasNext() ) {
print(tojson(shrt.next()))
}
var shrt = db.Shirts.find({});
print(shrt);
while (shrt.hasNext() ) {
print(shrt.next())
}
db.Reviews.aggregate([
{
$lookup: {
from: "Products",
localField: "product_id",
foreignField: "_id",
as: "movie_details",
},
},
{
$limit: 1
}
])
Aug 07
Shirt Collection and Mongo Queries
Aug 07
MongoDB: Shirts Colection, and Some Mongo Queries
db.Shirts.remove({});
db.Shirts.drop({});
db.Shirts.insertMany(
[
{
Brand:"Calvin",
Name: "Name 1",
Color: ["Blue", "Red"],
Price: 100,
Size: {
height:10, width:5
}
},
{
Brand:"Tommy",
Name: "Name 2",
Color: ["Blue", "Red", "Orange"],
Price: 10,
Size: {
height:10, width:5
}
}
,
{
Brand:"Calvin Kleins",
Name: "Name 3",
Color: ["Blue", "Red", "Orange"],
Price: 50,
Size: {
height:10, width:5
}
}
]
);
// related to: Update the Brand to Calvin Klein when you see only Calvin
db.Shirts.find( {Brand:"Calvin"} );
db.Shirts.find({Brand:{regex:/Calvin/}});
db.Shirts.find({Brand:"Calvin"});
db.Shirts.find({Brand:"Tommy"});
db.Shirts.updateMany({Brand:"Calvin"}, {$set:{Brand:"Calvin Klein"}} );
db.Shirts.find({});
db.Shirts.find(
{
Brand:"Calvin"
}
);
db.Shirts.find( {
Brand:{
$gt:2
}
}
);
// https://www.mongodb.com/docs/manual/reference/operator/query/regex/
db.Shirts.find({
Brand: { $regex: /Cal/ }
});
// Find Shirts that costs between 50 and 100
db.Shirts.find(
{
Price:
{
$gte:50, $lte:100
}
}
);
// Find Shirts that costs between 50 and 100
db.Shirts.find(
{
$and:[
{Price:{$gte:50}},
{Price:{$lte:100}}
]
}
);
// Find shirts where the color is Blue or Red
db.Shirts.find (
{
Color: {$in:["Red", "Blue"]}
}
);
Aug 07
MongoDB: Insert, Collection, Aggregate, Lookup
db.orders.insertMany( [
{ “_id” : 1, “item” : “almonds”, “price” : 12, “quantity” : 2 },
{ “_id” : 2, “item” : “pecans”, “price” : 20, “quantity” : 1 },
{ “_id” : 3 }
] )
db.inventory.insertMany( [
{ “_id” : 1, “sku” : “almonds”, “description”: “product 1”, “instock” : 120 },
{ “_id” : 2, “sku” : “bread”, “description”: “product 2”, “instock” : 80 },
{ “_id” : 3, “sku” : “cashews”, “description”: “product 3”, “instock” : 60 },
{ “_id” : 4, “sku” : “pecans”, “description”: “product 4”, “instock” : 70 },
{ “_id” : 5, “sku”: null, “description”: “Incomplete” },
{ “_id” : 6 }
] );
db.sales.insertMany([
{ “_id” : 1, “item” : “abc”, “price” : Decimal128(“10”), “quantity” : Int32(“2”), “date” : ISODate(“2014-03-01T08:00:00Z”) },
{ “_id” : 2, “item” : “jkl”, “price” : Decimal128(“20”), “quantity” : Int32(“1”), “date” : ISODate(“2014-03-01T09:00:00Z”) },
{ “_id” : 3, “item” : “xyz”, “price” : Decimal128(“5”), “quantity” : Int32( “10”), “date” : ISODate(“2014-03-15T09:00:00Z”) },
{ “_id” : 4, “item” : “xyz”, “price” : Decimal128(“5”), “quantity” : Int32(“20”) , “date” : ISODate(“2014-04-04T11:21:39.736Z”) },
{ “_id” : 5, “item” : “abc”, “price” : Decimal128(“10”), “quantity” : Int32(“10”) , “date” : ISODate(“2014-04-04T21:23:13.331Z”) },
{ “_id” : 6, “item” : “def”, “price” : Decimal128(“7.5”), “quantity”: Int32(“5” ) , “date” : ISODate(“2015-06-04T05:08:13Z”) },
{ “_id” : 7, “item” : “def”, “price” : Decimal128(“7.5”), “quantity”: Int32(“10”) , “date” : ISODate(“2015-09-10T08:43:00Z”) },
{ “_id” : 8, “item” : “abc”, “price” : Decimal128(“10”), “quantity” : Int32(“5” ) , “date” : ISODate(“2016-02-06T20:20:13Z”) },
])
db.orders.aggregate( [
{
$lookup:
{
from: “inventory”,
localField: “item”,
foreignField: “sku”,
as: “inventory_docs”
}
}
] )
db.Products.aggregate( [
{
$lookup:
{
from: “Reviews”,
localField: “ReviewIds”,
foreignField: “_id”,
as: “enrollee_info”
}
}
] )
db.Products.aggregate(
[
{
$lookup: {
from: “Products”,
localField: “ReviewIds”,
foreignField: “_id”,
as: “movie_details”
}
}]
);
Aug 06
MongoDB: Lookup and Aggregate: Reference Model
db.Reviews.drop();
db.Reviews.insertMany([
{_id:1, rating:5, comment: "overall not bad"},
{_id:2, rating:2, comment: "overall not bad"},
{_id:3, rating:3, comment: "overall not bad"},
{_id:4, rating:5, comment: "overall not bad"},
{_id:5, rating:4, comment: "overall not bad"},
{_id:6, rating:5, comment: "overall not bad"}
]);
db.ProductRefModel.drop();
db.ProductRefModel.insertMany([
{
ProductId:1,
Description: "Bottle",
ReviewIds: [ 1, 2 ]
},
{
ProductId: 2,
Description: "Air Pump",
ReviewIds: [3,4]
}
]);
db.ProductRefModel.find();
db.ProductRefModel.aggregate([
{
$lookup: {
from: "Reviews",
localField: "ReviewIds",
foreignField: "_id",
as: "Rating Details"
}
}
]
);
db.Reviews.aggregate([
{
$lookup: {
from: "ProductRefModel",
localField: "_id",
foreignField: "ReviewIds",
as: "Product Details"
}
}
]
);
Jul 28
Binary Tree Traversal using In Order: In C
// traverse the tree in in-order basis
// print the treee content in in-order basis
void inOrder(NodePtr node) {
if (node != NULL) {
inOrder(node->left);
printf("%s ", node->data.word);
inOrder(node->right);
}
}
Jul 28
Binary Tree Traversal in Post Order: In C
// traverse the tree in post-order basis
// print the treee content in post-order basis
void postOrder(NodePtr node) {
if (node != NULL) {
postOrder(node->left);
postOrder(node->right);
printf("%s ", node->data.word);
}
}
Jul 28
Pre Order Traversal of a Binary Tree in C
// pre order traversal
void preOrder(NodePtr node) {
if (node != NULL) {
printf(“%s “, node->data.word);
preOrder(node->left);
preOrder(node->right);
}
}
Jul 28
Steps to Create a Binary Search Tree
If binary tree is empty, create a node, and assign data, point to it
Otherwise : point to root node
Compare the new – data – to – insert with the current node data
We maintain a pointer say current to point to the node under visit
If data matched : nothing to do
If data does not match :
keep traversing left if new data is smaller than current node data(until match or reach leaf / null).
To keep traversing to the left : current = current->left
Then create a new node, and point the left of curr node to this new node
keep traversing right if new data is bigger than current node data(until match or reach leaf / null).
To keep traversing to the right : current = current->right
Then create a new node, and point the right of current node to this new node
Jul 28
Steps to Search in a Hash Table Built Using Graph: Chaining
•Search the Hash
•Read a number/word
•From the user
•Find the position for this number/word
•in the graph (vertical array of nodes)
•By dividing with N or similar
•Check if any vertex node is there
•In that array position (array of graph vertices)
•If no vertex is there
•The word is not in the hash
•If a vertex is there
•Match with the vertex label, if matched you found the word/number
•If no match with the vertex labels
•traverse through the edges
•Until you find a match
•How to Build
•Build the Hash
•Read a number/word from file
•Find the position for this number/word
•in the graph (vertical array of nodes)
•By dividing with N or similar
•Check if any vertex node is there
•In that array position (array of graph vertices)
•If no vertex is there
•Create a vertex node, assign the number/word to that node
•Insert into that position
•If a vertex is there
•If no edge so far, create an edge node, assign number/word
•Else: traverse through the edges
•Go to the end of the edge list
•Create an edge vertex, assign the number/word to that edge node
•
Jul 28
Steps to Build a Hash Table: Using Graph: Chaining
•Build the Hash
•Read a number/word from file
•Find the position for this number/word
•in the graph (vertical array of nodes)
•By dividing with N or similar
•Check if any vertex node is there
•In that array position (array of graph vertices)
•If no vertex is there
•Create a vertex node, assign the number/word to that node
•Insert into that position
•If a vertex is there
•If no edge so far, create an edge node, assign number/word
•Else: traverse through the edges
•Go to the end of the edge list
•Create an edge vertex, assign the number/word to that edge node
•
