Mongo db

[root@mongoserver ~]# hostname -f
mongoserver.example.com

[root@mongoserver ~]# hostname -I
192.168.183.128

[root@mongoserver ~]# cat >> /etc/yum.repos.d/mongodb.repo
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

[root@mongoserver ~]# yum repolist |grep mongo
mongodb MongoDB Repository 279

[root@mongoserver ~]# yum -y install mongodb-org mongodb-org-server

[root@mongoserver ~]# mongostat
2017-04-12T19:24:07.205+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
couldn’t connect to [127.0.0.1] couldn’t connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed

[root@mongoserver ~]# systemctl status mongod
● mongod.service – SYSV: Mongo is a scalable, document-oriented database.
Loaded: loaded (/etc/rc.d/init.d/mongod)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@mongoserver ~]# systemctl start mongod
[root@mongoserver ~]# systemctl status mongod
● mongod.service – SYSV: Mongo is a scalable, document-oriented database.
Loaded: loaded (/etc/rc.d/init.d/mongod)
Active: active (running) since Wed 2017-04-12 19:24:40 IST; 1s ago
Docs: man:systemd-sysv-generator(8)
Process: 2892 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS)
Main PID: 2905 (mongod)
CGroup: /system.slice/mongod.service
└─2905 /usr/bin/mongod -f /etc/mongod.conf

Apr 12 19:24:39 mongoserver.example.com systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database….
Apr 12 19:24:39 mongoserver.example.com runuser[2901]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Apr 12 19:24:40 mongoserver.example.com mongod[2892]: Starting mongod: [ OK ]
Apr 12 19:24:40 mongoserver.example.com systemd[1]: Started SYSV: Mongo is a scalable, document-oriented database..

[root@mongoserver ~]# mongostat
connected to: 127.0.0.1
insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0 80m 444m 32m 0 local:0.0% 0 0|0 0|0 62b 3k 1 19:25:00

[root@mongoserver ~]# netstat -tlpn |grep mongo
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 2905/mongod
[root@mongoserver ~]# mongostat –rowcount 3 2
connected to: 127.0.0.1
insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 0|0 0 80m 444m 32m 0 local:0.0% 0 0|0 0|0 31b 1k 1 19:26:54
*0 *0 *0 *0 0 0|0 0 80m 444m 32m 0 local:0.0% 0 0|0 0|0 31b 1k 1 19:26:56
*0 *0 *0 *0 0 0|0 0 80m 444m 32m 0 local:0.0% 0 0|0 0|0 31b 1k 1 19:26:58
[root@mongoserver ~]# mongo
MongoDB shell version: 2.6.12
connecting to: test
Server has startup warnings:
2017-04-12T19:24:40.548+0530 [initandlisten]
2017-04-12T19:24:40.548+0530 [initandlisten] ** WARNING: Readahead for /var/lib/mongo is set to 4096KB
2017-04-12T19:24:40.548+0530 [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less
2017-04-12T19:24:40.548+0530 [initandlisten] ** http://dochub.mongodb.org/core/readahead
> db.help()
DB methods:
db.adminCommand(nameOrDocument) – switches to ‘admin’ db, and runs command [ just calls db.runCommand(…) ]
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : …, capped : …, max : … } )
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval(func, args) run code server-side
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db[‘cname’] or db.cname
db.getCollectionInfos()
db.getCollectionNames()
db.getLastError() – just returns the err msg string
db.getLastErrorObj() – return full status object
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() – deprecated
db.getProfilingStatus() – returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() – returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server’s host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.dropUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }
db.serverStatus()
db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
db.setWriteConcern( <write concern doc> ) – sets the write concern for writes to the db
db.unsetWriteConcern( <write concern doc> ) – unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
> exit
bye
[root@mongoserver ~]#

[root@mongoserver ~]# cd /tmp
[root@mongoserver tmp]# curl -LO https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11.3M 100 11.3M 0 0 1146k 0 0:00:10 0:00:10 –:–:– 978k
[root@mongoserver tmp]# mongoimport –db test –collection restaurants –file /tmp/primer-dataset.json
connected to: 127.0.0.1
2017-04-12T20:40:11.016+0530 Progress: 6471417/11874761 54%
2017-04-12T20:40:11.016+0530 12000 4000/second
2017-04-12T20:40:12.163+0530 check 9 25359
2017-04-12T20:40:13.586+0530 imported 25359 objects
[root@mongoserver tmp]#

[root@mongoserver tmp]# mongo
MongoDB shell version: 2.6.12
connecting to: test
Server has startup warnings:
2017-04-12T19:24:40.548+0530 [initandlisten]
2017-04-12T19:24:40.548+0530 [initandlisten] ** WARNING: Readahead for /var/lib/mongo is set to 4096KB
2017-04-12T19:24:40.548+0530 [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less
2017-04-12T19:24:40.548+0530 [initandlisten] ** http://dochub.mongodb.org/core/readahead
> db.restaurants.find().limit( 1 ).pretty()
{
“_id” : ObjectId(“58ee4350a49e3d48be9d564c”),
“address” : {
“building” : “1007”,
“coord” : [
-73.856077,
40.848447
],
“street” : “Morris Park Ave”,
“zipcode” : “10462”
},
“borough” : “Bronx”,
“cuisine” : “Bakery”,
“grades” : [
{
“date” : ISODate(“2014-03-03T00:00:00Z”),
“grade” : “A”,
“score” : 2
},
{
“date” : ISODate(“2013-09-11T00:00:00Z”),
“grade” : “A”,
“score” : 6
},
{
“date” : ISODate(“2013-01-24T00:00:00Z”),
“grade” : “A”,
“score” : 10
},
{
“date” : ISODate(“2011-11-23T00:00:00Z”),
“grade” : “A”,
“score” : 9
},
{
“date” : ISODate(“2011-03-10T00:00:00Z”),
“grade” : “B”,
“score” : 14
}
],
“name” : “Morris Park Bake Shop”,
“restaurant_id” : “30075445”
}
> db.restaurants.drop()
true
> exit
bye
[root@mongoserver tmp]#

###############################################

RDBMS                       MongoDB

Database                   Database
Table                         Collection
Tuple/Row                Document
column                     Field
Table Join                Embedded Documents
Primary Key            Primary Key (Default key _id provided by
mongodb itself)
Database Server    and             Client
Mysqld/Oracle                      mongod
mysql/sqlplus                        mongo

################ Creating users in mongodb ##############

> show users

#################################
> db.createUser( { user: “mohammed”, pwd: “redhat”, roles: [ { role: “read”,db: “income”} ] } )
Successfully added user: {
“user” : “mohammed”,
“roles” : [
{
“role” : “read”,
“db” : “income”
}
]
}
###############################
> db.createUser( { user: “jack”, pwd: “easypassword”,roles: [{role: “read”, db: “mydb”}]})
Successfully added user: {
“user” : “jack”,
“roles” : [
{
“role” : “read”,
“db” : “mydb”
}
]
}
########################
> db.createUser(
… {
… user: “reportsUser”,
… pwd: “12345678”,
… roles: [
… { role: “read”, db: “reporting” },
… { role: “read”, db: “products” },
… { role: “read”, db: “sales” },
… { role: “readWrite”, db: “accounts” }
… ]
… }
… )
Successfully added user: {
“user” : “reportsUser”,
“roles” : [
{
“role” : “read”,
“db” : “reporting”
},
{
“role” : “read”,
“db” : “products”
},
{
“role” : “read”,
“db” : “sales”
},
{
“role” : “readWrite”,
“db” : “accounts”
}
]
}
> show users
{
“_id” : “test.reportsUser”,
“user” : “reportsUser”,
“db” : “test”,
“roles” : [
{
“role” : “read”,
“db” : “reporting”
},
{
“role” : “read”,
“db” : “products”
},
{
“role” : “read”,
“db” : “sales”
},
{
“role” : “readWrite”,
“db” : “accounts”
}
]
}
{
“_id” : “test.mohammed”,
“user” : “mohammed”,
“db” : “test”,
“roles” : [
{
“role” : “read”,
“db” : “income”
}
]
}
{
“_id” : “test.jack”,
“user” : “jack”,
“db” : “test”,
“roles” : [
{
“role” : “read”,
“db” : “mydb”
}
]
}

############################

MongoDB-CheatSheet-v1_0

db.ships.insert({name:’USS Enterprise-D’,operator:’Starfleet’,type:’Explorer’,class:’Galaxy’,crew:750,codes:[10,11,12]})
WriteResult({ “nInserted” : 1 })

> show collections
movie
mycol
mycollection
ships
system.indexes

> db.ships.insert({name:’USS Prometheus’,operator:’Starfleet’,class:’Prometheus’,crew:4,codes:[1,14,17]})
WriteResult({ “nInserted” : 1 })
> db.ships.insert({name:’USS Defiant’,operator:’Starfleet’,class:’Defiant’,crew:50,codes:[10,17,19]})
WriteResult({ “nInserted” : 1 })
> db.ships.insert({name:’IKS Buruk’,operator:’ Klingon Empire’,class:’Warship’,crew:40,codes:[100,110,120]})
WriteResult({ “nInserted” : 1 })
> db.ships.insert({name:’IKS Somraw’,operator:’ Klingon Empire’,class:’Raptor’,crew:50,codes:[101,111,120]})
WriteResult({ “nInserted” : 1 })
> db.ships.insert({name:’Scimitar’,operator:’Romulan Star Empire’,type:’Warbird’,class:’Warbird’,crew:25,codes:[201,211,220]})
WriteResult({ “nInserted” : 1 })
> db.ships.insert({name:’Narada’,operator:’Romulan Star Empire’,type:’Warbird’,class:’Warbird’,crew:65,codes:[251,251,220]})
WriteResult({ “nInserted” : 1 })

> db.ships.count()
7
> db.ships.findOne()
{
“_id” : ObjectId(“58f09068dffc506974ad44bc”),
“name” : “USS Enterprise-D”,
“operator” : “Starfleet”,
“type” : “Explorer”,
“class” : “Galaxy”,
“crew” : 750,
“codes” : [
10,
11,
12
]
}

> db.ships.find({}, {name:true, _id:false})
{ “name” : “USS Enterprise-D” }
{ “name” : “USS Prometheus” }
{ “name” : “USS Defiant” }
{ “name” : “IKS Buruk” }
{ “name” : “IKS Somraw” }
{ “name” : “Scimitar” }
{ “name” : “Narada” }

> db.ships.findOne({‘name’:’USS Defiant’})
{
“_id” : ObjectId(“58f0907edffc506974ad44be”),
“name” : “USS Defiant”,
“operator” : “Starfleet”,
“class” : “Defiant”,
“crew” : 50,
“codes” : [
10,
17,
19
]
}

> db.ships.find({class:{$gt:’P’}})
{ “_id” : ObjectId(“58f09073dffc506974ad44bd”), “name” : “USS Prometheus”, “operator” : “Starfleet”, “class” : “Prometheus”, “crew” : 4, “codes” : [ 1, 14, 17 ] }
{ “_id” : ObjectId(“58f09086dffc506974ad44bf”), “name” : “IKS Buruk”, “operator” : ” Klingon Empire”, “class” : “Warship”, “crew” : 40, “codes” : [ 100, 110, 120 ] }
{ “_id” : ObjectId(“58f0908cdffc506974ad44c0”), “name” : “IKS Somraw”, “operator” : ” Klingon Empire”, “class” : “Raptor”, “crew” : 50, “codes” : [ 101, 111, 120 ] }
{ “_id” : ObjectId(“58f09092dffc506974ad44c1”), “name” : “Scimitar”, “operator” : “Romulan Star Empire”, “type” : “Warbird”, “class” : “Warbird”, “crew” : 25, “codes” : [ 201, 211, 220 ] }
{ “_id” : ObjectId(“58f09099dffc506974ad44c2”), “name” : “Narada”, “operator” : “Romulan Star Empire”, “type” : “Warbird”, “class” : “Warbird”, “crew” : 65, “codes” : [ 251, 251, 220 ] }
> db.ships.find({class:{$lte:’P’}})
{ “_id” : ObjectId(“58f09068dffc506974ad44bc”), “name” : “USS Enterprise-D”, “operator” : “Starfleet”, “type” : “Explorer”, “class” : “Galaxy”, “crew” : 750, “codes” : [ 10, 11, 12 ] }
{ “_id” : ObjectId(“58f0907edffc506974ad44be”), “name” : “USS Defiant”, “operator” : “Starfleet”, “class” : “Defiant”, “crew” : 50, “codes” : [ 10, 17, 19 ] }

> db.ships.find({type:{$exists:true}})
{ “_id” : ObjectId(“58f09068dffc506974ad44bc”), “name” : “USS Enterprise-D”, “operator” : “Starfleet”, “type” : “Explorer”, “class” : “Galaxy”, “crew” : 750, “codes” : [ 10, 11, 12 ] }
{ “_id” : ObjectId(“58f09092dffc506974ad44c1”), “name” : “Scimitar”, “operator” : “Romulan Star Empire”, “type” : “Warbird”, “class” : “Warbird”, “crew” : 25, “codes” : [ 201, 211, 220 ] }
{ “_id” : ObjectId(“58f09099dffc506974ad44c2”), “name” : “Narada”, “operator” : “Romulan Star Empire”, “type” : “Warbird”, “class” : “Warbird”, “crew” : 65, “codes” : [ 251, 251, 220 ] }

> db.ships.find({name:{$regex:’^USS\\sE’}})
{ “_id” : ObjectId(“58f09068dffc506974ad44bc”), “name” : “USS Enterprise-D”, “operator” : “Starfleet”, “type” : “Explorer”, “class” : “Galaxy”, “crew” : 750, “codes” : [ 10, 11, 12 ] }
> db.ships.find({name : {$type:2}})
{ “_id” : ObjectId(“58f09068dffc506974ad44bc”), “name” : “USS Enterprise-D”, “operator” : “Starfleet”, “type” : “Explorer”, “class” : “Galaxy”, “crew” : 750, “codes” : [ 10, 11, 12 ] }
{ “_id” : ObjectId(“58f09073dffc506974ad44bd”), “name” : “USS Prometheus”, “operator” : “Starfleet”, “class” : “Prometheus”, “crew” : 4, “codes” : [ 1, 14, 17 ] }
{ “_id” : ObjectId(“58f0907edffc506974ad44be”), “name” : “USS Defiant”, “operator” : “Starfleet”, “class” : “Defiant”, “crew” : 50, “codes” : [ 10, 17, 19 ] }
{ “_id” : ObjectId(“58f09086dffc506974ad44bf”), “name” : “IKS Buruk”, “operator” : ” Klingon Empire”, “class” : “Warship”, “crew” : 40, “codes” : [ 100, 110, 120 ] }
{ “_id” : ObjectId(“58f0908cdffc506974ad44c0”), “name” : “IKS Somraw”, “operator” : ” Klingon Empire”, “class” : “Raptor”, “crew” : 50, “codes” : [ 101, 111, 120 ] }
{ “_id” : ObjectId(“58f09092dffc506974ad44c1”), “name” : “Scimitar”, “operator” : “Romulan Star Empire”, “type” : “Warbird”, “class” : “Warbird”, “crew” : 25, “codes” : [ 201, 211, 220 ] }
{ “_id” : ObjectId(“58f09099dffc506974ad44c2”), “name” : “Narada”, “operator” : “Romulan Star Empire”, “type” : “Warbird”, “class” : “Warbird”, “crew” : 65, “codes” : [ 251, 251, 220 ] }

###############################

> db.unicorns.insert({name: ‘Aurora’,gender: ‘f’, weight: 450})
WriteResult({ “nInserted” : 1 })

> db.unicorns.find()
{ “_id” : ObjectId(“58f097d7dffc506974ad44c3”), “name” : “Aurora”, “gender” : “f”, “weight” : 450 }

> db.unicorns.count()
1

>db.unicorns.findOne()
{
“_id” : ObjectId(“58f097d7dffc506974ad44c3”),
“name” : “Aurora”,
“gender” : “f”,
“weight” : 450
}

> db.unicorns.getIndexes()
[
{
“v” : 1,
“key” : {
“_id” : 1
},
“name” : “_id_”,
“ns” : “test.unicorns”
}
]

> db.unicorns.insert({name: ‘Leto’,gender: ‘m’,home: ‘Arrakeen’,worm: false})
WriteResult({ “nInserted” : 1 })

> db.unicorns.count()
2

> db.unicorns.find()
{ “_id” : ObjectId(“58f097d7dffc506974ad44c3”), “name” : “Aurora”, “gender” : “f”, “weight” : 450 }
{ “_id” : ObjectId(“58f09881dffc506974ad44c4”), “name” : “Leto”, “gender” : “m”, “home” : “Arrakeen”, “worm” : false }

> db.unicorns.insert({name: ‘Horny’,
… dob: new Date(1992,2,13,7,47),
… loves: [‘carrot’,’papaya’],
… weight: 600,
… gender: ‘m’,
… vampires: 63});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Aurora’,
… dob: new Date(1991, 0, 24, 13, 0),
… loves: [‘carrot’, ‘grape’],
… weight: 450,
… gender: ‘f’,
… vampires: 43});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Unicrom’,
… dob: new Date(1973, 1, 9, 22, 10),
… loves: [‘energon’, ‘redbull’],
… weight: 984,
… gender: ‘m’,
… vampires: 182});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Roooooodles’,
… dob: new Date(1979, 7, 18, 18, 44),
… loves: [‘apple’],
… weight: 575,
… gender: ‘m’,
… vampires: 99});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Solnara’,
… dob: new Date(1985, 6, 4, 2, 1),
… loves:[‘apple’, ‘carrot’,
… ‘chocolate’],
… weight:550,
… gender:’f’,
… vampires:80});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name:’Ayna’,
… dob: new Date(1998, 2, 7, 8, 30),
… loves: [‘strawberry’, ‘lemon’],
… weight: 733,
… gender: ‘f’,
… vampires: 40});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name:’Kenny’,
… dob: new Date(1997, 6, 1, 10, 42),
… loves: [‘grape’, ‘lemon’],
… weight: 690,
… gender: ‘m’,
… vampires: 39});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Raleigh’,
… dob: new Date(2005, 4, 3, 0, 57),
… loves: [‘apple’, ‘sugar’],
… weight: 421,
… gender: ‘m’,
… vampires: 2});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Leia’,
… dob: new Date(2001, 9, 8, 14, 53),
… loves: [‘apple’, ‘watermelon’],
… weight: 601,
… gender: ‘f’,
… vampires: 33});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Pilot’,
… dob: new Date(1997, 2, 1, 5, 3),
… loves: [‘apple’, ‘watermelon’],
… weight: 650,
… gender: ‘m’,
… vampires: 54});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Nimue’,
… dob: new Date(1999, 11, 20, 16, 15),
… loves: [‘grape’, ‘carrot’],
… weight: 540,
… gender: ‘f’});
WriteResult({ “nInserted” : 1 })
> db.unicorns.insert({name: ‘Dunx’,
… dob: new Date(1976, 6, 18, 18, 18),
… loves: [‘grape’, ‘watermelon’],
… weight: 704,
… gender: ‘m’,
… vampires: 165});
WriteResult({ “nInserted” : 1 })

> db.unicorns.find()
{ “_id” : ObjectId(“58f097d7dffc506974ad44c3”), “name” : “Aurora”, “gender” : “f”, “weight” : 450 }
{ “_id” : ObjectId(“58f09881dffc506974ad44c4”), “name” : “Leto”, “gender” : “m”, “home” : “Arrakeen”, “worm” : false }
{ “_id” : ObjectId(“58f09901dffc506974ad44c5”), “name” : “Horny”, “dob” : ISODate(“1992-03-13T02:17:00Z”), “loves” : [ “carrot”, “papaya” ], “weight” : 600, “gender” : “m”, “vampires” : 63 }
{ “_id” : ObjectId(“58f09901dffc506974ad44c6”), “name” : “Aurora”, “dob” : ISODate(“1991-01-24T07:30:00Z”), “loves” : [ “carrot”, “grape” ], “weight” : 450, “gender” : “f”, “vampires” : 43 }
{ “_id” : ObjectId(“58f09901dffc506974ad44c7”), “name” : “Unicrom”, “dob” : ISODate(“1973-02-09T16:40:00Z”), “loves” : [ “energon”, “redbull” ], “weight” : 984, “gender” : “m”, “vampires” : 182 }
{ “_id” : ObjectId(“58f09901dffc506974ad44c8”), “name” : “Roooooodles”, “dob” : ISODate(“1979-08-18T13:14:00Z”), “loves” : [ “apple” ], “weight” : 575, “gender” : “m”, “vampires” : 99 }
{ “_id” : ObjectId(“58f09901dffc506974ad44c9”), “name” : “Solnara”, “dob” : ISODate(“1985-07-03T20:31:00Z”), “loves” : [ “apple”, “carrot”, “chocolate” ], “weight” : 550, “gender” : “f”, “vampires” : 80 }
{ “_id” : ObjectId(“58f09901dffc506974ad44ca”), “name” : “Ayna”, “dob” : ISODate(“1998-03-07T03:00:00Z”), “loves” : [ “strawberry”, “lemon” ], “weight” : 733, “gender” : “f”, “vampires” : 40 }
{ “_id” : ObjectId(“58f09901dffc506974ad44cb”), “name” : “Kenny”, “dob” : ISODate(“1997-07-01T05:12:00Z”), “loves” : [ “grape”, “lemon” ], “weight” : 690, “gender” : “m”, “vampires” : 39 }
{ “_id” : ObjectId(“58f09901dffc506974ad44cc”), “name” : “Raleigh”, “dob” : ISODate(“2005-05-02T19:27:00Z”), “loves” : [ “apple”, “sugar” ], “weight” : 421, “gender” : “m”, “vampires” : 2 }
{ “_id” : ObjectId(“58f09901dffc506974ad44cd”), “name” : “Leia”, “dob” : ISODate(“2001-10-08T09:23:00Z”), “loves” : [ “apple”, “watermelon” ], “weight” : 601, “gender” : “f”, “vampires” : 33 }
{ “_id” : ObjectId(“58f09901dffc506974ad44ce”), “name” : “Pilot”, “dob” : ISODate(“1997-02-28T23:33:00Z”), “loves” : [ “apple”, “watermelon” ], “weight” : 650, “gender” : “m”, “vampires” : 54 }
{ “_id” : ObjectId(“58f09901dffc506974ad44cf”), “name” : “Nimue”, “dob” : ISODate(“1999-12-20T10:45:00Z”), “loves” : [ “grape”, “carrot” ], “weight” : 540, “gender” : “f” }
{ “_id” : ObjectId(“58f09905dffc506974ad44d0”), “name” : “Dunx”, “dob” : ISODate(“1976-07-18T12:48:00Z”), “loves” : [ “grape”, “watermelon” ], “weight” : 704, “gender” : “m”, “vampires” : 165 }

