tag:blogger.com,1999:blog-51902188501718081622024-03-13T06:44:06.003-07:00Learn mongodb with me Arvind Toorpu“Knowing others is intelligence; knowing yourself is true wisdom. Mastering others is strength; mastering yourself is true power.” - Arvind ToorpuArvind Toorpuhttp://www.blogger.com/profile/02536712992750057274noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-5190218850171808162.post-80964034549047342462016-08-19T08:17:00.003-07:002016-08-19T08:17:44.678-07:00MongoDB Basics -2<div dir="ltr" style="text-align: left;" trbidi="on">
<br />**********************************<br /> GET DB NAME<br />**********************************<br />
<br />
To get current DB name use :<br />
<br />
<br />
<br />> db.getName()<br />test<br /><br />**********************************<br />GET PREVIOUS ERROR IN SESSION<br />**********************************<br />
<br />
To get previous error for the MongoDB use below query :<br />
<br />
<br />
<br />> db.getPrevError()<br />{ "err" : null, "n" : 0, "nPrev" : -1, "ok" : 1 }<br /><br />
<br />
<br />**********************************<br />CHECK MONGODB hostInfo<br />**********************************<br />
<br />
To get the host info for the MongoDB use below query :<br />
<br />
<br />
<br />> db.hostInfo()<br />{<br /> "system" : {<br /> "currentTime" : ISODate("2016-08-19T15:12:29.998Z"),<br /> "hostname" : "OMA-508353L",<br /> "cpuAddrSize" : 64,<br /> "memSizeMB" : 16009,<br /> "numCores" : 8,<br /> "cpuArch" : "x86_64",<br /> "numaEnabled" : false<br /> },<br /> "os" : {<br /> "type" : "Windows",<br /> "name" : "Microsoft Windows 7",<br /> "version" : "6.1 SP1 (build 7601)"<br /> },<br /> "extra" : {<br /> "pageSize" : NumberLong(4096)<br /> },<br /> "ok" : 1<br />}</div>
Arvind Toorpuhttp://www.blogger.com/profile/02536712992750057274noreply@blogger.com0tag:blogger.com,1999:blog-5190218850171808162.post-14332655883648394852016-08-19T07:29:00.000-07:002016-08-19T07:29:02.332-07:00create Collections in MongoDB<div dir="ltr" style="text-align: left;" trbidi="on">
Syntax:<br />Basic syntax of createCollection() command is as follows<br /><br />db.createCollection(name, options)<br />In the command, name is name of collection to be created. Options is a document and used to specify configuration of collection<br /><br />Change Your DB to DB where you want to create collections:<br /><br />> use test<br />switched to db test<br /><br />> db<br />test<br /><br />> show collections<br />><br />><br />> db.createCollection("TESTCOLLECTION", { capped : true, autoIndexID : true, size : 61428, max : 100 } )<br />{ "ok" : 1 }<br /><br />> show collections<br />TESTCOLLECTION<br />mycol<br />tutorialspoint<br />><br /><br />capped Boolean (Optional) If true, enables a capped collection. Capped collection is a collection fixed size collecction that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.<br /><br />autoIndexID Boolean (Optional) If true, automatically create index on _id field.s Default value is false.<br /><br />size number (Optional) Specifies a maximum size in bytes for a capped collection. If If capped is true, then you need to specify this field also.<br /><br />max number (Optional) Specifies the maximum number of documents allowed in the capped collection.<br /><br />While inserting the document, MongoDB first checks size field of capped collection, then it checks max field.<br /><br />Basic syntax of createCollection() method without options is as follows:<br /><br />> db.createCollection2("TESTCOLLECTION2")<br /><br />> db.tutorialspoint.insert({"name" : "tutorialspoint"})<br />WriteResult({ "nInserted" : 1 })<br /><br />> show collections<br />mycol<br />tutorialspoint<br />><br /></div>
Arvind Toorpuhttp://www.blogger.com/profile/02536712992750057274noreply@blogger.com0tag:blogger.com,1999:blog-5190218850171808162.post-33377007017363196302016-08-19T07:27:00.002-07:002016-08-19T07:27:58.376-07:00Connecting to MongoDB<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Change your directory to oracle <br />
<br />
Microsoft Windows [Version 6.1.7601]<br />Copyright (c) 2009 Microsoft Corporation. All rights reserved.<br /><br />C:\Windows\System32>cd C:\MongoDB<br /><br />C:\MongoDB>mongo.exe<br />'mongo.exe' is not recognized as an internal or external command,<br />operable program or batch file.<br /><br />C:\MongoDB>cd bin<br /><br />C:\MongoDB\bin>mongo.exe<br />2016-08-19T09:26:16.495-0500 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files<br />MongoDB shell version: 3.2.4<br />connecting to: test<br />><br />> db.help()<br />DB methods:<br /> db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]<br /> db.auth(username, password)<br /> db.cloneDatabase(fromhost)<br /> db.commandHelp(name) returns the help for the command<br /> db.copyDatabase(fromdb, todb, fromhost)<br /> db.createCollection(name, { size : ..., capped : ..., max : ... } )<br /> db.createUser(userDocument)<br /> db.currentOp() displays currently executing operations in the db<br /> db.dropDatabase()<br /> db.eval() - deprecated<br /> db.fsyncLock() flush data to disk and lock server for backups<br /> db.fsyncUnlock() unlocks server following a db.fsyncLock()<br /> db.getCollection(cname) same as db['cname'] or db.cname<br /> db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections<br /> db.getCollectionNames()<br /> db.getLastError() - just returns the err msg string<br /> db.getLastErrorObj() - return full status object<br /> db.getLogComponents()<br /> db.getMongo() get the server connection object<br /> db.getMongo().setSlaveOk() allow queries on a replication slave server<br /> db.getName()<br /> db.getPrevError()<br /> db.getProfilingLevel() - deprecated<br /> db.getProfilingStatus() - returns if profiling is on and slow threshold<br /> db.getReplicationInfo()<br /> db.getSiblingDB(name) get the db at the same server as this one<br /> db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set<br /> db.hostInfo() get details about the server's host<br /> db.isMaster() check replica primary status<br /> db.killOp(opid) kills the current operation in the db<br /> db.listCommands() lists all the db commands<br /> db.loadServerScripts() loads all the scripts in db.system.js<br /> db.logout()<br /> db.printCollectionStats()<br /> db.printReplicationInfo()<br /> db.printShardingStatus()<br /> db.printSlaveReplicationInfo()<br /> db.dropUser(username)<br /> db.repairDatabase()<br /> db.resetError()<br /> db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }<br /> db.serverStatus()<br /> db.setLogLevel(level,<component>)<br /> db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all<br /> db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db<br /> db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db<br /> db.setVerboseShell(flag) display extra information in shell output<br /> db.shutdownServer()<br /> db.stats()<br /> db.version() current version of the server<br />><br /><br /></div>
Arvind Toorpuhttp://www.blogger.com/profile/02536712992750057274noreply@blogger.com0tag:blogger.com,1999:blog-5190218850171808162.post-22946823225707156852016-04-01T13:19:00.004-07:002016-08-19T07:25:15.383-07:00Install Mongo DB cmd line<div dir="ltr" style="text-align: left;" trbidi="on">
Install MongoDB On Windows<br />
To install the MongoDB on windows, first download the latest release of MongoDB from http://www.mongodb.org/downloads Make sure you get correct version of MongoDB depending upon your windows version. To get your windows version open command prompt and execute following command.<br />
<br />
Microsoft Windows [Version 6.1.7601]<br />
Copyright (c) 2009 Microsoft Corporation. All rights reserved.<br />
<br />
<br />
C:\MongoDB\bin>wmic os get osarchitecture<br />
OSArchitecture<br />
64-bit<br />
<br />
Install MongoDB for Windows.<br />
Change to the directory containing the .msi installation binary of your choice and invoke:<br />
<br />
Directory of C:\MongoDB<br />
<br />
03/29/2016 04:40 PM <DIR> .<br />
03/29/2016 04:40 PM <DIR> ..<br />
03/29/2016 04:38 PM 97,922,560 mongodb-win32-x86_64-3.2.4-signed.msi<br />
1 File(s) 97,922,560 bytes<br />
2 Dir(s) 43,619,405,824 bytes free<br />
<br />
C:\MongoDB>msiexec.exe /q /i mongodb-win32-x86_64-3.2.4-signed.msi INSTALLLOCATION="C:\MongoDB" ADDLOCAL="all"<br />
<br />
C:\MongoDB><br />
<br />
<br />
Configure a Windows Service for MongoDB Community Edition<br />
<br />
Open an Administrator command prompt.<br />
<br />
Press the Win key, type cmd.exe, and press Ctrl + Shift + Enter to run the Command Prompt as Administrator.<br />
<br />
Execute the remaining steps from the Administrator command prompt.<br />
<br />
Create directories.<br />
<br />
Create directories for your database and log files:<br />
<br />
mkdir c:\data\db<br />
mkdir c:\data\log<br />
<br />
<br />
3<br />
Create a configuration file.<br />
<br />
Create a configuration file. The file must set systemLog.path. Include additional configuration options as appropriate.<br />
<br />
For example, create a file at C:\mongodb\mongod.cfg that specifies both systemLog.path and storage.dbPath:<br />
<br />
C:\MongoDB\bin>notepad C:\mongodb\mongod.cfg<br />
<br />
systemLog:<br />
destination: file<br />
path: c:\data\log\mongod.log<br />
storage:<br />
dbPath: c:\data\db<br />
<br />
<br />
<br />
Install the MongoDB service.<br />
<br />
IMPORTANT<br />
Run all of the following commands in Command Prompt with “Administrative Privileges”.<br />
Install the MongoDB service by starting mongod.exe with the --install option and the -config option to specify the previously created configuration file.<br />
<br />
C:\MongoDB\bin>mongod.exe --config "C:\mongodb\mongod.cfg" --install<br />
<br />
C:\MongoDB\bin><br />
<br />
To use an alternate dbpath, specify the path in the configuration file (e.g. C:\mongodb\mongod.cfg) or on the command line with the --dbpath option.<br />
<br />
<br />
Start the MongoDB service.<br />
<br />
C:\MongoDB\bin>net start MongoDB<br />
<br />
The MongoDB service was started successfully.<br />
<br />
<br />
C:\MongoDB\bin><br />
<br /></div>
Arvind Toorpuhttp://www.blogger.com/profile/02536712992750057274noreply@blogger.com0tag:blogger.com,1999:blog-5190218850171808162.post-27845058641632925492016-04-01T13:19:00.001-07:002016-04-01T13:19:02.851-07:00Dropping collections-MongoDB> db.COLLECTION_NAME.drop()<br />
false<br />
<br />
> show collections<br />
mycol<br />
mycollection<br />
tutorialspoint<br />
<br />
> db.mycol.drop()<br />
true<br />
<br />
> show collections<br />
mycollection<br />
tutorialspoint<br />
<br />
<br />
> show collections<br />
mycollection<br />
tutorialspoint<br />
<br />
> db.mycollection.drop()<br />
true<br />
<br />
> db.tutorialspoint.drop()<br />
true<br />
<br />
> show collections<br />
>Arvind Toorpuhttp://www.blogger.com/profile/02536712992750057274noreply@blogger.com0tag:blogger.com,1999:blog-5190218850171808162.post-25817362198329555612016-03-30T08:39:00.000-07:002016-08-19T07:33:14.534-07:00MONGO DB BASICS<div dir="ltr" style="text-align: left;" trbidi="on">
*******************************<br />
LOGIN INTO MONGO DATABASE<br />
*******************************<br />
<br />
C:\MongoDB\bin>mongo.exe<br />
2016-03-30T10:31:31.317-0500 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files<br />
MongoDB shell version: 3.2.4<br />
connecting to: test<br />
> show dbs<br />
local 0.000GB<br />
<br />
*******************************<br />
USE MYDB DATABASE<br />
*******************************<br />
<br />
> use mydb<br />
switched to db mydb<br />
> show dbs<br />
local 0.000GB<br />
> db<br />
mydb<br />
<br />
**********************************<br />
Insert into MYDB<br />
**********************************<br />
> db.movie.insert({"name":"tutorials point"})<br />
WriteResult({ "nInserted" : 1 })<br />
> show dbs<br />
local 0.000GB<br />
mydb 0.000GB<br />
<br />
*******************************<br />
DROP MYDB DATABASE<br />
*******************************<br />
<br />
If you want to drop a database in MONGO DB <mydb>, then dropDatabase() command would be as follows:<br />
<br />
1. change your current DB to MYDB<br />
2. Drop using below cmd.<br />
<br />
> use mydb<br />
switched to db mydb<br />
<br />
> db.dropDatabase()<br />
{ "dropped" : "mydb", "ok" : 1 }<br />
> show dbs<br />
local 0.000GB<br />
><br />
<br />
*******************************<br />CHECK MONGODB VERSION<br />*******************************<br /><br /><br />> db.version()<br />3.2.4 </div>
Arvind Toorpuhttp://www.blogger.com/profile/02536712992750057274noreply@blogger.com0tag:blogger.com,1999:blog-5190218850171808162.post-43230903289302964622015-07-22T17:05:00.000-07:002015-07-22T17:05:01.854-07:00Querying data from Mongodb<b><br /></b>
<b><br /></b>
<b>-- limit output where position : Center results --</b><br />
<br />
db.player.find(<br />
{"position" : "Center"}<br />
<br />
)<br />
<br />
<b>-- limit output where position : Center results & display only name values --</b><br />
<br />
db.player.find(<br />
{"position" : "Center"},<br />
{"name":1}<br />
<br />
)<br />
<br />
<br />
<b>-- limit output to 3 results --</b><br />
<br />
db.player.find(<br />
{"position" : "Center"},<br />
{"name":1,_id:0}<br />
).limit(3)<br />
<br />
-- skip first 3 results --<br />
<br />
db.player.find(<br />
{"position" : "Center"},<br />
{"name":1,_id:0}<br />
).skip(3)<br />
<br />
<br />
<b>-- output to 2 values per doc & search criteria is filtered by position "or" id --</b><br />
<br />
db.player.find(<br />
{<br />
$or:[{"position":"Right Wing"},<br />
{"id":8475761}]<br />
},{"name":1}<br />
).pretty()Arvind Toorpuhttp://www.blogger.com/profile/02536712992750057274noreply@blogger.com0