(1) To download the MongoDB package, it is easy to find on the MongoDB official website, and then delete the extra stuff, just keep the bin folder, create the Data folder under the directory of the Bin folder sibling, and create the Mongo.log file under the log folder.
(2) Administrator open cmd CD to MONGO bin directory, example below
C:\Program files\mongodb\bin>mongod--dbpath "c:\program Files\mongodb\data"--logpath "c:\program Files\mongodb\ Log\mongo.log "--install--servicename" MongoDB "
(3) MongoDB Basic Grammar Reference Novice Tutorial
(3) Java Operation mongodb,curd operation download version corresponding to the jar package, otherwise error
The code is as follows:
Package CN.MONGO.JDBC;
Import java.util.ArrayList;
Import java.util.Collection;
Import java.util.List;
Import javax.swing.text.DefaultEditorKit.InsertBreakAction;
Import org.bson.Document;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoCursor;
Import Com.mongodb.DB;
Import com.mongodb.MongoClient;
Import Com.mongodb.client.MongoDatabase;
Import Com.mongodb.client.model.Filters;
public class Mongodbjdbc {
public static void Main (string[] args) {
try{
Mongoclient mongoclient=new mongoclient ("127.0.0.1", 27017);
Get collection
Mongodatabase mongodatabase=mongoclient.getdatabase ("Maydb");
Mongocollection<document> collection=mongodatabase.getcollection ("loction");
SYSTEM.OUT.PRINTLN ("Database connection succeeded");
Insert (mongodatabase,collection);
Finditerable (Mongodatabase, collection);
Updata (collection);
Delete (collection);
Collection.deleteone (Filters.eq ("likes", 200));
Delete all documents that match the criteria
}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Library link failed");
}
}
Inserting data
public static int Insert (Mongodatabase mongodatebase,mongocollection<document> collection) {
System.out.println ("Collection selection succeeded!") ");
Document Document=new document ("title", "MongoDB");
Document.append ("description", "Database");
Document.append ("likes", 100);
Document.append ("by", "Fly");
list<document> documents =new arraylist<document> ();
Documents.Add (document);
Collection.insertmany (documents);
System.out.println ("Insert succeeded! ");
return 1;
}
Retrieving documents
Get iterator finditerable<document>
Get cursor mongocursor<document>
The cursor facilitates the retrieval of document sets and
public static int finditerable (Mongodatabase mongodatabase,mongocollection<document> collection) {
Finditerable<document>finditerable=collection.find ();
Mongocursor<document>mongocursor=finditerable.iterator ();
while (Mongocursor.hasnext ()) {
System.out.println (Mongocursor.next ());
}
System.out.println ("Search completed 1");
return 1;
}
/* Update document
* Updatamany ();
*/
public static void Updata (Mongocollection<document> collection) {
Collection.updatemany (Filters.eq ("likes"), New Document ("$set", New document ("likes", 200)));
Search results
Finditerable<document> Finditerable=collection.find ();
Mongocursor<document> Mongocursor=finditerable.iterator ();
while (Mongocursor.hasnext ()) {
System.out.println (Mongocursor.next ());
}
System.out.println ("Search completed 2");
Delete Remove ()
}
public static void Delete (Mongocollection<document> collection) {
Delete First Document
Collection.deleteone (Filters.eq ("likes", 200));
Delete all documents that match the criteria
Collection.deletemany (Filters.eq ("likes", 200));
Finditerable<document> Finditerable=collection.find ();
Mongocursor<document> Mongocursor=finditerable.iterator ();
while (Mongocursor.hasnext ()) {
System.out.println (Mongocursor.next ());
}
System.out.println ("Delete succeeded! ");
}
}
Mongod installation under Windows