> db.unicorns.count()
14
> db.unicorns.find({gender: ‘m’,weight: {$gt: 700}})
{ “_id” : ObjectId(“58f09901dffc506974ad44c7”), “name” : “Unicrom”, “dob” : ISODate(“1973-02-09T16:40:00Z”), “loves” : [ “energon”, “redbull” ], “weight” : 984, “gender” : “m”, “vampires” : 182 }
{ “_id” : ObjectId(“58f09905dffc506974ad44d0”), “name” : “Dunx”, “dob” : ISODate(“1976-07-18T12:48:00Z”), “loves” : [ “grape”, “watermelon” ], “weight” : 704, “gender” : “m”, “vampires” : 165 }

> db.unicorns.find({vampires: {$exists: false}})
{ “_id” : ObjectId(“58f097d7dffc506974ad44c3”), “name” : “Aurora”, “gender” : “f”, “weight” : 450 }
{ “_id” : ObjectId(“58f09881dffc506974ad44c4”), “name” : “Leto”, “gender” : “m”, “home” : “Arrakeen”, “worm” : false }
{ “_id” : ObjectId(“58f09901dffc506974ad44cf”), “name” : “Nimue”, “dob” : ISODate(“1999-12-20T10:45:00Z”), “loves” : [ “grape”, “carrot” ], “weight” : 540, “gender” : “f” }

