Skip to content

Instantly share code, notes, and snippets.

@bitgord
Last active December 13, 2016 05:45
Show Gist options
  • Save bitgord/3ae9fc09b5cb9582a61c7fbef6a5ce77 to your computer and use it in GitHub Desktop.
Save bitgord/3ae9fc09b5cb9582a61c7fbef6a5ce77 to your computer and use it in GitHub Desktop.
Intro to MongoDB Arrays and Embedded Data
// From mongo shell run this to insert
db.customers.insert({
first_name: 'Test',
last_name: 'Testlast',
age: '32',
address: {
street: '111 street',
city: 'Toronto',
province: 'ON',
},
phone: {
home: '111-111-1111',
work: '222-222-2222'
},
services: [
{
service_id: 'hosting',
service_name: 'Linux'
},
{
service_id: 'webdev',
service_name: 'Node'
},
{
service_id: 'database',
service_name: 'Mongo'
}
],
services_count:3
})
// show first service
db.customers.find({first_name:'Test'}, {services:1})
// First service_name 1
db.customers.find({first_name:'Test'}, {services.service_name:1})
// Update and push to Array
db.customers.update({first_name: 'Test'}, {$push: {
services: {
service_id: 'hosting_windows',
service_name: 'Windows'
}
}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment