Académique Documents
Professionnel Documents
Culture Documents
Basic
of
MongoDB(Basic
commands),
Basic
of
Constructor
s:
DB( Mongo mongo, String name)
Method Summary:
Modifier
and
Descriptio
n
Method
Type
void
addOption(int option)
DBCollectio
n
Set<
String>
getCollectionFromString
(String s)
Mongo
getMongo()
createCollection( String
DBCollection name,
DBObject options)
boolean
authenticate( String
username,
Authenticat
e
database user
char[] password)
void
dropDatabase()
Constructors :
DBCollection(DB base, String name)
Method Summary:
Modifier
Method
Description
and Type
long
count()
void
createIndex( DBObject
keys)
calls
createIndex(com.mongodb.DB
Object,
com.mongodb.DBObject)
with
DBCursor
drop()
WriteResu
insert( DBObject... arr)
lt
WriteResu
remove( DBObject o)
lt
calls
remove(com.mongodb.DBObje
ct,
com.mongodb.WriteConc
ern)
the default WriteConcern
with
Constructors :
public BasicDBObject(int size): creates an empty object
public BasicDBObject(int size): creates an empty object
Constructors :
public DBCursor(DBCollection collection,DBObject q,
DBObject k, ReadPreference preference)): Initializes
a new database cursor
Method Summary:
Modifier
and Method
Description
Type
public boolean
hasNext()
public
DBObject
next()
public void
public int
remove()
length()
Not implemented
pulls back all items into an
array
and returns the number of
objects.
Note: this can be resource
intensive
Conclusion:
Here we performed Connectivity with MongoDB using any Java
application.
//##########################Connect to
Database################# MongoClient mongoClient = new
MongoClient( "localhost" , 27017 );
DB db =
mongoClient.getDB( "MandarMokashi");
System.out.println("Connect to database
successfully");
//###########################Create
Collection################# DBCollection coll =
db.createCollection("Nilesh", null);
System.out.println("Collection created successfully");
//#########################Inserting
Document#################
DBCursor cursor =
coll.find(); int i=1;
while (cursor.hasNext())
{
System.out.println("Inserted Document: "+i);
System.out.println(cursor.next());
i++;
}
//######################Removeing
Document################## DBCollection coll1 =
db.getCollection("mycol");
DBObject myDoc =
coll1.findOne();
coll1.remove(myDoc);
DBCursor cursor1 =
coll.find(); i=1;
while (cursor1.hasNext())
{
System.out.println("Inserted Document: "+i);
System.out.println(cursor1.next());
i++;
}
}catch(Exception e)
{
}
}
}
Output:
Connect to database successfully
Collection created successfully
Document inserted successfully
Inserted Document: 1
{ "_id" : { "$oid" : "45982d53e4b08e9fe2dd47bc"} , "Name" :
"Nilesh Korade" ,
"Qualification" : "M-Tech" , "Post" :
"http://www.dypic.in"