> db.unicorns.find({loves: {$in:[‘apple’,’orange’]}})
{ “_id” : ObjectId(“58f09901dffc506974ad44c8”), “name” : “Roooooodles”, “dob” : ISODate(“1979-08-18T13:14:00Z”), “loves” : [ “apple” ], “weight” : 575, “gender” : “m”, “vampires” : 99 }
{ “_id” : ObjectId(“58f09901dffc506974ad44c9”), “name” : “Solnara”, “dob” : ISODate(“1985-07-03T20:31:00Z”), “loves” : [ “apple”, “carrot”, “chocolate” ], “weight” : 550, “gender” : “f”, “vampires” : 80 }
{ “_id” : ObjectId(“58f09901dffc506974ad44cc”), “name” : “Raleigh”, “dob” : ISODate(“2005-05-02T19:27:00Z”), “loves” : [ “apple”, “sugar” ], “weight” : 421, “gender” : “m”, “vampires” : 2 }
{ “_id” : ObjectId(“58f09901dffc506974ad44cd”), “name” : “Leia”, “dob” : ISODate(“2001-10-08T09:23:00Z”), “loves” : [ “apple”, “watermelon” ], “weight” : 601, “gender” : “f”, “vampires” : 33 }
{ “_id” : ObjectId(“58f09901dffc506974ad44ce”), “name” : “Pilot”, “dob” : ISODate(“1997-02-28T23:33:00Z”), “loves” : [ “apple”, “watermelon” ], “weight” : 650, “gender” : “m”, “vampires” : 54 }
>
> db.unicorns.find({gender: ‘f’,$or: [{loves: ‘apple’},{weight: {$lt: 500}}]})
{ “_id” : ObjectId(“58f097d7dffc506974ad44c3”), “name” : “Aurora”, “gender” : “f”, “weight” : 450 }
{ “_id” : ObjectId(“58f09901dffc506974ad44c6”), “name” : “Aurora”, “dob” : ISODate(“1991-01-24T07:30:00Z”), “loves” : [ “carrot”, “grape” ], “weight” : 450, “gender” : “f”, “vampires” : 43 }
{ “_id” : ObjectId(“58f09901dffc506974ad44c9”), “name” : “Solnara”, “dob” : ISODate(“1985-07-03T20:31:00Z”), “loves” : [ “apple”, “carrot”, “chocolate” ], “weight” : 550, “gender” : “f”, “vampires” : 80 }
{ “_id” : ObjectId(“58f09901dffc506974ad44cd”), “name” : “Leia”, “dob” : ISODate(“2001-10-08T09:23:00Z”), “loves” : [ “apple”, “watermelon” ], “weight” : 601, “gender” : “f”, “vampires” : 33 }
>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s