This document provides an overview and introduction to getting started with MongoDB. It discusses that MongoDB is a document-oriented database that stores JSON-like documents with dynamic schemas. It can be used to create and store JSON objects and arrange them in collections. The document outlines several MongoDB concepts including its data model, CRUD operations using queries and operators, and working with embedded documents and arrays. It also provides an agenda for a presentation on using MongoDB with topics like schemas, querying, validation, and plugins.
The Ext GWT Data Loading API provides a powerful and flexible mechanism for retrieving remote or local data and binding the data to user interface components. During this session, you will learn will learn the difference between Data Loaders, Proxies and Readers and how they work together.
Introduction to Python and Web ProgrammingDavid Neiss
Slides created for the San Diego Python Users Group to teach our "Intro To Python" class. We have held the class twice already and will continue to do so as there continues to be interest in learning Python.
MongoDB is the coolest NoSQL DB around, partially because it's simple-by-design philosophy. Without transactions or joins, all the bad vibe of SQL is gone.
In this presentation I demonstrate how to get started with MongoDB in the cloud using Mon
Super basic tutorial about how to create an interactive web graphic with d3.js for those that know how to program/use the command line but not any web technology.
The Ext GWT Data Loading API provides a powerful and flexible mechanism for retrieving remote or local data and binding the data to user interface components. During this session, you will learn will learn the difference between Data Loaders, Proxies and Readers and how they work together.
Introduction to Python and Web ProgrammingDavid Neiss
Slides created for the San Diego Python Users Group to teach our "Intro To Python" class. We have held the class twice already and will continue to do so as there continues to be interest in learning Python.
MongoDB is the coolest NoSQL DB around, partially because it's simple-by-design philosophy. Without transactions or joins, all the bad vibe of SQL is gone.
In this presentation I demonstrate how to get started with MongoDB in the cloud using Mon
Super basic tutorial about how to create an interactive web graphic with d3.js for those that know how to program/use the command line but not any web technology.
How to write multi threaded applications using Qt:
In the slides you'll learn about 3 alternatives, all of which allow running tasks simultaneously in Qt applications, and understand the use cases leading to choosing each.
Slides cover how to get started testing your web application. Technologies and concepts explained:
- Unit tests (mocha, jasmine, karma)
- System tests (Selenium)
- Code coverage (istanbul)
- CI servers
Design Patterns help us solve problems in tried and tested ways.
Turns out they also help us understand our framework better, for framework developers also use patterns.
In these slides you'll see how Design Patterns are implemented by Qt framework, to better both understand patterns and Qt
Climate Impact of Software Testing at Nordic Testing DaysKari Kakkonen
My slides at Nordic Testing Days 6.6.2024
Climate impact / sustainability of software testing discussed on the talk. ICT and testing must carry their part of global responsibility to help with the climat warming. We can minimize the carbon footprint but we can also have a carbon handprint, a positive impact on the climate. Quality characteristics can be added with sustainability, and then measured continuously. Test environments can be used less, and in smaller scale and on demand. Test techniques can be used in optimizing or minimizing number of tests. Test automation can be used to speed up testing.
Dr. Sean Tan, Head of Data Science, Changi Airport Group
Discover how Changi Airport Group (CAG) leverages graph technologies and generative AI to revolutionize their search capabilities. This session delves into the unique search needs of CAG’s diverse passengers and customers, showcasing how graph data structures enhance the accuracy and relevance of AI-generated search results, mitigating the risk of “hallucinations” and improving the overall customer journey.
How to write multi threaded applications using Qt:
In the slides you'll learn about 3 alternatives, all of which allow running tasks simultaneously in Qt applications, and understand the use cases leading to choosing each.
Slides cover how to get started testing your web application. Technologies and concepts explained:
- Unit tests (mocha, jasmine, karma)
- System tests (Selenium)
- Code coverage (istanbul)
- CI servers
Design Patterns help us solve problems in tried and tested ways.
Turns out they also help us understand our framework better, for framework developers also use patterns.
In these slides you'll see how Design Patterns are implemented by Qt framework, to better both understand patterns and Qt
Climate Impact of Software Testing at Nordic Testing DaysKari Kakkonen
My slides at Nordic Testing Days 6.6.2024
Climate impact / sustainability of software testing discussed on the talk. ICT and testing must carry their part of global responsibility to help with the climat warming. We can minimize the carbon footprint but we can also have a carbon handprint, a positive impact on the climate. Quality characteristics can be added with sustainability, and then measured continuously. Test environments can be used less, and in smaller scale and on demand. Test techniques can be used in optimizing or minimizing number of tests. Test automation can be used to speed up testing.
Dr. Sean Tan, Head of Data Science, Changi Airport Group
Discover how Changi Airport Group (CAG) leverages graph technologies and generative AI to revolutionize their search capabilities. This session delves into the unique search needs of CAG’s diverse passengers and customers, showcasing how graph data structures enhance the accuracy and relevance of AI-generated search results, mitigating the risk of “hallucinations” and improving the overall customer journey.
Pushing the limits of ePRTC: 100ns holdover for 100 daysAdtran
At WSTS 2024, Alon Stern explored the topic of parametric holdover and explained how recent research findings can be implemented in real-world PNT networks to achieve 100 nanoseconds of accuracy for up to 100 days.
A tale of scale & speed: How the US Navy is enabling software delivery from l...sonjaschweigert1
Rapid and secure feature delivery is a goal across every application team and every branch of the DoD. The Navy’s DevSecOps platform, Party Barge, has achieved:
- Reduction in onboarding time from 5 weeks to 1 day
- Improved developer experience and productivity through actionable findings and reduction of false positives
- Maintenance of superior security standards and inherent policy enforcement with Authorization to Operate (ATO)
Development teams can ship efficiently and ensure applications are cyber ready for Navy Authorizing Officials (AOs). In this webinar, Sigma Defense and Anchore will give attendees a look behind the scenes and demo secure pipeline automation and security artifacts that speed up application ATO and time to production.
We will cover:
- How to remove silos in DevSecOps
- How to build efficient development pipeline roles and component templates
- How to deliver security artifacts that matter for ATO’s (SBOMs, vulnerability reports, and policy evidence)
- How to streamline operations with automated policy checks on container images
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
Epistemic Interaction - tuning interfaces to provide information for AI supportAlan Dix
Paper presented at SYNERGY workshop at AVI 2024, Genoa, Italy. 3rd June 2024
https://alandix.com/academic/papers/synergy2024-epistemic/
As machine learning integrates deeper into human-computer interactions, the concept of epistemic interaction emerges, aiming to refine these interactions to enhance system adaptability. This approach encourages minor, intentional adjustments in user behaviour to enrich the data available for system learning. This paper introduces epistemic interaction within the context of human-system communication, illustrating how deliberate interaction design can improve system understanding and adaptation. Through concrete examples, we demonstrate the potential of epistemic interaction to significantly advance human-computer interaction by leveraging intuitive human communication strategies to inform system design and functionality, offering a novel pathway for enriching user-system engagements.
Full-RAG: A modern architecture for hyper-personalizationZilliz
Mike Del Balso, CEO & Co-Founder at Tecton, presents "Full RAG," a novel approach to AI recommendation systems, aiming to push beyond the limitations of traditional models through a deep integration of contextual insights and real-time data, leveraging the Retrieval-Augmented Generation architecture. This talk will outline Full RAG's potential to significantly enhance personalization, address engineering challenges such as data management and model training, and introduce data enrichment with reranking as a key solution. Attendees will gain crucial insights into the importance of hyperpersonalization in AI, the capabilities of Full RAG for advanced personalization, and strategies for managing complex data integrations for deploying cutting-edge AI solutions.
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...SOFTTECHHUB
The choice of an operating system plays a pivotal role in shaping our computing experience. For decades, Microsoft's Windows has dominated the market, offering a familiar and widely adopted platform for personal and professional use. However, as technological advancements continue to push the boundaries of innovation, alternative operating systems have emerged, challenging the status quo and offering users a fresh perspective on computing.
One such alternative that has garnered significant attention and acclaim is Nitrux Linux 3.5.0, a sleek, powerful, and user-friendly Linux distribution that promises to redefine the way we interact with our devices. With its focus on performance, security, and customization, Nitrux Linux presents a compelling case for those seeking to break free from the constraints of proprietary software and embrace the freedom and flexibility of open-source computing.
GridMate - End to end testing is a critical piece to ensure quality and avoid...ThomasParaiso2
End to end testing is a critical piece to ensure quality and avoid regressions. In this session, we share our journey building an E2E testing pipeline for GridMate components (LWC and Aura) using Cypress, JSForce, FakerJS…
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
In his public lecture, Christian Timmerer provides insights into the fascinating history of video streaming, starting from its humble beginnings before YouTube to the groundbreaking technologies that now dominate platforms like Netflix and ORF ON. Timmerer also presents provocative contributions of his own that have significantly influenced the industry. He concludes by looking at future challenges and invites the audience to join in a discussion.
Removing Uninteresting Bytes in Software FuzzingAftab Hussain
Imagine a world where software fuzzing, the process of mutating bytes in test seeds to uncover hidden and erroneous program behaviors, becomes faster and more effective. A lot depends on the initial seeds, which can significantly dictate the trajectory of a fuzzing campaign, particularly in terms of how long it takes to uncover interesting behaviour in your code. We introduce DIAR, a technique designed to speedup fuzzing campaigns by pinpointing and eliminating those uninteresting bytes in the seeds. Picture this: instead of wasting valuable resources on meaningless mutations in large, bloated seeds, DIAR removes the unnecessary bytes, streamlining the entire process.
In this work, we equipped AFL, a popular fuzzer, with DIAR and examined two critical Linux libraries -- Libxml's xmllint, a tool for parsing xml documents, and Binutil's readelf, an essential debugging and security analysis command-line tool used to display detailed information about ELF (Executable and Linkable Format). Our preliminary results show that AFL+DIAR does not only discover new paths more quickly but also achieves higher coverage overall. This work thus showcases how starting with lean and optimized seeds can lead to faster, more comprehensive fuzzing campaigns -- and DIAR helps you find such seeds.
- These are slides of the talk given at IEEE International Conference on Software Testing Verification and Validation Workshop, ICSTW 2022.
UiPath Test Automation using UiPath Test Suite series, part 6DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 6. In this session, we will cover Test Automation with generative AI and Open AI.
UiPath Test Automation with generative AI and Open AI webinar offers an in-depth exploration of leveraging cutting-edge technologies for test automation within the UiPath platform. Attendees will delve into the integration of generative AI, a test automation solution, with Open AI advanced natural language processing capabilities.
Throughout the session, participants will discover how this synergy empowers testers to automate repetitive tasks, enhance testing accuracy, and expedite the software testing life cycle. Topics covered include the seamless integration process, practical use cases, and the benefits of harnessing AI-driven automation for UiPath testing initiatives. By attending this webinar, testers, and automation professionals can gain valuable insights into harnessing the power of AI to optimize their test automation workflows within the UiPath ecosystem, ultimately driving efficiency and quality in software development processes.
What will you get from this session?
1. Insights into integrating generative AI.
2. Understanding how this integration enhances test automation within the UiPath platform
3. Practical demonstrations
4. Exploration of real-world use cases illustrating the benefits of AI-driven test automation for UiPath
Topics covered:
What is generative AI
Test Automation with generative AI and Open AI.
UiPath integration with generative AI
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Threats to mobile devices are more prevalent and increasing in scope and complexity. Users of mobile devices desire to take full advantage of the features
available on those devices, but many of the features provide convenience and capability but sacrifice security. This best practices guide outlines steps the users can take to better protect personal devices and information.
2. Whoami
Ynon Perek
http://ynonperek.com
ynon@ynonperek.com
Thursday, January 31, 13
3. Agenda
MongoDB Overview
Mongo Test Drive
Mongo Data Model
CRUD Operations
Working With Files
Thursday, January 31, 13
4. MongoDB Overview
Data Store for
JSON Objects
Thursday, January 31, 13
5. MongoDB Overview
Data Store for
JSON Objects
{
“Name” : “Rose Tyler”
}
Thursday, January 31, 13
6. JSON Objects
A JSON Object is a collection of key/value pairs
Keys are simple strings
Values can be: Numbers, Strings, Arrays, Other Objects,
and more
Thursday, January 31, 13
23. Connecting To The DB
There are two options to work with your new DB
You can use the web console
You can use the command line console
Let’s start with the web.
Thursday, January 31, 13
24. Demo: Creating Documents
Create a few
documents on the web
console
Update the data
Delete some of them
Search by fields
Thursday, January 31, 13
25. Mongo Data Model
Let’s model A blog post
in a blog app
What’s The Data ?
How Should You Save
It ?
Thursday, January 31, 13
28. Lab
Create a DB for musical info
Create a collection called albums
Add info for 3 albums you like, including:
Album Name, Artist, Tracks, Release Date, Genres
Tracks is an array of objects
Genres is an array of strings
Thursday, January 31, 13
29. CRUD Operations
Create, Read, Update and Destroy Data
Thursday, January 31, 13
30. Mongo CRUD
Create is called insert
Read is called find
Update is called update
Destroy is called remove
Thursday, January 31, 13
31. Mongo CRUD
From a developer’s perspective, MongoDB operations are
the same through the driver and through the console
In both cases, operations look like function calls or
method invocations
We’ll use mongo shell for the rest of this chapter
Thursday, January 31, 13
32. Inserting Data
Use the command insert or save to insert a new object
db.collection.insert( obj );
db.collection.insert( array );
Thursday, January 31, 13
33. Inserting Data
Inserting to a new collection creates the collection
Inserting an object with an _id key, it is used as the
object’s id (and must be unique).
Thursday, January 31, 13
34. Reading Data
find and findOne perform read operations
Both take a query
find returns a cursor
findOne returns an object Optional: Fields to
fetch
db.collection.find( <query>, <projection> )
Thursday, January 31, 13
35. Query Document
An empty (or missing) query document returns
everything
db.collection.find({})
db.collection.find()
Thursday, January 31, 13
36. Query Document
Each key/value pair in the query document imposes a
condition on the results (objects that match).
db.movies.find({ “genre” : “indie” });
db.books.find({“pages” : { “$gt” : 100 }});
Thursday, January 31, 13
37. Query Document
Query Object
Each key/value pair in the query document imposes a
condition on the results (objects that match).
db.movies.find({ “genre” : “indie” });
db.books.find({“pages” : { “$gt” : 100 }});
Thursday, January 31, 13
38. Query Document
A compound query means a logical AND on the
conditions.
db.inventory.find(
{
“type” : “snacks”,
“available” : { “$lt” : 10 }
});
Thursday, January 31, 13
39. Quiz: What Is Returned
from alterego publisher
Bruce
{ Earth DC
Wayne
“publisher” :
“DC”
} Peter
Earth Marvel
Parker
Krypton Clark Kent DC
Thursday, January 31, 13
40. Quiz: What Is Returned
from alterego publisher
{ Bruce
“publisher” : Earth DC
Wayne
“DC”,
“from” :
“Earth” Peter
Earth Marvel
} Parker
Krypton Clark Kent DC
Thursday, January 31, 13
41. More Queries
You can use “$or” to have an OR expression
{
“$or” : [
{ “type” : “food” },
{ “type” : “drinks” }
]
}
Thursday, January 31, 13
42. Sub Documents
If your document has sub-documents, it’s possible to
query by a full sub document or look for a partial match
Full sub-document query means subdocument is exactly
as specified in the query
Example:
{ ISBN : { “ISBN-10” : “1906465592”,
“ISBN-13” : “978-1906465599” }}
Thursday, January 31, 13
43. Sub Documents
A partial query matches all objects that have at least the
required field (but may contain more)
Example:
{
“language.primary” : “english”
}
Value of language is an object, and it has a field called
primary
Thursday, January 31, 13
44. Arrays
You can use an exact array match by providing the full
array in the query
Example:
{
tags : [ “funny”, “cute”, “cats” ]
}
Thursday, January 31, 13
45. Arrays
You can query for an array that has at least one element
matching the query
Example:
{ “tags” : “funny” }
Thursday, January 31, 13
46. Arrays
If you have a subdocument as the element of an array,
it’s possible to query by its fields using the dot notation.
Examples:
{ “tracks.4.name” : “Rose Mary Stretch” }
{ “tracks.name” : “Rose Mary Stretch” }
Thursday, January 31, 13
47. Query Operators
Complex queries are performed with special operators.
These are reserved words starting with a $
Some of them: $gt, $gte, $lt, $lte, $ne, $in, $nin, $all,
$or, $not
Thursday, January 31, 13
48. Comparator Queries
Value for key a is greater than 10
{ “a” : { “$gt” : 10 }}
Value for key b is not 7
{ “b” : { “$ne” : 7 }}
Value for key name is greater (dictionary sort) than
‘bird’
{ “name” : { “$gt” : “bird” }}
Thursday, January 31, 13
49. Queries: $in, $nin
Use $in to specify a choice from multiple options
Value for grade is 85, 90 or 100
{ “grade” : { “$in” : [ 85, 90, 100 ] } }
Value for fruit is neither apple nor banana
{ “fruit” : { “$nin” : [“apple”, “banana” ] } }
Thursday, January 31, 13
50. Quiz: What Is Selected
{ “reads” : { “$gt” : 10 },
“author” : { “$nin” : [“admin”, “manager”, “boss” ] } }
author reads title
How To Use
admin 99
Mongo
How To Make
Joe 120
Money
Windows
Jim 8
Manual
Thursday, January 31, 13
51. Queries: $all
Select objects with array containing all elements
Example:
{ “tags” : { “$all” : [ “funny”, “cats” ] } }
Thursday, January 31, 13
52. More Query Operators
“$size” - array has a specific number of elements
“$exists” - field present or missing
Example:
{ “friends” : { “$size” : 7 } }
{ “producer” : { “$exists” : false } }
Thursday, January 31, 13
53. Aggregation
count() - returns how many objects found
distinct() - returns all distinct values for a key
Example:
db.posts.distinct( “tags” )
Thursday, January 31, 13
56. Lab
Using the previously defined musical DB. Query for:
Albums released after/before 2008
Albums with 7 tracks
Albums by a specific genre
Albums by a specific track name
Display ALL different genres in the DB
Thursday, January 31, 13
57. Update
Update operations modify existing data in the DB
Mongo supports two update commands:
update() and save()
Update is the more general (and complex)
Thursday, January 31, 13
58. Update
The general form for update is:
db.collection.update(
<query>, <update>, <options> )
Which Entries What to do with
to update them
Thursday, January 31, 13
59. Update
The second argument to update() is an operator object
It tells update what to do with the data
Some keys you can use: “$set”, “$inc” “$push”,
“$pushAll”, “$addToSet”, “$pop”, “$pull”, “$pullAll”
Thursday, January 31, 13
60. Update: set
$set modifies a value or add a new value
Example:
db.posts.update(
{ title: “Why Is Your Cat Unhappy” },
{ $set : { “archived” : true } }
);
Thursday, January 31, 13
61. Quiz: $set
What happens here ?
db.cats.update(
{ color: “white” },
{ “$set” : { “owners” : [“John”, “Jim”] } }
);
Thursday, January 31, 13
62. Quiz: $set
Update owners array of the first cat with white color
If you want to update all objects, use multi
db.cats.update(
{ color: “white” },
{ “$set” : { “owners” : [“John”, “Jim”] } }
{ multi : true }
);
Thursday, January 31, 13
63. Update: inc
$inc increases a numeric value
Example:
{ “$inc” : { “age” : 11 } }
Thursday, January 31, 13
64. Quiz: $inc
What happens here ?
db.songs.update(
{ “title” : “Killing Lies” },
{ “$inc” : { “plays” : 1 } }
);
Thursday, January 31, 13
65. Update: push and pushAll
push() and pushAll() add items to an existing array
If they array did not exists, it is created
Example:
db.creatures.update(
{ name: “The Doctor” },
{ “$push” : { companions : “Rose Tyler” } }
)
Thursday, January 31, 13
66. Update: addToSet
The $addToSet adds a new item only if it wasn’t already
in the array
Example:
{ “$addToSet” : { “tags” : “funny” } }
Thursday, January 31, 13
67. Update: pop
pop removes items of an array
Use a value of 1 to remove the last element
Use a value of -1 to remove the first element
Example:
{ “$pop” : { “companions” : 1 } }
Thursday, January 31, 13
68. Update: pull
Remove a specific item from an array.
Can use $pullAll to remove all matching elements
Example:
{ “$pull” : { “companions” : “Rose Tyler” } }
Thursday, January 31, 13
69. Updating with save()
The second update operation is save()
takes a document:
If the document has an id - update it
If not, insert it to the DB
Thursday, January 31, 13
70. Deleting Data
remove() deletes objects from a collection
Takes a query and possibly a <justOne> arguments
Examples:
db.posts.remove({ “author” : “Father Angelo” })
db.music.remove({ “genres” : “pop” })
db.posts.remove({ “tags” : “funny” }, 1 );
Thursday, January 31, 13
72. Lab
From the previous music database:
Add a new album with 4 tracks
Add a new track to that new album
Set property “plays” on all albums to 6
Increase it by 4 only for “indie” albums
Delete all “indie” music
Thursday, January 31, 13
73. Mongoose
MongoDB + Node.JS
Thursday, January 31, 13
74. What’s That
An Object Relational Mapper for Node.JS
Handles gory details so you don’t have to
Fat Models
Thursday, January 31, 13
75. Agenda
Hello Mongoose
Schema and Data Types
Custom Validators
Querying Data
Poor Man’s Joins (Populate)
Mongoose Plugins
Thursday, January 31, 13
76. Online Resources
http://mongoosejs.com/
https://github.com/LearnBoost/mongoose
http://www.youtube.com/watch?v=4fQsDiioj3I
irc: #mongoosejs on freenode
Thursday, January 31, 13
77. Hello Mongoose
var mongoose = require('mongoose');
mongoose.connect('localhost', 'test');
var schema = mongoose.Schema({ name: 'string' });
var Cat = mongoose.model('Cat', schema);
var kitty = new Cat({ name: 'Zildjian' });
kitty.save(function (err) {
if (err) // ...
console.log('meow');
});
Thursday, January 31, 13
78. Mongoose Objects
Schema Schema
Model Model Model
Thursday, January 31, 13
79. Mongoose Objects
var mongoose = require('mongoose');
mongoose.connect('localhost', 'test'); { name: String}
var schema = mongoose.Schema(
{ name: 'string' });
Cat
var Cat = mongoose.model(
'Cat', schema);
var kitty = new Cat( kitty
{ name: 'Zildjian' });
Thursday, January 31, 13
80. Schema Definitions
new Schema({
title: String,
A schema takes a
body: String,
description object
date: Date,
which specifies its keys
hidden: Boolean,
and their types meta: {
votes: Number,
Types are mostly favs: Number
normal JS }
});
Thursday, January 31, 13
81. Schema Types
String
Number
Date
Buffer
Boolean
Mixed
ObjectId
Array
Thursday, January 31, 13
82. Nested Objects
var PersonSchema = new Schema({
Creating nested objects name: {
first: String,
is easy
last: String
}
Just assign an object as });
the value
Thursday, January 31, 13
83. Array Fields
var PersonSchema = new Schema({
Array fields are easy name: {
first: String,
last: String
Just write the type as a },
single array element hobbies: [String]
});
Thursday, January 31, 13
84. Schema Use Case
Let’s start writing a
photo taking app var PhotoSchema = new Schema({
username: String,
Each photo is saved photo: String,
in the DB as a Data uploaded_at: Date
URL });
var Photo = mongoose.model(
Along with the
'Photo', PhotoSchema);
photo we’ll save the
username
Thursday, January 31, 13
85. Creating New Objects
Create a new object
by instantiating the var mypic = new Photo({
username: 'ynon',
model photo: 'foo',
uploaded_at: new Date()
Pass the values to });
the ctor
Thursday, January 31, 13
86. Creating New Objects
After the object is
ready, simply save it
mypic.save();
Thursday, January 31, 13
87. What Schema Can Do For You
Add validations on var PhotoSchema = new Schema({
the fields username:
{ type: String, required: true },
Stock validators: photo:
{ type: String, required: true },
required, min, max
uploaded_at: Date
Can also create });
custom validators
Validation happens
on save
Thursday, January 31, 13
88. What Schema Can Do For You
Provide default var PhotoSchema = new Schema({
values for fields username:
{ type: String, required: true },
Can use a photo:
function as { type: String, required: true },
default for uploaded_at:
{ type: Date, default: Date.now }
delayed
});
evaluation
Thursday, January 31, 13
89. What Schema Can Do For You
Add methods to your documents
var EvilZombieSchema = new Schema({
name: String,
brainz: { type: Number, default: 0 }
});
EvilZombieSchema.methods.eat_brain = function() {
this.brainz += 1;
};
Thursday, January 31, 13
90. Custom Validators
It’s possible to use your own validation code
var toySchema = new Schema({
color: String,
name: String
});
toySchema.path('color').validate(function(value) {
return ( this.color.length % 3 === 0 );
});
Thursday, January 31, 13
91. Schema Create Indices
A schema can have some fields marked as “index”. The
collection will be indexed by them automatically
var PhotoSchema = new Schema({
username: { type: String, required: true, index: true },
photo: { type: String, required: true },
uploaded_at: { type: Date, default: Date.now }
});
Thursday, January 31, 13
92. Schemas Create Accessors
A virtual field is not saved in the DB, but calculated from
existing fields. “full-name” is an example.
personSchema.virtual('name.full').get(function () {
return this.name.first + ' ' + this.name.last;
});
personSchema.virtual('name.full').set(function (name) {
var split = name.split(' ');
this.name.first = split[0];
this.name.last = split[1];
});
Thursday, January 31, 13
94. Querying Data
Use Model#find / Model#findOne to query data
// executes immediately, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }},
function (err, docs) {
// do something with data
// or handle err
});
Thursday, January 31, 13
95. Querying Data
You can also chain queries by not passing a callback
Pass the callback at the end using exec
var p = Photo.find({username: 'ynon'}).
skip(10).
limit(5).
exec(function(err, docs) {
console.dir( docs );
});
Thursday, January 31, 13
97. Counting Matches
Use count to discover how many matching documents
are in the DB
Adventure.count({ type: 'jungle' }, function (err, count) {
if (err) ..
console.log('there are %d jungle adventures', count);
});
Thursday, January 31, 13
98. Lab
Create a Schema called “Album”
Add fields: artist, year, tracks
Create a model and a document
Add validator for year
Save it in the DB
Thursday, January 31, 13
99. Lab
Create 5 albums from years 2008, 2009, 2010, 2011,
2012
Query the 3 newest albums
Print the artist name and the number of tracks
Print the artist who has the most albums
Thursday, January 31, 13
101. Start With Relationships
Execute the following to create an initial relationship
https://gist.github.com/4657446
Watch the data in the DB:
Album.artist = ObjectId("5106b6e6fde8310000000001")
Thursday, January 31, 13
102. Use Query#populate
query#populate sends another query for the related
object
Album.findOne().exec(function(err, doc) {
// prints undefined
console.log( doc.artist.name );
});
Album.findOne().populate('artist').exec(function(err, doc) {
// prints Pink Floyd
console.log( doc.artist.name );
});
Thursday, January 31, 13
103. Use Query#populate
Full method signature:
Query#populate( path, [fields], [model], [cond],
[options] )
cond is a query condition object ( i.e.
{ age: { $gte: 21 }}
options is a query options object ( i.e.
{ limit: 5 }
Helps when populating arrays
Thursday, January 31, 13
104. Mongoose Plugins
A plugin connects to
the Schema and
extends it in a way
Thursday, January 31, 13
105. Mongoose Plugins
A mongoose plugin is a simple function which takes
schema and options
Demo: lastModifiedPlugin
https://gist.github.com/4657579
Thursday, January 31, 13
106. Mongoose Plugins
find or create plugin:
https://github.com/drudge/mongoose-findorcreate
Thursday, January 31, 13
107. Mongoose Plugins
Hashed password field plugin:
https://gist.github.com/4658951
Thursday, January 31, 13
108. Mongoose Plugins
Mongoose troops is a collection of useful mongoose
plugins:
https://github.com/tblobaum/mongoose-troop
Thursday, January 31, 13
109. Thank You
Photos from: http://123rf.com
Slides available at: http://ynonperek.com
Thursday, January 31, 13