MongoDB Basic Use

Source: Internet
Author: User
Tags mongoclient mongodb

MongoDB is a document-type NoSQL database, the official website downloads the address https://www.mongodb.com/download



Linux version of the 64-bit my virtual machine is 32-bit, I am under the Windows version, but the operation is similar.

The download is ready to install next.

Create a database path after installation




CMD Run MongoDB service side

D:\software\mongodbserver\bin/mongod.exe--dbpath D:\software/mongodbdata #服务端

Then start a CMD window to run the client

D:\software\mongodbserver\bin/mongo #客户端

Create DB use
dbxx

Query all DB show
DBS

Delete db use
Runoob
db.dropdatabase ()


Insert Document
Db.col.insert ({title: ' Test ', 
    Description: ' MongoDB is a Nosql database ', by
    : ' Test ',
    URL: ' http:// Www.test.com ',
    Tags: [' mongodb ', ' database ', ' NoSQL '],
    likes:200
})

View the document
Db.col.find ();


Java API Operations MongoDB database

Need a mongo-java-driver jar if you are maven into this address http://mongodb.github.io/mongo-java-driver/




Package com.zit.test;
Import java.util.ArrayList;

Import java.util.List;

Import org.bson.Document;
Import com.mongodb.MongoClient;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoCursor;
Import Com.mongodb.client.MongoDatabase;

Import Com.mongodb.client.model.Filters; public class Mongodbutils {/** * <p> method Description: Establish connection </p>/public static mongodatabase Getmongodbconnecti
		        On () {try{//Connect to MongoDB service mongoclient mongoclient = new Mongoclient ("127.0.0.1", 27017);  
		       Connect to database Mongodatabase Mongodatabase = mongoclient.getdatabase ("Zzqtest");
		       System.out.println ("Connect to Database successfully");
		      return mongodatabase;
		        }catch (Exception e) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ());
		     return null; }/** * <p> method Description: Creating set </p>/private static void CreateCollection(String CollectionName)
		{getmongodbconnection (). CreateCollection (CollectionName);
	System.out.println ("Collection Creation succeeded"); /** * <p> Method Description: Select Set </p> * * private static Mongocollection selectcollection (String collectionname)
		{mongocollection<document> collection = Getmongodbconnection (). GetCollection (CollectionName);
		System.out.println ("Set selection" +collectionname + "success");
	return collection; /** * <p> Method Description: Insert Document </p> * * private static void Insertdocument (Mongocollection<document> Colle
		 ction) {Document Document2 = new Document ("title", "MongoDB");
         Document document = new Document ("title", "MongoDB").  
         Append ("description", Document2).  
         Append ("likes", 100).  
         Append ("by", "Fly");  
         list<document> documents = new arraylist<document> ();  
         Documents.Add (document);  
         Collection.insertmany (documents);
	SYSTEM.OUT.PRINTLN ("Document insert succeeded"); /** * <p> Method Description: ArticleFile Update </p> * * private static void UpdateDocument (Mongocollection<document> collection) {Collection.updatem  
        Any (Filters.eq ("likes"), New Document ("$set", New document ("likes", 200));
	SYSTEM.OUT.PRINTLN ("document updated successfully"); /** * <p> Method Description: Query document </p>/private static void Selectdocument (mongocollection<document> col  
        lection) {//Retrieve view results finditerable<document> finditerable = Collection.find ();  
        mongocursor<document> mongocursor = Finditerable.iterator ();  
        while (Mongocursor.hasnext ()) {System.out.println (Mongocursor.next ()); }/** * <p> method Description: Delete document </p>/private static void Deletedocument (Mongocollection<document&gt ;  
        Collection) {//delete the first document qualifying Collection.deleteone (Filters.eq ("likes", 100));  
	Delete all eligible documents Collection.deletemany (FILTERS.EQ ("likes", 100)); public static void Main (string[] args) {//creatEcollection ("test");
		Mongocollection<document> collection = selectcollection ("test");
		/*insertdocument (collection); * * UpdateDocument (collection);
		Deletedocument (collection);
	Selectdocument (collection);
 }
	
	
	
}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.