Java crud for MongoDB

Source: Internet
Author: User
Tags mongo shell

Driver package download: https://github.com/mongodb/mongo-java-driver/downloads
I downloaded mongo-2.8.0.jar-
Version 2.8.0
Open Mongo shell -- create database test -- (use test)
Open eclipse new project, import JUnit, mongo-2.8.0.jar.
The new test class is as follows:

Package COM. DB; import java.net. unknownhostexception; import Java. util. arraylist; import Java. util. list; import Org. bson. types. objectid; import Org. JUnit. after; import Org. JUnit. test; import COM. mongoDB. basicdbobject; import COM. mongoDB. DB; import COM. mongoDB. dbcollection; import COM. mongoDB. dbcursor; import COM. mongoDB. dbobject; import COM. mongoDB. mongo; import COM. mongoDB. except exception; public class dbutils {Private Static Mongo; Private Static dB; static {try {Mongo = new Mongo ("localhost", 27017);} catch (unknownhostexception e) {e. printstacktrace ();} catch (except exception e) {e. printstacktrace ();} dB = Mongo. getdb ("test") ;}@ after public void output () {// after the test, all dbcollection DBS = dB is displayed. getcollection ("user"); dbcursor cursor = DBC. find (); While (cursor. hasnext () {// cursor. next is One line of record (one document) system. out. println (cursor. next () ;}@ test public void testinsert () throws exception {dbcollection DBC = dB. getcollection ("user"); dbobject object = new basicdbobject (); object. put ("username", "zdw"); object. put ("password", "123"); // a person has a position // database JSON format: {Username: "zdw", password: "123", position: {name: "engineer"} basicdbobject o = new basicdbobject (); O. put ("name", "manager"); object. put ("POS Ition ", O); DBC. insert (object); object = new basicdbobject (); object. put ("username", "zhangjun"); object. put ("password", "456"); // one person has multiple positions (one document contains multiple documents) // database JSON format: {Username: "zhangjun ", password: "456", position: [{name: "engineer" },{ name: "designer"}]} List <Object> List = new arraylist <Object> (); O = new basicdbobject (); O. put ("name", "engineer"); list. add (o); O = new basicdbobject (); O. put ("name", "designer"); list. add (O); object. put ("position", list); DBC. insert (object) ;}// Delete the specified @ test public void testremoveone () throws exception {dbcollection DBC = dB. getcollection ("user"); dbobject o = new basicdbobject (); O. put ("username", "zhangjun"); DBC. remove (o) ;}@ test public void testmodify () throws exception {dbcollection DBC = dB. getcollection ("user"); // you need to add $ set to change the password Based on the ID. Otherwise, the password of all records will be modified and other fields will be deleted. Int result = DBC. update (New basicdbobject ("_ id", new objectid ("4ff8eaa65dbc67340278a07c"), new basicdbobject ("$ set", new basicdbobject ("password ", "4444 "))). getn (); system. out. println (result) ;}@ test public void testremove () throws exception {dbcollection DBC = dB. getcollection ("user"); // Delete All dbrs. drop () ;}// query specified @ test public void testfindone () throws exception {dbcollection DBC = dB. getcollection ("user"); dbobject o = new basicdbobject (); O. put ("username", "zdw"); dbobject OBJ = DBC. findone (o); system. out. println (OBJ );}}

This key-value storage method facilitates operations.
Source code download

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.