Quantcast
Channel: Other – Michał Szałkowski – Blog
Viewing all articles
Browse latest Browse all 125

MongoDB – Part 1

$
0
0

To start with mongodb you have to download mongo from this site

next, you have to create:
– folder (/home/USER_NAME/mongodb/)
– configuration file (/home/USER_NAME/mongodb/mongod.conf).
– and folder for logs (/home/USER_NAME/mongodb/log/)

you can use this configuration:

# how to use this file ?
# in cmd use: ./mongod -f /home/USER_NAME/mongodb/mongod.conf
# if you want you can also use ./mongod -dbpath /home/USER_NAME/mongodb

# were data files will reside
dbpath=/home/USER_NAME/mongodb

#where the log file will be stored
logpath=/home/USER_NAME/mongodb/log/mdb-log.log

# how verbos the server will be logging
verbose=vvvvv

If you use linux please use this command to run mongo (goto /mongodb-linux-x86_64-2.6.4/bin):

./mongod -f /home/szalek/mongodb/mongod.conf

Now you have to run mongo client:

mongo

Show database:

show dbs

Switch to database:

use test

Show collections

show collections

mongo4

Insert data to cats collections:

db.cats.insert({_id:1, gender: 'm', name: 'Mikesch', breed: 'Cyprus'});
db.cats.insert({_id:2, gender: 'm', name: 'Toby', breed: 'Ocicat'});
db.cats.insert({_id:3, gender: 'm', name: 'Oliver', breed: 'Korat'});
db.cats.insert({_id:4, gender: 'm', name: 'Leo', breed: 'Cyprus cat'});
db.cats.insert({_id:5, gender: 'm', name: 'Blade', breed: 'Birman'});

db.cats.insert({_id:6, gender: 'f', name: 'Abbie', breed: 'Ocicat'});
db.cats.insert({_id:7, gender: 'f', name: 'Baila', breed: 'Korat'});
db.cats.insert({_id:8, gender: 'f', name: 'Catte', breed: 'Cyprus'});
db.cats.insert({_id:9, gender: 'f', name: 'Cinco', breed: 'Birman'});
db.cats.insert({_id:10, gender: 'f', name: 'Duffy', breed: 'Munchkin'});

Show collections

show collections

Show all items from collection

db.cats.find()

Show 3 items from collection

db.cats.find().limit(3)

mongo8

Show 3 items from collection but skip two first

db.cats.find().limit(3).skip(2)

mongo9

Show all items where gender = m

db.cats.find({gender:'m'})

Show all items where breed = Korat

db.cats.find({breed:'Korat'})

Show all items where breed is Korat or Ocicat

db.cats.find({$or:[{breed:'Korat'},{breed:'Ocicat'}]})

Sorting Records

db.cats.find({$or:[{breed:'Korat'},{breed:'Ocicat'}]}).sort({breed:-1})

or:

db.cats.find({$or:[{breed:'Korat'},{breed:'Ocicat'}]}).sort({breed:1})

Update all records

db.cats.update({}, {$set:{age:0}}, {multi:true})

Update one by one

db.cats.update({_id:1},{$set:{age:2}})
db.cats.update({_id:2},{$set:{age:3}})
db.cats.update({_id:3},{$set:{age:4}})
db.cats.update({_id:4},{$set:{age:5}})
db.cats.update({_id:5},{$set:{age:2}})
db.cats.update({_id:6},{$set:{age:3}})
db.cats.update({_id:7},{$set:{age:4}})
db.cats.update({_id:8},{$set:{age:5}})
db.cats.update({_id:9},{$set:{age:2}})
db.cats.update({_id:10},{$set:{age:2}})

Update one by on, update existing attribute and add new one:

db.cats.update({_id:1},{$set:{age:3, color:'black'}})
db.cats.update({_id:2},{$set:{age:4, color:'brown'}})
db.cats.update({_id:3},{$set:{age:5, color:'white'}})
db.cats.update({_id:4},{$set:{age:6, color:'red'}})
db.cats.update({_id:5},{$set:{age:3, color:'black'}})
db.cats.update({_id:6},{$set:{age:4, color:'brown'}})
db.cats.update({_id:7},{$set:{age:5, color:'white'}})
db.cats.update({_id:8},{$set:{age:6, color:'red'}})
db.cats.update({_id:9},{$set:{age:3, color:'black'}})
db.cats.update({_id:10},{$set:{age:3, color:'brown'}})


Viewing all articles
Browse latest Browse all 125

Trending Articles